krob
krob

Reputation: 1

UserID with Google Tag Manager

Enabled User ID on GA. GTM is setup and using dataLayer to push userId

dataLayer.push({'userId': "fetch logged in user"})

Created DataLayer variable to fetch userId. Created Universal Tag & adding userId field and dimension. Tested userId tag from GTM preview.

But, I don't see userId in User Tracker report on GA. If I replace data layer variable with some constant then I can see those on GA.

Anyone knows what could be an issue?

enter image description here

Upvotes: 0

Views: 2603

Answers (1)

faridghar
faridghar

Reputation: 1515

Are you pushing the userId onto the dataLayer before the GTM container code? If not, then this would explain why it's not being picked up. Your tag has a trigger of All Pages. This means that tag will fire whenever the gtm.js event is pushed onto the dataLayer. So you need to make sure that the userId is available and ready on the dataLayer before the gtm.js event is pushed onto the dataLayer. Since it's the GTM container code that is responsible for pushing the gtm.js event onto the dataLayer, then you need to push the userId onto the dataLayer before the GTM container code executes.

To fix this, your final code should look something like this:

<script>

    window.dataLayer = window.dataLayer || [];
    dataLayer.push({'userId': "fetch logged in user"});

    <!-- Google Tag Manager -->
    (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=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXXXX');
    <!-- End Google Tag Manager -->

</script>

Upvotes: 2

Related Questions