Tracking Custom Events in React with Google Tag Manager (GTM) and GA4

#GTM Tutorial

#Google Tag Manager

Want to track user interactions—like a "Sign Out" button click—in your React app without hardcoding analytics?

Google Tag Manager (GTM) makes it seamless to manage tracking and send custom events to Google Analytics 4 (GA4)—all without constantly editing your codebase.

In this tutorial, you’ll learn how to create and track a custom event from a React.js app using GTM, in just a few easy steps.

Why Use Google Tag Manager for Event Tracking?

If you're a React developer or digital marketer, GTM helps you:

  • Track clicks without editing code every time
  • Separate dev and marketing concerns
  • Preview/debug before going live
  • Send events to GA4, Ads, Facebook Pixel, and more

Project Setup

We're working in a typical React.js app. The goal is to track when a user clicks the "Sign Out" button and send that event to Google Analytics via GTM.

Create a GTM Utility in React

Start by installing and setting up the react-gtm-module or create your own GTM utility function like this:

// utils/tagManager.ts
export const GTMEvent = ({ event, category, action, label }) => {
  if (window && window.dataLayer) {
    window.dataLayer.push({
      event,
      category,
      action,
      label,
    });
  }
};

Trigger the GTM Event on Button Click

Use the utility in your React component:

import { GTMEvent } from './utils/tagManager';

<button
  onClick={() =>
    GTMEvent({
      event: 'button_click',
      category: 'User Interaction',
      action: 'Sign Out Button Clicked',
      label: 'Logout Button'
    })
  }
>
  Sign Out
</button>

Create a Custom Trigger in GTM

In your Google Tag Manager workspace:

  1. Go to Triggers → New
  2. Choose Custom Event
  3. Name: button_click
  4. Condition: All Custom Events
  5. Save the trigger

Create a GA4 Event Tag in GTM

Next, set up a tag that sends the event to GA4:

  1. Go to Tags → New
  2. Choose GA4 Event
  3. Add your GA4 Measurement ID
  4. Event Name: sign_out_button_clicked
  5. Trigger: Select the button_click trigger you created
  6. Save and Submit the changes

Preview and Test Your Setup

Use Tag Assistant (Preview mode) in GTM:

  1. Click the Sign Out button on your site
  2. Verify that the custom event fires
  3. Go to Google Analytics 4 → Real-Time → Events

Confirm that the event appears in the GA4 dashboard

You’ve now successfully:

  • Created a custom GTM event in React
  • Set up a custom trigger and tag in Google Tag Manager
  • Verified the event in Google Analytics 4

With this setup, you can replicate tracking for Login, Register, CTA clicks, scrolls, downloads, and more!

📌 Need help with GTM, Facebook Pixel, or Server-Side Tagging?

Check out Guru Labs — where we help you set up clean, reliable analytics infrastructure for your business.

Himanshu Batra

Himanshu Batra

Himanshu Batra is the founder and lead writer at Gurulabs, where they share deep insights into digital marketing.