How to create a custom-event trigger in Google Tag Manager
Introduction
Google Tag Manager (GTM) supports custom triggers that fire tags in response to specific website events. Custom events are useful when standard click or form-submission tracking is not enough and tracking needs to follow JavaScript events on the website.
For example, custom events can track specific button clicks, video views, scroll depth or other website actions.
Steps to set up a custom-event trigger
Step 1: Send a custom event from JavaScript
To use a custom event, make sure your website sends it through dataLayer.push.
Example JavaScript that sends a custom event:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'customEventName', // Custom event name
'category': 'Button Click',
'action': 'Click',
'label': 'Download PDF'
});
Important: customEventName is the event name to use in the trigger settings.
Step 2: Create a custom-event trigger in GTM
- Open the tab "Triggers" and click "New".
- Select "Custom Event".
- Under Event name, enter the event name sent by JavaScript, for example:
customEventName). - Choose whether the trigger should fire on "All Custom Events" (all events) or "Some Custom Events" (selected events only).
- For selected events, add a condition such as:
- Event name equals
customEventName - Category contains
Button Click
- Event name equals
- Click "Save".
Step 3: Create a tag that uses the trigger
- Open the tab "Tags" and click "New".
- Choose the tag type, for example:
- Google Analytics: GA4 Event to send data to Google Analytics.
- Facebook Pixel – Custom Event for Facebook tracking.
- For Google Analytics 4, configure the event as follows:
- Event Name:
{{Event}} - Event Parameters: Add the data sent through the
dataLayer, such ascategory,action, andlabel.
- Event Name:
- Under Triggering, select the trigger you created in the previous step.
- Click "Save".
Step 4: Test and publish
- Click "Preview" to check that the trigger works correctly.
- Trigger the event on your website, for example by clicking the button that runs the
dataLayer.push). - In GTM debug mode, check whether the event appears under "Events".
- If everything works correctly, click "Submit" and then "Publish" to apply the change.
What if the event does not fire?
Check the data layer: Open the Console in Chrome DevTools and enter:
console.log(window.dataLayer);
Check whether your event appears in the list.
Check the trigger in GTM Preview: Make sure the event appears and the data-layer values are sent correctly.
Check that the JavaScript runs correctly: Check that the code runs at the intended moment, such as when a button is clicked.
