question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow modifications on the adsense code

See original GitHub issue

Currently there isn’t any way to make modifications (ex. make anchor tags always come from bottom) to the adsense code. Make it easier for new/novice users to do this kind of modifications.

Feature Description

Within AdSense Auto Ads there are different ad formats (Display ads, in-fedd ads, matched etc) that users can enable or disable globally across their sites.

Some users would like additional options from within Site Kit (specifically the AdSense module) to toggle such ad formats.

While the first step would naturally be allowing users to select specific ad formats some users would benefit from follow on features. The ability to disable anchor ads at the top of a page is an example. At the moment this feature must be implemented via adding parameters to Auto Ads code. With Site Kit placing the Auto Ads code this is not easily achievable for users.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The options which are passed to the AdSense snippet that Site Kit places should be filterable.
  • For non-AMP:
    • The associative array that is currently JSON-encoded should first be passed through a new googlesitekit_auto_ads_opt filter.
    • It should be ensured though that the google_ad_client key is never overwritten (e.g. by re-setting it afterwards, similarly to how it is done in the AMP tag for Analytics.
  • For AMP (amp-auto-ads):
    • There should be a filterable array which contains only a data-ad-client key with the tag ID as value. It should be passed through a new googlesitekit_amp_auto_ads_attributes filter.
    • It should be ensured that the data-ad-client key is never overwritten.
    • Then, every key in the array should be added as an attribute to the amp-auto-ads tag (so that by default the tag looks the same as today, with a data-ad-client attribute).
  • For Web Stories (amp-story-auto-ads):
    • There should be a filterable array which contains data-ad-client and data-ad-slot keys with the respective values as currently used. It should be passed through a new googlesitekit_amp_story_auto_ads_attributes filter.
    • It should be ensured that the data-ad-client key is never overwritten.
    • Then, the array should be merged into the ad-attributes array currently used, appending to the type key (which should not be hard-coded because it should always be adsense, basically like a constant, as it is also enforced in the other 2 snippets).

Note that the filter name suffix _opt was chosen for parity with the similar Analytics filters. For AMP however, what’s being filtered is not direct options passed as AdSense configuration, but rather attributes - that’s why these two filters use the _attributes suffix.

Implementation Brief

  • Using includes/Modules/AdSense/Web_Tag.php, within the render method,
    • Create a new variable which has the array within the wp_json_encode function as value.
    • Pass the above variable through the googlesitekit_auto_ads_opt filter.
    • If the filtered array is empty or is not an array, set the filtered array to be the non filtered array.
    • To make sure the google_ad_client key is not overwritten in the filtered array, set the value again in the array, i.e $this->tag_id.
    • Use the filtered array within the wp_json_encode function.
  • Using includes/Modules/AdSense/AMP_Tag.php, within the render method,
    • Create a new variable which is an array containing an item with data-ad-client as key and the tag ID as value.
    • Pass the above variable through the googlesitekit_amp_auto_ads_attributes filter.
    • If the filtered array is empty or is not an array, set the filtered array to be the non filtered array.
    • To make sure the data-ad-client key is not overwritten in the filtered array, set the value again in the array, i.e $this->tag_id.
    • Update the rendered markup to display all key values from the filtered array.
  • Using includes/Modules/AdSense/AMP_Tag.php, within the render_story_auto_ads method,
    • Create a new variable which is an array which contains the data-ad-client and data-ad-slot with their appopriate values, i.e $this->tag_id and $this->story_ad_slot_id respectively.
    • Pass the above variable through the googlesitekit_amp_story_auto_ads_attributes filter.
    • If the filtered array is empty or is not an array, set the filtered array to be the non filtered array.
    • To make sure the data-ad-client key is not overwritten in the filtered array, set the value again in the array, i.e $this->tag_id.
    • Merge the filtered array with the ad-attributes array.

Test Coverage

  • No new tests to be added.

Visual Regression Changes

  • N/A

QA Brief

  • Verify if the conditions in the ACs are met.
  • Or, connect AdSense module and edit the theme’s functions.php to run the filters in the AC.
    • For e.g
    add_filter( 'googlesitekit_auto_ads_opt', function () {
    	return array( 'tag_partner' => 'my-custom-tag-partner' );
    } );
    
  • Verify markup on the front end contains the appropriate filtered values.

Changelog entry

  • Add filters to allow modifications on the AdSense code.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
asvinbcommented, Aug 16, 2021

@ivankruchkoff So, turns out that if we don’t have a story specific ad unit, the snippet will not be displayed: https://github.com/google/site-kit-wp/blob/develop/includes/Modules/AdSense/Tag_Guard.php#L41

Can you add add an unit id in AdSense settings and test again? image

Thanks!

1reaction
felixarntzcommented, Apr 7, 2021

Given that the AdSense API doesn’t allow write interactions, unfortunately we cannot manage these settings from within Site Kit. However, being able to modify the AdSense snippet is definitely a great idea. We’re going to prioritize this one and introduce a filter which can be used to modify the snippet parameters.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modifications of AdSense ad code
Some AdSense code modifications are not permitted. Learn about acceptable ad code modifications and what to avoid when implementing your code.
Read more >
How to Modify Adsense Code for Responsive Design
Google Adsense now allows you to modify Adsense ad code to meet responsive design requirements. A lot of websites like us have switched...
Read more >
7 Modifications Allowed for Google Adsense Code
1.Modifying adcode for SSL and https enabled pages. · 2. Ad Tag minification (Removing the comment line> · 3 . Parsing the ad...
Read more >
Create and manage deployments | Apps Script
For example, if you create a versioned deployment and then modify your code, the head deployment reflects those changes, while the versioned ...
Read more >
Easy Responsive Ads for Google AdSense
To promote a clean user experience, Google allows modification of the AdWords code (so long as it doesn't artificially inflate clicks or otherwise...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found