Track your user ID using Google Tag Manager

I am new to the world of Google Analytics and Tag Manager. I am trying to add user id to ga events. For example, I would like to add a user ID in case of a Google analytic event.

What is the best practice for storing userId on the client side?

  • In cookie?
  • On pages with meta tags?
  • Or something else?
+4
source share
5 answers

you need to pass it as a dataLayer variable and add a macro to pass it to GA by setting additional fields in UA UA code: https://support.google.com/tagmanager/answer/4565987

, , dataLayer ( javascript) GTM.

, GTM, , dataLayer cookie , js, - dataLayer.

+3

...

. GoogleTagManager + Univarsal GAnalytics + ... dataLayer .

1) dataLayer GTM ( Rails application.html/haml):

<script>
    dataLayer = [];
</script>

2), :

<script>
    dataLayer.push({
      "&uid": "#{YOUR-BACKEND-USER-ID}"
    });
</script>

3) GTM

(function (w, d, s, l, i) {
    w[l] = w[l] || [];
    w[l].push({
        'gtm.start': new Date().getTime(),
        event: 'gtm.js'
    });
    var f = d.getElementsByTagName(s)[0],
        j = d.createElement(s),
        dl = l != 'dataLayer' ? '&l=' + l : '';
    j.async = true;
    j.src =
        '//www.googletagmanager.com/gtm.js?id=' + i + dl;
    f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-XXXXX');

4) "uid" . Layer Variable Name & uid

5) ( ). > : & uid | : {{uid}}

6) Google Analytics USER-ID. admin > account > property > tracking info > USER-ID CREATE

+2

.

dataLayer = [{
    'userId': 'abcdefg123456'
}]

, , {{userId}} .

+1

, , . JS, cookie ( ) - GTM cookie ( HTML5 localstorage thingamajig, ).

id id , ( Blexys) datalayer GTM:

dataLayer = [{
    'userID': '12345',    
}];

dataLayer:

dataLayer.push({'userID': '123456'});

datalayer, . , , , .

, , , , cookie ( , )

0

cookie. cookie . GTM cookie .

If you use a cookie, you need to set up the first cookie in GTM. If the data layer you need to configure the variable macro data level in GTM.

0
source

All Articles