0
votes

In dataLayer i have my userId with his correct value but in my GA variable the value is undefined.

I'm trying to push to dataLayer the username (userId) inside of my login function (an angular project). I put my GTM code in the index.html page and my dataLayer.push() is in my login function (after the GTM code is run)

this is my push() code in the login function

window['dataLayer'] = window['dataLayer'] || [];
window['dataLayer'].push({
      'username' : '1234567'  
})

The dataLayer contain the current value but the GA tag not. (undefined)

1
Do you run your GA tag with the same event? Or is there a chance, that the GA tag is already run, when your dataLayer.push is executed? - kgrg
Not sure that i understand your question. how can i check it? - lironv
You can use GTM preview mode to check the actual value of a given variable during any GTM event. You can also check, what tags were fired at these points. E.g. if you fire your GA tag during Page View, but push the data after GTM load (as you do), the data will not be available for earlier events and tags. - kgrg
thank you for the quick answer! I am new with GTM and GA so i'm not sure on understanding your answer. In the GTM preview (after i press the login button and push username to dataLayer), the situation is like this: --if i push 'event' : 'login' so in the login category i actully can see the vulue of the signin username. --if i push the username without event so the variable is undefined at all tabs (pageview, dom ready..). does it is a good situation? - lironv

1 Answers

0
votes

I will tell you how to configure GTM and GA for an event. Follow the below steps, and maybe you can modify to your own particular requirement.

  1. First create variables which you are going to use in your datalayer. ex -
 window.dataLayer = window.dataLayer || [];
    window.addEventListener('scroll', function (event) {
      window.dataLayer.push({ 
        'event': 'scroll',//event name configured in trigger 
        'username': '123456', //configured as variable in Tags

      });
    });

The above example will send data to GA every time you scroll the page.You can create your event listener based on your requirements. So in above example create a variable named username.Click on New in User Defined variables,click on variable configuration and select data layer variable.

  1. Click on Triggers and create an custom event,the event name should be same as event in your datalayer.
  2. Create a Tag Google Analytics - Universal Analytics with trigger filtering pointing to the previous trigger you created.While configuring GA fields you can use previously created variables which will be visible like this-{{username}}.

You can test this in preview mode and see which triggers are fired and what is the datalayer that is pushed to google analytics. In GA to see live data of your debugging, go to Real Time -> Events