Learn business growth with Google Analytics 4 Forums Google Analytics 4 Optimal placement of parameters for GTM-driven GA4 auto-tracking events

  • Optimal placement of parameters for GTM-driven GA4 auto-tracking events

    Posted by Lucas on 31 May 2022 at 6:42 am

    Hey folks! Just wanted to get your thoughts on how and where to mix in parameters with the GTM dataLayer model with the GA4 JS events (you know, the ones listed out here). Let’s say we’re talking about a ‘video_start’ event: am I right in thinking I can just push that event right onto the Data Layer and we’ll be able to start automatically recording from there? Like so: dataLayer.push({event:'video_start'});?

    But here’s where I keep hitting a wall: where do the parameter values come into play? I mean, can we just add them to the top layer of the Data Layer? Could it possibly look like this:

    dataLayer.push({event:'video_start', video_title:'my title', video_url:'my-url'});

    The thing is, I really want to keep the Data Layer clean and not cluttered. My ideal solution would be to squeeze those parameter values into one single property in the Data Layer. Kind of like this:

    dataLayer.push({event:'video_start', video_params:{title:'my title', url:'my-url'}});

    And then, get the GA4 GTM tags to correctly map the data layer variables to the corresponding GA4 parameters for these events. I mean, I could stop the automatic event tracking and do everything manually, but for now let’s stick to the automated route so we understand how the GTM GA4 tags work.
    I can’t seem to find any official info (i.e. from Google themselves) about GA4-via-GTM detailing how events and parameters are handled through the Data Layer by the GA4 GTM tags. Do you maybe know how these mappings are managed in those ‘by default’ situations?

    Jayden replied 10 months, 3 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • Isaiah

    Member
    25 April 2023 at 11:17 am

    Alright, let’s crack this.

    So, in your case, you got two options. First, you can use Google’s built-in listener for GA4 analytics. This will automatically capture and send events like a ‘video_start’ to GA4 right when they happen (neat, huh?). But one downside with auto-mode is that you can’t customize the events or add any specific details.

    Second option, and where we’re heading now, is you doing your own magic (like a true wizard!). So, yes, your own events and parameters can be sent to GA4 via GTM. This is where Data Layer plays a super useful role – as kind of a handyman, keeping all your request data in one place for GTM to use.

    Just push info right onto the Data Layer like this thinking: “Hey Data Layer, hold this for me”:

    `javascript
    dataLayer.push({
    event:’video_start’,
    video_params:{
    title:’my title’,
    url:’my-url’
    }
    });
    `

    Next, hop onto GTM and create some Data Layer Variables for your data to stop by before it heads to GA4:

    [Data Layer Variable Image](https://i.stack.imgur.com/V0oOA.png)

    [Data Layer Variable Image](https://i.stack.imgur.com/nQA1i.png)

    Now, we’re in the homestretch! Just set up your GA4 tag like this to send your data along. Be sure to match your parameters with your newly-created GTM variables.

    [GA4 Tag Example](https://i.stack.imgur.com/hIQ7B.png)

    And there you go, pal! Happy tracking!

  • Jayden

    Member
    28 June 2023 at 9:34 am

    Yes, you’re on the right track. As far as I know you’ve got it correct, GA4 listens for the specific properties (like video_title, video_url) on the Data Layer if it corresponds to the GA4 event schema.

    In terms of your desired structure, you can include the additional parameters in a nested fashion like you’ve given in your example: dataLayer.push({event:'video_start', video_params:{title:'my title', url:'my-url'}});. However, keep in mind that while this keeps your Data Layer tidy, it adds an extra step in GTM where you need to create separate Data Layer Variables for each nested parameter.

    The mapping of GA4 event parameters to Data Layer values is quite straightforward: the name of the ‘parameter’ in GA4 should match the name of the key in the Data Layer object. If you do automatic tracking, Google has certain predefined parameters it looks for (like page_location, page_referrer). Anything else not defined by Google will have to be manually assigned. Unfortunately, official documents from Google on this specific question seem lacking, and it can result in some trial and error.

Log in to reply.