This article introduces how to use ga-data method to do custom event tracking.
Meet ga-data method
The ga-data method is to add a ga-data attribute to the location that needs to be tracked to set the trigger condition. This method is suitable for batch event tracking.
- Advantage:Batch event tracking with high efficiency.
- Disadvantage:Need development support, add a ga-data attribute
- Limit:No limit
- Event Rules:
Event = Event name + Event parameter
Length of event name is up to 40 characters.
Event parameters per event is up to 25, since there are 5 default event parameters, actually 20 can be set.
Event parameters are divided into Event Scope Custom Dimension and Event Scope Custom Metrics.
Event Scope Custom Dimension is up to 40 characters.
Event Scope Custom Metrics is up to 100 characters.
Event tracking example: ga-data method
Suppose I want to use the ga-data method to track events at this location:
add ga-data
Return to the location that needs to be tracked, and view the source code of the tracked location, the source code is:
After adding the ga-data:
Variables
In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Auto-Event Variable」,Name it “element”, and make the following settings:
In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Custom JavaScript」,Name it “GetGaData”, and make the following settings:
Code :
function() { var elem = {{element}}, attr = "ga-data", // change this to the attribute that you want to get result = (elem.getAttribute && elem.getAttribute(attr)) || null; if( !result ) { var attrs = elem.attributes, l = attrs.length; for(var i = 0; i < l; i++) { if(attrs[i].nodeName === attr) result = attrs[i].nodeValue; } } return result; }
In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Lookup Table」,Name it “Lookup Table ga-data”, and make the following settings:
Triggers
In GTM , click「Triggers」——「New」——「Choose a trigger type to begin setup…」——「Click – All Elements」,Name it “ga-data trigger”, and make the following settings:
ga-data is less than or equal to 103, greater than or equal to 101 are triggered.
Tags
In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Google Analytics: GA4 Event」,Name it “Event Tracking-ga-data(ga-data method)”, and make the following settings:
Preview
Next is the Preview test, you can use any of the following methods:
Click to open it:
The event fires as expected and can be published.
Custom definitions
The next step is to register event parameters position, 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:
The event tracking of the ga-data method is complete.