Sub topic 8
Managing Tags with a Tag Manager and Data Layer
This chapter assumes you know basic HTML, how scripts load in a web page, URL query strings, social post or ad creation, and basic JavaScript. You also need a Google Analytics 4 property and a tag manager container, such as Google Tag Manager (GTM).
A tag manager is a tool that controls tracking scripts from one interface. A tag is a configured script or measurement request, such as a GA4 event or an advertising pixel. A trigger decides when a tag runs. A variable supplies a changing value, such as a form ID or page URL.
How the data layer connects your site to tags
The data layer is a JavaScript array that stores structured information about actions on your site. Your page code pushes an event into it. GTM listens for that event and activates the matching tags.
Keep the event name stable and put changing details in parameters. This separates your website's business events from the tools that receive them.
window.dataLayer = window.dataLayer || [];
function trackNewsletterSignup() {
window.dataLayer.push({
event: 'generate_lead',
form_id: 'newsletter_footer',
lead_type: 'newsletter'
});
}
document
.querySelector('#newsletter-form')
.addEventListener('submit', trackNewsletterSignup);The form submission should be validated before this function runs. Otherwise, you may count failed submissions as leads.
Configure the tag manager
In GTM, create a Custom Event trigger with the event name generate_lead. Then create a GA4 Event tag with these settings:
- Select your existing GA4 configuration tag.
- Set the event name to
generate_lead. - Add
form_idandlead_typeas event parameters. - Attach the Custom Event trigger.
- Submit and publish the container only after testing.
The tag manager receives the values from the data layer. For example, create a Data Layer Variable named form_id, then use it as the value for the GA4 parameter named form_id.
| Layer | Example | Purpose |
|---|---|---|
| Event name | `generate_lead` | Selects the trigger and GA4 event |
| Data layer value | `newsletter_footer` | Identifies the form |
| GA4 parameter | `form_id` | Makes the value available in reports |
Use names that describe business actions rather than interface details. generate_lead is more useful than button_7_clicked, because the same action might later use a different button.
Consent and testing
Consent settings must apply before nonessential tags run. Configure your consent banner and GTM consent checks so analytics or advertising tags wait for the required permission. A data-layer event can still describe an action, but a tag should not send it when the visitor has not granted the necessary consent.
Use GTM Preview mode to test the complete path:
- Open Preview and connect it to the test page.
- Perform the action, such as submitting the form.
- Select the
generate_leadevent in the event timeline. - Check the data layer for the expected values.
- Check that the GA4 tag fired once.
- In GA4 DebugView, confirm the event and parameters.
- Test validation failures, repeated clicks, and denied consent.
After publishing, compare event counts with completed records in your application or form provider. A working tag can still report incorrect data if the event fires at the wrong point.
Exercise
A generate_lead event appears twice in GA4 after one form submission. What should you inspect first?
Show answer
Inspect whether both page code and a GTM tag send the event, and check whether the form listener or trigger fires more than once.