Vadorequest
Vadorequest

Reputation: 17997

Clarifications how to use Google Analytics through Google Tag Manager

Anyone with a bit of experience would be kind to clarify the GA/GA-old/GTM mix for me?

I am using GTM (Google Tag Manager) and I basically inject this in the DOM on page load (I got this script from GTM):

const scriptTag = `
    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
      new Date().getTime(),event:'gtm.js',${JSON.stringify(events).slice(1, -1)}});
      var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
      j.async=true;j.src='${protocol}://www.googletagmanager.com/gtm.js?id='+i+dl+'${gtm_auth}${gtm_preview}&gtm_cookies_win=x';
      f.parentNode.insertBefore(j,f);
    })(window,document,'script','${dataLayerName}','${id}');`;

I have set up a "Universal Google Analytics" on my GTM, and I have ga function available in the browser console. I have successfully used the dataLayer feature of GTM and I can see my dataLayer get filled when using the preview GTM mode.

But, I don't understand how to link the variables I store in the dataLayer to GA, I save a env variable in my dataLayer, and I have added a "data layer" variable env on GTM, but I feel like I missed a step to link that variable to GA. (None of the variables I save in my dataLayer are stored into GA)

I also heard about the "new GA way", which relies on gtag... But I don't have a gtag function created by the "google analytics" script injected by GTM.

I'm getting lost in what is supposed to be the way of doing things.

Old way: https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications

New way: https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications

(I'm building a SPA)

So:

  1. How to link GTM dataLayer and events data to GA?
  2. Should I use gtag instead of ga? I don't understand why GTM doesn't include the new version of GA with gtag

Upvotes: 0

Views: 599

Answers (1)

Brian Norman
Brian Norman

Reputation: 564

Do you have a Google Analytics Settings variable set up that your are setting on your Universal Analytics tags?

My settings variable looks like this:

GA Settings Variable

The tracking ID variable set in the image above looks like this:

Tracking ID Variable

Then, in my Universal Analytics event tag, I set the settings variable in the tag configuration: enter image description here

Upvotes: 1

Related Questions