Update time: January 13, 2025
This article introduces how to use gtag method to do custom event tracking.
The gtag method is suitable for direct deployment of GA4 without using GTM.
It requires adding gtag tracking code at the tracking location, so it is very inefficient.
If you use GTM, this method does not apply.
Suppose I want to use the gtag method to track events at this location:
Step 1 :Add gtag Tracking Code
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'
});
The source code is:
<a href="/" >gtag method demo test</a>
After adding the gtag code:
<a href="/" onclick="gtag('event', 'gtag_method', {'label':'test'});">gtag method demo test</a>
Whenever this location is clicked, the gtag_method event will be sent.
Step 2 : Preview and Publish
The gtag method requires the plug-in Google Analytics Debugger to debug.
Open the Google Analytics Debugger, then simulate the user to click, and finally open the Debugview of GA4:
You can see that the gtag_method event is triggered. Click on it to see the event parameter label:
The event parameters are also correct and can be published.
Read More:
Step 3 : 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:
Step 4 : 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