Sub topic 3
Implementing events with Google Tag Manager and the data layer
This chapter assumes you know how GTM containers, tags, triggers, and variables work. It also assumes you have a GA4 property with a web data stream and a GA4 configuration tag that already sends page views.
The data layer is a JavaScript array named dataLayer on the page. Your code pushes objects onto it, and GTM reads them. When a pushed object has an event key, a Custom Event trigger can fire a tag that sends a GA4 event.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'tutorial_complete',
tutorial_id: 'ga4-events-101'
});Set up the GTM side in three steps:
- Create a Data Layer Variable named
tutorial_idthat reads the keytutorial_id. - Create a Custom Event trigger that fires on the event name
tutorial_complete. - Create a GA4 Event tag named
tutorial_complete. Add an event parametertutorial_idwith the value{{tutorial_id}}, attach the trigger, and point the tag at your configuration tag.
Checking events in DebugView and preview mode
Click Preview in GTM and load your page. GTM marks the session as debug, so the event appears in GA4 under Admin, then DebugView, within a few seconds. Confirm that the event name and the tutorial_id parameter match what you pushed.
Registering custom dimensions and metrics
GA4 does not show event parameters in standard reports. To use one, open Admin, then Data display, then Custom definitions. Create a custom dimension with event scope and the parameter name tutorial_id. Register numeric parameters as custom metrics. GA4 does not backfill data, so only events collected after registration appear.
Marking events as key events
Open Admin, then Events. After the first collection, tutorial_complete appears in the list. Turn on Mark as key event. GA4 uses key events as its conversion measure.
Documenting events in a tracking plan
A tracking plan is a shared table of every event you send. Keep one row per event.
| Event name | Type | Parameters | Trigger | Owner |
|---|---|---|---|---|
| tutorial_complete | Custom | tutorial_id | Custom Event trigger from data layer | Web team |
| login | Recommended | method | Login button click | Web team |
| sign_up | Recommended | method | Account creation response | Backend team |
Sending events from a server with the Measurement Protocol
The Measurement Protocol sends events over HTTP from a server, so it works when the browser cannot. Create an API secret under Data streams, then send a POST request with your measurement ID, the secret, and a client_id.
POST https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=API_SECRET
Content-Type: application/json
{
"client_id": "CLIENT_ID",
"events": [
{
"name": "sign_up",
"params": { "method": "email" }
}
]
}Exercise
Exercise
A GA4 Event tag sends tutorial_id correctly, and DebugView shows it, but the parameter is missing from standard reports. What setting did you most likely skip?
Show answer
You did not register a custom dimension. Create one under Admin, then Custom definitions, with event scope and the parameter name tutorial_id. Only data collected after registration appears in reports.