This article introduces how to use attributes method to do custom event tracking.
Meet attributes method
The attributes method is to use some attributes, such as class, id, text, url, etc., to locate user trigger conditions.
For example, if there is a unique class at the clicked position, then this class can be used to set the trigger condition.
If it is not unique, it can be combined with other attributes, such as class and text, as a trigger condition.
- Advantage:Easy to set up, accurate tracking, and the way I recommend it.
- Disadvantage:No.
- 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: attributes method
Suppose I want to use the attributes method to track events at this location:
Variables
Since we are doing click event tracking, we need to enable some Built-In Variables.
If you have already enabled it, you can ignore this step.
In GTM , click「Variables」——「Configure」,Check these Built-In Variables of click, and exit:
- Click Element: Accesses the
gtm.element
key in the dataLayer, which is set by Click triggers. This will be a reference to the DOM element where the click occurred. - Click Classes: Accesses the
gtm.elementClasses
key in the dataLayer, which is set by Click triggers. This will be the string value of the classes attribute on the DOM element that was clicked. - Click ID: Accesses the
gtm.elementId
key in the dataLayer, which is set by Click triggers. This will be the string value of the id attribute on the DOM element that was clicked. - Click Target: Accesses the
gtm.elementTarget
key in the dataLayer, which is set by Click triggers. - Click URL: Accesses the
gtm.elementUrl
key in the dataLayer, which is set by Click triggers. - Click Text: Accesses the
gtm.elementText
key in the dataLayer, which is set by Click triggers.
The positions of these Built-In Variables in the source code are as follows:
Click ID is ad.
Click Classes is de.
Trigger
Return to the location that needs to be tracked, and view the source code of the tracked location:
We can know that the Click URL is https://www.bbccss.com/contact-me, and the Click Text is Contract Me. You can use these two to locate the trigger conditions.
In GTM , click「Triggers」——「New」——「Choose a trigger type to begin setup…」——「Click – All Elements」,Name it “Contract Me”, and make the following settings:
Note: If Click URL or Click Text is unique, then you only need to use one; what I demonstrate here is to use two, and two can ensure uniqueness.
Tags
In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Google Analytics: GA4 Event」,Name it “Event Tracking-Contract Me (attributes method)”, and make the following settings:
I just set the event name contract_me without setting the event parameters.
Note:If you have set event parameters, you also need to register it in GA4‘s Custom definitions before it can be used in GA4.
Preview
Next is the Preview test, you can use any of the following methods:
The event fires as expected and can be published.
The event tracking of the attributes method is complete.