This article introduces how to use gtag method to do custom event tracking.
Meet gtag method
The gtag method is also called hard coding, which is to add event tracking code directly in the source code where the location is to be tracked.
- Advantage:Tracking is accurate.
- Disadvantage:Adding to the source code requires development support, and every adjustment needs to be released.
- Limit:It can only be used for GA4 library code, which is hard-coded deployment. If it is deployed with GTM, the gtag method cannot be used for event tracking.
- 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: gtag method
Suppose I want to use the gtag method to track events at this location:
The usage of gtag is as follows:
gtag('event', '<event_name>', {
<event_parameters>
});
Set event name as gtag_method, event parameter as label, value as test (you can customize these, as long as you follow the Event Rules)
gtag('event', 'gtag_method', {
'label':'test'
});
add gtag event code
Return to the location that needs to be tracked, and view the source code of the tracked location:
The source code is:
<a href="/">Event Tracking(gtag method)</a>
After adding the gtag code:
<a href="/" onclick=“gtag('event', 'gtag_method', {'label':'test' });
”>Event Tracking(gtag method)</a>
Since it is a click, use onclick.
Preview
Next is the Preview test, you can use any of the following methods:
Custom definitions
There is also an event parameter label, 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:
gtag_method_label represents the label event parameter, you can use the dimension gtag_method_label in GA4.
The event tracking of the gtag method is complete.