GA4 Custom Event Tracking with gtag.js

Google Analytics BCS 3 years ago (2023-01-31) 4404 Views 0 Comments
文章目录[隐藏]

Update time: January 13, 2025

If your website sends data to GA4 using gtag.js, you can track custom events without using GTM.

This approach is suitable for websites that implement GA4 directly in the source code. Instead of creating tags and triggers in GTM, you add the gtag() function wherever you want an event to be sent.

However, because each event must be added manually to your website’s code, this method is generally less flexible and harder to maintain than using GTM.

Note: If your website already uses GTM, you should create GA4 events in GTM instead of using gtag().

In this guide, you’ll learn how to send a custom GA4 event using gtag.js, verify that it’s working, and register event parameters in GA4.

When Should You Use gtag.js?

Using gtag.js is appropriate when:

  • Your website doesn’t use GTM.
  • You only need to track a small number of custom events.
  • You have direct access to your website’s source code.

If your website already uses GTM, creating events in GTM is usually the recommended approach because it’s easier to maintain and doesn’t require code changes.

 

How to Send a Custom Event with gtag.js

The basic syntax is:

gtag('event', '<event_name>', {
<event_parameters>
});

Where:

  • event_name is the name of the event.
  • event_parameters contains any additional information you want to send.

Example:

gtag('event', 'gtag_method', {
  label: 'test'
});

In this example:

Item Value
Event name gtag_method
Parameter label = test

 

 

Step 1 :Add gtag Tracking Code

Suppose I want to use the gtag method to track events at this location:

GA4 Custom Event Tracking with gtag.js

Original HTML looks like this::

<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 the visitor clicks the link, GA4 receives the custom event.

 

Step 2: Test the Event

Before publishing your changes, verify that the event is being sent correctly.

The easiest way is to open GA4 DebugView and trigger the event from your website.

GA4 Custom Event Tracking with gtag.js

You can see that the gtag_method event is triggered. Click on it to see the event parameter label:

GA4 Custom Event Tracking with gtag.js

The event parameters are also correct and can be published.

 

Step 3: Register Custom Parameters

In this example, the event includes an event parameter label

If you want to use label in standard reports or Explorations, register it as an Event-scoped Custom Dimension.

In GA4,click「Admin」——「Custom definitions」——「Create custom dimension」, then do the following configuration:

GA4 Custom Event Tracking with gtag.js

After registration, GA4 will begin storing this parameter as a reporting dimension.

 

Step 4: Verify the Data

Generally, after 24 hours, we can see the data in GA4, such as:GA4 Custom Event Tracking with gtag.js

 

Common Questions

Do I need GTM to use gtag()?

No.

gtag.js works independently of GTM.

Can I use gtag() together with GTM?

It’s technically possible, but Google generally recommends using one implementation method to avoid duplicate events.

Summary

The gtag() function provides a simple way to send custom events directly to Google Analytics 4 without using GTM.

It’s a good choice for websites with a straightforward tracking setup or where GTM isn’t installed. However, for larger implementations with many events, GTM offers greater flexibility, easier maintenance, and a more scalable tracking architecture.

Like (1)
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