2

I am trying to figure out why injected Adsense units don’t show up on a page when the client id is changed.

What I am Doing I use a script I found on a similar stack overflow question and inject an Adsense ad and script into a website, which already runs Adsense, after page load: dynamic Adsense

var externalScript   = document.createElement("script");
externalScript.type  = "text/javascript";
externalScript.setAttribute('async','async');
externalScript.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
document.getElementsByTagName('body')[0].appendChild(externalScript);

var ins   = document.createElement("ins");
ins.setAttribute('class','adsbygoogle');
ins.setAttribute('style','display:block;');/*add other styles if required*/
ins.setAttribute('data-ad-client','ca-pub-YOUR-CLIENTID');
ins.setAttribute('data-ad-slot','YOUR-SLOTID');
ins.setAttribute('data-ad-format','auto');
document.getElementsByTagName('body')[0].appendChild(ins);

var inlineScript   = document.createElement("script");
inlineScript.type  = "text/javascript";
inlineScript.text  = '(adsbygoogle = window.adsbygoogle || []).push({});'  
document.getElementsByTagName('body')[0].appendChild(inlineScript);

The Problem This script injects ads if I set the client id and ad slot to one that is already on the page. However, if I change the client id or ad slot, the element is objected but no ad is loaded. It should be mentioned that I inject after page load.

My Question Does anyone know why some ads load and some don’t given the parameters I explained earlier?

Please note: I am aware Adsense injection is against the TOS. I do not intent to misuse this method, but am experimenting to discover how it works.

Community
  • 1
  • 1
wilky
  • 33
  • 7

0 Answers0