This article introduces the use of dataLayer.push method for event tracking, this is how e-commerce tracking is used, read more: Google Analytics 4: Google Tag Manager Ecommerce Setup Guide
The principle of dataLayer.push is very simple. Once a specific event is triggered, it passes dataLayer.push pushes the event to the data layer, then uses the custom event as the trigger condition in the Trigger in GTM, and then uses a Tags to configure the event.
Suppose now we want to do dataLayer.push tracking on this location:
Step 1 : DE add dataLayer.push
The source code for tracking location is:
<a>dataLayer.push demo test</a>
DE needs to add dataLayer.push as:
dataLayer.push = ({
'event': 'test',
'pageType': 'product'
});
The effect after adding it to the required position is:
<a onclick="dataLayer.push({'event': 'test','pageType': 'product'
});">dataLayer.push demo test</a>
As long as the location is clicked, dataLayer.push is executed.
Step 2 : Set Up the Variable
In GTM, click「 Variables」——「New」——「Choose a variable type to begin setup…」——「Data Layer Variable」, name it “dlv-Page Type”, and then set as follows:
Step 3 : Set Up the Trigger
In GTM , click「Triggers」——「New」——「Choose a trigger type to begin setup…」——「Custom Event」,Name it “dataLayer.push demo”, and make the following settings:
Whatever the value of the event in dataLayer.push is, fill in the event name.
Step 4 : Set Up the Tags
In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Google Analytics: GA4 Event」,Name it “GA4-Event-dataLayer.push”, and make the following settings:
Step 5 : Preview and Publish
Next is the Preview test:
Tags is triggered, click it to open and check whether the value of the event parameter page_type is accurate:
The event parameters are accurate, and can be published.
Read More:
Step 6 : Custom definitions
The next step is to register event parameters page_type, if you want to use it in GA4, you need to register it in the Custom definitions of GA4.
In GA4,click「Admin」——「Custom definitions」——「Create custom dimension」, then do the following configuration:
Step 7 : Verify Data in Reports
Generally, after 24 hours, we can see the data in GA4, such as:
Read More:
- GA4 Event Tracking Series ⓪ —— Rules and Limits for GA4 Events
- GA4 Event Tracking Series ① —— Automatically Collected Events
- GA4 Event Tracking Series ② —— Enhanced Measurement Events
- GA4 Event Tracking Series ③ —— Recommended Events
- GA4 Event Tracking Series ④ —— Custom Events (gtag Method)
- GA4 Event Tracking Series ⑤ —— Custom Events (Attributes Method)
- GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)
- GA4 Event Tracking Series ⑧ —— Custom Events (jQuery Method)
- GA4 Event Tracking Series ⑨ —— Custom Events (ga-data Method)
- GA4 Event Tracking Series ⑩ —— Custom Events (dataLayer.push Method)
- GA4 Event Tracking Series ⑪ —— Create Event(Codeless Event Tracking)
- GA4 Event Tracking Series ⑫ —— Modify Event