Sub topic 10
Implementing Events with Google Tag Manager and the Data Layer
This chapter assumes you understand GA4 events and parameters, and that you have basic familiarity with GTM containers, tags, triggers, and variables. You also need a published GTM web container connected to your website and a GA4 configuration tag that sends data to your property.
The data layer and custom events
The data layer is a JavaScript array that carries information from your website to Google Tag Manager. Your application pushes an object into it when an interaction occurs. The event property tells GTM that something happened.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'tutorial_complete',
tutorial_id: 'ga4-events-101'
});Here, tutorial_complete is the GTM event that activates a trigger. tutorial_id is additional context that you can send to GA4 as an event parameter.
The name in the data layer does not automatically send anything to GA4. GTM must have a trigger and a GA4 event tag that read the data.
Create the GTM components
Create these components in your GTM web container:
| Component | Configuration | Purpose |
|---|---|---|
| Data Layer Variable | `tutorial_id` | Reads the tutorial identifier |
| Custom Event trigger | `tutorial_complete` | Detects the data layer push |
| GA4 Event tag | Event name `tutorial_complete` | Sends the event to GA4 |
Create the data layer variable
In GTM, open Variables, select New, and choose Data Layer Variable. Set the Data Layer Variable Name to tutorial_id. Name the GTM variable something recognizable, such as DLV - tutorial_id.
The variable reads the value from the latest matching data layer event. In this example, it returns ga4-events-101.
Create the custom event trigger
Open Triggers, select New, and choose Custom Event. Set the event name to tutorial_complete. Keep the trigger configured to fire on all custom events with that name.
The trigger listens for the event property in the data layer. It does not listen for the tutorial_id value.
Create the GA4 event tag
Open Tags, select New, and choose Google Analytics: GA4 Event. Select your existing GA4 configuration tag or Google tag. Set the event name to tutorial_complete.
Add an event parameter:
- Parameter name:
tutorial_id - Value:
{{DLV - tutorial_id}}
Attach the custom event trigger to this tag. When the trigger fires, GTM substitutes the data layer value and sends both the event and its parameter to GA4.
Understand the event flow
Test before publishing
Select Preview in GTM and enter your website URL. Perform the interaction that runs the dataLayer.push() call. In Tag Assistant, select the tutorial_complete event and confirm that:
- The custom event trigger fired.
DLV - tutorial_idcontains the expected value.- The GA4 Event tag fired once.
- The tag has the expected event name and parameter.
Then open GA4 DebugView. Select the event and inspect its parameters. DebugView can take a short time to display data. Publish the container only after the preview and GA4 checks succeed.
Exercise
A data layer push contains event: 'tutorial_complete' and tutorial_id: 'lesson-7', but the GA4 tag fires without the parameter. What should you check first?
Show answer
Check that the Data Layer Variable uses the exact name tutorial_id and that the GA4 tag uses that variable as its parameter value.