Ozge Cokyasar
Ozge Cokyasar

Reputation: 313

How can I access GPT data with pure javascript on browser console?

So I'm working on a website which renders ad banners on certain pages. How can I get the list of all the possible ad banners to render with pure javascript on the browser console? Currently the only way I am able to access this data is via the Google Publisher Console which is a popup.

There is the 'googletag' method which when I run in the console, gives me an object but I am unable to find the data for all the possible slots.

I'd like to be able to access the Ad Slots and programatically choose what to render.

Thanks.

Upvotes: 0

Views: 1255

Answers (2)

Ozge Cokyasar
Ozge Cokyasar

Reputation: 313

This function I found will render all the page specific GPT data: https://gist.github.com/rdillmanCN/a70ec955d9a982127fefadabe8b898b5

Upvotes: 0

rabsom
rabsom

Reputation: 859

To get the list of all slots on the page, you can use the following googletag.pubads().getSlots().

To list all defined slot ids and adpath :

  var adslots = googletag.pubads().getSlots();
  //loop through the GPT adslots
    for (i=0;i<adslots.length;i++) {
    //get slot defined div ID
    console.group('slodId : '+ adslots[i].getSlotId().getDomId());
      //get slot defined path
      console.log('slotPath : '+ adslots[i].getAdUnitPath());
    console.groupEnd();
  }

Keep in mind you don't have to handle the rendering yourself. Google Ad Manager will generate the ad iframes itself. For specific renderings, use custom creative templates or native ad templates.

Upvotes: 1

Related Questions