Google gtag in DTM

vipinvijayan10

20-12-2018

Hello Everyone!

Just wanted to know how do you guys deploy the Google gtag via DTM to collect the audiences for remarketing lists in DV360 or Google Ads?

As instructed by stewarts16448458​ on this thread, I deployed the gtag as a page top rule (Sequential HTML), however the Remarketing lists within the Google Ads  UI weren't populating. When I checked with the Google Ads Support team, they got back to me saying that the setup isn't right as they want the gtag to be injected directly on the page (i.e. not in an iframe and not run server-side).

So I tried converting the gtag to JS as shown below and it looks it is working. However I'd love to know if you know of any easier and efficient way of doing this.

var dcJS = document.createElement("SCRIPT");

var done = false;

dcJS.setAttribute("src", "//www.googletagmanager.com/gtag/js?id=AW-XXXXXXXX");

dcJS.setAttribute("type", "text/javascript");

document.body.appendChild(dcJS);

dcJS.onload = dcJS.onreadystatechange = function() {

  if (

    !done &&

    (!this.readyState ||

      this.readyState === "loaded" ||

      this.readyState === "complete")

  ) {

    done = true;

    callback();

    // Handle memory leak in IE

    dcJS.onload = dcJS.onreadystatechange = null;

    document.body.removeChild(dcJS);

  }

};

function callback() {

  if (done) {

    /* <![CDATA[ */

    window.dataLayer = window.dataLayer || [];

    function gtag() {

      dataLayer.push(arguments);

    }

    gtag("js", new Date());

    gtag("config", "AW-XXXXXXXXX");

  }

  //]]>

}

Accepted Solutions (0)

Answers (2)

Answers (2)

StewSchilling

MVP

04-01-2019

Have you had a chance to check on this?  If my suggested solution did not work, I really would like to understand what went wrong.

-Stew

StewSchilling

MVP

20-12-2018

Are you absolutely sure that you deployed it as Sequential HTML?

What you describe (loading in an iframe) would indicate that it had been deployed as Non-Sequential HTML.