Sub topic 10
Implementing Events with Google Tag Manager and the Data Layer
This chapter covers sending GA4 events through Google Tag Manager (GTM). It assumes you know what a GTM container, tag, and trigger are, and that you can read a JavaScript object. It also assumes you have already chosen an event name and its parameters, as covered in the earlier chapters on recommended and custom events.
The working model
GTM sits between your page and GA4. Your page does not call gtag('event', ...) directly. Instead, the page pushes a message into the data layer, GTM notices the message, and a GA4 event tag sends the event to Google Analytics.
The data layer is a JavaScript array named window.dataLayer. Each item you push is an object. GTM watches the array and keeps an internal merged state of all values it has seen.
Three GTM building blocks do the work:
- A trigger decides when a tag fires. For data layer events, you use a Custom Event trigger.
- A variable reads a value, such as a data layer value, and makes it available to tags.
- A GA4 event tag sends the event name and its parameters to your GA4 property.
Step 1: Push an event to the data layer
The reserved key event carries the name that a Custom Event trigger matches. Every other key is a value you can read later.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'tutorial_complete',
tutorial_id: 'ga4-events-101'
});The first line makes the code safe to run before GTM has loaded. If the array does not exist yet, it creates it. GTM later picks up everything already in the array.
Note that event: 'tutorial_complete' is the data layer event name. It is not automatically the GA4 event name. You choose the GA4 event name separately in the tag, though keeping the two identical avoids confusion.
Step 2: Create Data Layer Variables
For each value you want to send as an event parameter, create a variable.
- In GTM, open Variables and click New under User-Defined Variables.
- Choose the type Data Layer Variable.
- Set Data Layer Variable Name to
tutorial_id. The name must match the key exactly, including case. - Name the variable something clear, such as
DLV - tutorial_id, and save it.
Dot notation reaches nested values. If you push { ecommerce: { value: 59.98 } }, the variable name ecommerce.value reads the number.
Step 3: Create the Custom Event trigger
- Open Triggers and click New.
- Choose the type Custom Event.
- Set Event name to
tutorial_complete. This is case sensitive. - Leave it firing on All Custom Events, or narrow it with a condition, for example a page path.
Step 4: Create the GA4 event tag
- Open Tags and click New, then choose Google Analytics: GA4 Event.
- Enter your Measurement ID (it looks like
G-XXXXXXXXXX), or select a Google tag or configuration setting if your container uses one. - Set Event Name to
tutorial_complete. - Under Event Parameters, add a row. The parameter name is
tutorial_idand the value is the variable{{DLV - tutorial_id}}. - Attach the Custom Event trigger from Step 3 and save.
The parameter name in the tag is what GA4 receives. The variable is only the source of the value. You can rename the parameter here without touching the page code, which is one of the benefits of using GTM.
Recommended events through GTM
The same pattern works for recommended events. You must still use the exact recommended event name and parameter names, because GA4 recognizes them by spelling. Here is a purchase push that follows the Google Analytics ecommerce structure:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'T_12345',
value: 59.98,
currency: 'USD',
items: [
{
item_id: 'SKU_123',
item_name: 'Trail Shoes',
price: 29.99,
quantity: 2
}
]
}
});The first push sets ecommerce to null. This clears the previous ecommerce object, because GTM merges data layer values and an old items array could otherwise leak into the next event.
In the GA4 event tag for ecommerce events, you can tick Send Ecommerce data and choose Data Layer as the source. GTM then reads the ecommerce object for you, so you do not map each parameter by hand. The tag's event name must still be purchase. For custom events like tutorial_complete, you map parameters manually as shown above.
Choosing the right trigger type
A Custom Event trigger is not the only way to fire a GA4 event tag. Choose by where the signal comes from.
| Signal | Trigger type | When to use it |
|---|---|---|
| Your code pushes `event` to the data layer | Custom Event | Most reliable. Use it for anything your developers can instrument. |
| User clicks a link or button | Click triggers | When you cannot change page code. Fragile if the markup changes. |
| A form is submitted | Form Submission | Works only for standard form submits. AJAX forms often need a data layer push. |
| A page or screen loads | Page View or Window Loaded | For events tied to a specific URL, such as a thank-you page. |
Prefer a data layer push whenever you can. Selectors and click classes break when a designer changes the page. A pushed event name does not.
Common errors
Most problems come from small mismatches. Check these first.
- Tag never fires. The Custom Event name does not match the pushed
eventvalue. Compare them character by character.Tutorial_Completeandtutorial_completeare different. - Parameter arrives empty. The Data Layer Variable name has a typo, or the value was pushed after the event. Values must be in the data layer at the moment the event push is processed, either in the same object or in an earlier push.
- Event fires twice. The same event is sent by both a hardcoded
gtag()call and a GTM tag, or the trigger matches more than once. Use one implementation path per event. - Stale values. GTM keeps merged data layer state. A value pushed on an earlier page of a single-page app can still be readable later. Reset values you do not want to persist by pushing them as
nullorundefined. - Push before the container. Pushing an event before the GTM snippet is fine, but only if the array was created first with
window.dataLayer = window.dataLayer || [];. Replacing the array withwindow.dataLayer = [...]after GTM loads breaks the connection. - Parameters missing in reports. The event appears in GA4, but the parameter cannot be used in reports. This is not a GTM fault. Event parameters must be registered as custom dimensions or metrics in GA4, which a later chapter covers.
Testing before you publish
Edit your tags in the GTM workspace, then click Preview to start Tag Assistant. Open your site in the window it launches and perform the action.
In the Tag Assistant summary, find the tutorial_complete event in the left list. Select it and check two things:
- Under Tags Fired, your GA4 event tag appears.
- Under Data Layer, the push contains the keys and values you expect. The Variables tab shows what each variable resolved to at that moment.
If the tag is under Tags Not Fired, open it to see which trigger condition failed. Only publish the container once the preview behaves correctly. The next chapter covers confirming that the event reaches GA4 itself using DebugView.
Keeping the setup maintainable
A few habits keep a container readable as it grows.
- Use one naming scheme for GTM objects, such as
GA4 - Event - tutorial_complete,CE - tutorial_complete, andDLV - tutorial_id. - Prefer one flexible tag per family of events only when the parameters truly match. Otherwise keep one tag per event so a change affects one thing.
- Use a constant variable or a Google tag setting for the Measurement ID instead of retyping it in each tag.
- Record every pushed event, its parameters, and its trigger in your tracking plan, so developers and analysts agree on what the page should push.
Exercise
A developer pushes { event: 'newsletter_signup_click', placement: 'footer' } to the data layer. In GTM Preview, the push appears under Data Layer, but your GA4 event tag shows under Tags Not Fired. The tag uses a Custom Event trigger with event name Newsletter_Signup_Click. What is wrong, and how do you fix it? Also, how would you send placement to GA4?
Show answer
The trigger's event name does not match the pushed value because Custom Event names are case sensitive. Change the trigger's Event name to newsletter_signup_click. To send placement, create a Data Layer Variable named placement, then add an event parameter named placement with the value {{DLV - placement}} in the GA4 event tag.
Exercise
Why do you push { ecommerce: null } before a purchase push, and what happens if you replace window.dataLayer with a new array after GTM has loaded?
Show answer
GTM merges data layer values, so clearing ecommerce first stops an older object, such as a previous items array, from mixing into the new event. Replacing window.dataLayer after GTM has loaded breaks the connection between the page and GTM, so later pushes are not processed. Always use window.dataLayer.push().