GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method) | BCS GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method) | BCSBCS

GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

Google Analytics BCS 2 years ago (2023-01-28) 15 Views 0 Comments

Update time: January 13, 2025

This article introduces the addEventListener method for event tracking.

Use custom HMTL to add addEventListener to attach an event listener to an HTML element. When a specific event occurs (such as a click, mouse movement, or keyboard input) on that element, dataLayer.push is executed.

This method is used a lot, such as:

Next, take tracking for adding a website to a bookmark as an example to see how to set it up:

Step 1 : Custom HTML to inject javascript

In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Custom HTML」,Name it “HTML-Boomark”, and make the following settings:GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

Source Code:

<script type="text/javascript">   
  document.addEventListener('keydown', function(event) {
 
  var isCtrlOrCmd = event.ctrlKey || event.metaKey;
  var isDKey = event.key === 'd' || event.key === 'D';

  if (isCtrlOrCmd && isDKey) {
     <a href="https://www.bbccss.com/tag/datalayer-push" title="Read more Articles about dataLayer.push" target="_blank">dataLayer.push</a>({'event':'AddtoBookmark'});
  }
});
  
  
</script>

 

Step 2 : Set Up the Trigger

In GTM , click「Triggers」——「New」——「Choose a trigger type to begin setup…」——「Custom Event」,Name it “Bookmark”, and make the following settings:

GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

 

Step 3 : Set Up the Tags

In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Google Analytics: GA4 Event」,Name it “GA4-Event-AddtoBookmark”, and make the following settings:GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

The event name is AddtoBookmark

Step 4 : Preview and Publish

Next is preview debugging.

GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

Tags is triggered and can be published.

Read More:

 

Step 5 : Verify Data in Reports

Generally, after 24 hours, we can see the data in GA4, such as:GA4 Event Tracking Series ⑦ —— Custom Events (addEventListener Method)

 

Read More: 


If you don't understand, You can leave a comment below.
Like (0)
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