Option to Disable Adsense Ads

0
481

In some cases it is beneficial to provide a link or option for visitors to a site not to see the adsense ads the first time they visit a page. Ads can sometimes be viewed negatively and for the first visit or under certain circumstances it is best to temporarily disable them. Here is the code so that you can just add ?noads=1 to the end of your url and the ads wont be displayed so you can use the following links:

http://example.com/mypage.htm    = ads are shown
http://example.com/mypage.htm?noads=1    = ads are not shown

The following is a standard adsense flex unit.

<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
<ins class=”adsbygoogle”
style=”display:block”
data-ad-client=”ca-pub-7xxxxxxxxxxxx1″
data-ad-slot=”5xxxxxxxxxx1″
data-ad-format=”auto”></ins>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Now we are going to take the second to last line starting (adsbygoogle = and put some aditional code above and below it.

<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
<ins class=”adsbygoogle”
style=”display:block”
data-ad-client=”ca-pub-7xxxxxxxxxxxx1″
data-ad-slot=”5xxxxxxxxxx1″
data-ad-format=”auto”></ins>
<script>
function getQueryParams(qs) {
qs = qs.split(“+”).join(” “);
var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) { params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); }
return params;
}
var get = getQueryParams(document.location.search);
if (typeof get.noads == ‘undefined’) {
 (adsbygoogle = window.adsbygoogle || []).push({});
}
</script>

 

To explain what is going on here we have a function to collect any data in the URL called getQueryParams. We use this to load anything in the URL into the variable get. We then use an if statement to decide whether to push the ad or not.

if (typeof get.noads == ‘undefined’) {

You can change the noads to whatever you want. So you could use an existing variable like:

if (typeof get.members == ‘undefined’) {
http://example.com/mypage.htm?members=John

And the ads will not be shown.

Another useful method of using this same function is if you have a particular traffic source that you don’t want to show ads to. Lets look at a example using google analytics tracking i.e.

if (get.utm_campaign !== ‘adwords1’) {

Notice how we’ve changed the if statement to an if not by adding a punction mark ! So only show ads if the google analytics link utm_campaign does not equal adwords1.

There’s other things you can do with this too it doesn’t have to be just showing ads. You can set up different pages for compliance for example. Don’t autoplay the video on this traffic source or don’t use sound on that traffic source.

A really simple split test could be to show this version of the landing page with a certain link if the url contains lander=1

http://example.com/mypage.htm?lander=1
if (get.lander == ‘1’) {
document.write(‘hello world’);
}

Bring this back to adsense and the original topic for the post you can use this across all the ads on a page. Simply load the function in the head part of the page and then use the if statement for all the push lines:

if (typeof get.noads == ‘undefined’) {
(adsbygoogle = window.adsbygoogle || []).push({});
}

There is no need to repeat all the code.

If you don’t want to change all the links to a site but want to disable ads for some visitors you could store a cookie with a set duration and then check for it in the if statement. This would mean you wouldn’t have to change any links and the visitors wouldn’t see any ads until the cookie expires.

LEAVE A REPLY

Please enter your comment!
Please enter your name here