Custom Events in Google Analytics 4 (ga-data method) | BCS Custom Events in Google Analytics 4 (ga-data method) | BCSBCS

Custom Events in Google Analytics 4 (ga-data method)

Google Analytics BCS 2 years ago (2023-02-02) 2037 Views 0 Comments

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

Custom Events in Google Analytics 4 (ga-data method)

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:

Custom Events in Google Analytics 4 (ga-data method)

 

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:

Custom Events in Google Analytics 4 (ga-data method)

After adding the ga-data:

Custom Events in Google Analytics 4 (ga-data method)

 

Variables

In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Auto-Event Variable」,Name it “element”, and make the following settings:

Custom Events in Google Analytics 4 (ga-data method)

 

 

In GTM , click「Variables」——「New」——「Choose a variables type to begin setup…」——「Custom JavaScript」,Name it “GetGaData”, and make the following settings:

Custom Events in Google Analytics 4 (ga-data method)

 

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:

Custom Events in Google Analytics 4 (ga-data method)

 

 

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:

Custom Events in Google Analytics 4 (ga-data method)

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:

Custom Events in Google Analytics 4 (ga-data method)

 

Preview

Next is the Preview test, you can use any of the following methods:

 

Custom Events in Google Analytics 4 (ga-data method)

Click to open it:

Custom Events in Google Analytics 4 (ga-data method)

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:

Custom Events in Google Analytics 4 (ga-data method)

 

 

 

The event tracking of the ga-data method is complete.


If you don't understand, You can leave a comment below.
Like (3)
Post my comment
Cancel comment
Expression Bold Strikethrough Center Italic

Hi, you need to fill in your nickname and email!

  • Nickname (required)
  • Email (required)
  • Website