Skip to content

Instantly share code, notes, and snippets.

Created July 2, 2019 09:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qasimalyas/4d93b21352aad30f3d04e0e4cc8543bc to your computer and use it in GitHub Desktop.
Save qasimalyas/4d93b21352aad30f3d04e0e4cc8543bc to your computer and use it in GitHub Desktop.


  • Criteria
  • Data layer
  • Targeting
  • Google Ad Manager set-up
  • Testing
  • Debugging
  • Demo
  • Documentation


  1. Target using information from data layer
  2. Target by navcode, storycode, url anything else
  3. Use custom functions to get data and use googletag setTargeting method to pass to GAM

Data layer

We output the dataLayer object which contains the data needed from stories / navigation, logged in / out. This is then picked up by various functions and then passed down to GAM for targeting.


  • Two-step
    • data has to come from site and passed to GAM
    • GAM matches line-item with criteria and serves ad-unit


In WebVision CMS add the below functions to Global_JavaScript Head page text.

  function getDataLayerVal(keyVal) {
    for (var i = 0; i < window.dataLayer.length; i++) {
      if (dataLayer[i][keyVal] !== undefined) {
        return dataLayer[i][keyVal];
  function getFullNavPath() {
    if(getArrayVal('FullNavPath') !== undefined) {
    	getArrayVal('FullNavPath').split('|').join(' ').trim();
  function isMicrosite() {
    return document.body.classList.contains('microsite')

Targeting can be done by calling getDataLayerVal function and passing a value of what to look for in the data layer e.g. getDataLayerVal('LoggedIn') which will return either 'True' or 'False'. This data can then be used in GAM to target specific line-items.

The same process can be used for other items needing to be targeted with the data layer.

By SPIN (section)

googletag.pubads().setTargeting('navcode', getDataLayerVal('NavCode'));

The data passed to GAM will be the type of stringand will be numerical e.g. "13609"

By Article (story) ID

googletag.pubads().setTargeting('storycode', getDataLayerVal('StoryID'));

If you are on a story page the data passed to GAM will be the type of stringand will be numerical e.g. "1003404". Otherwise, the value will be undefined.

By slug

googletag.pubads().setTargeting('slug', document.location.pathname.substr(document.location.pathname.lastIndexOf('/')));

This will return a string that will be the value from the last forward slash from the url e.g. "/". This is useful for non-spin and non-article related pages for example, biography and contacts.

By section

googletag.pubads().setTargeting('FullNavPath', getFullNavPath());

This requires a custom function to pass the value in a transformed way that Ad manager can then consume. GAM should then use this with a match filter using the tilde symbol ~.

Is microsite?

googletag.pubads().setTargeting('isMicrosite', isMicrosite());

This function checks and returns a Boolean (true or false) if the page being viewed is a microsite. This can be used in combination with navcode or event fullnavpath to apply targeting.



  • Ad-unit
  • Line-items
  • Creatives
  • Creative template

Once targeting has been set we need to create a new key in GAM, collect the value passed i.e. 'True' or 'False' and add that as a targeting criteria to the line-item


Often you may need to debug why some targeting insn't working. For that we can use the debugger. Using this will allow you to see why a certain line-tem that you've targeted ins't working.


You will only need to follow the below steps if a new targeting criteria is going to be set-up. Often the default NavCode, StoryID and LoggedIn are more than sufficient to target articles / spin pages in GAM

  1. create a new function to pass the information to googletag
  2. use setTargeting to pass the data to GAM with a key
  3. in GAM create a new key that matches in step 2 above
  4. add the key to the line-item using the targeting criteria


  1. Help center
  2. Jira
  3. How ad selection works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment