Learn business growth with Google Analytics 4 Forums Google Analytics 4 Implementing User IDs in GTM and GA4

  • Implementing User IDs in GTM and GA4

    Posted by Henry on 15 March 2023 at 5:51 pm

    Hey there, I’ve been dabbling with GTM and GA4 recently, and in one of the lessons, I stumbled upon a nifty little thing called dataLayer, which you push to collect userId in GTM. I’ve got the gist of it but when the tutor mentioned swapping out the default userId with the actual one (highlighted in the tutorial), I was left scratching my head. I’m still new on this journey, further away from being a coder still!

    I do know how to tweak the code in my header.php file and place it properly above the GTM code. Yet, I’m stumped on how to make it work so it pulls a new userId every time a user logs in. Got any ideas on this one? Remember, I’m just starting out here, so a simple explanation from my fellow developers on how I can ace this without a developer would be much appreciated. Cheers!

    window.dataLayer = window.dataLayer || [];
      'event' : 'login',
      '**userId' : '1234567' //Supposedly this number needs to be swapped with an actionable User ID
    Abdul replied 10 months, 3 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • Amelia

    4 June 2023 at 7:41 am

    To dynamically change the userId with the actual user Id in the dataLayer, you need to use a PHP function to fetch the user ID and then echo it within the JavaScript snippet. Also, keep in mind that the user ID is usually stored as part of user authentication in a session variable or a cookie in PHP.

    Hence, assuming you authenticate users with something like WordPress, the code would look something like this:

    window.dataLayer = window.dataLayer || [];
    ‘event’ : ‘login’,
    ‘userId’: ”
    Here, get_current_user_id(); is a WordPress function to get the currently logged in user’s ID. Also remember this code must be executed after the user logs in, potentially you might want to check if the user is already logged in before pushing the data to the dataLayer.

    However, if you’re not using WordPress or any other CMS with a built-in function to get the user ID, you would need to fetch it from the session variable or cookie where the user information is stored. For instance, it might look something like $_SESSION['userId']. It’s also worth noting that the specifics of how you fetch the userID and where you put this script might vary considerably depending on your website’s specifics.

  • Abdul

    15 June 2023 at 1:43 pm

    To get a new userId everytime a user logs in, you need to replace the ‘1234567’ in your code with a variable that retrieves the userId from your system. This will differ based on your setup, but it’s often something like ‘window.user.id’ or ‘session.user.id’, where ‘user’ is the logged in user data and ‘id’ is their unique identifier. Basically, this variable should point to where your system keeps track of the logged-in user’s ID. Once this is setup, everytime a user logs in, the correct ID will be sent to Google Tag Manager.

Log in to reply.