How to Install Google Tag Manager (GTM) in a React App — The Right Way

#GTM Tutorial

#Google Tag Manager


If you’re building modern web applications with React.js, chances are you’ve already thought about how to integrate tracking and analytics. One of the best tools to do that is Google Tag Manager (GTM).

In this post, I’ll walk you through a real-world implementation of GTM inside a React application — step-by-step, clean, and scalable. Whether you're tracking conversions, user events, or building a data-rich dashboard, this guide will get you started fast.

🔧 The Setup: React Codebase with GTM

Here’s what we’re working with:

  • A typical React + Node project with separate frontend and backend
  • A need to track page views and send data to Google Analytics
  • Hosted locally on localhost:8080 for testing

📦 Step 1: Install the React GTM Module

Start by installing the react-gtm-module package:

yarn add react-gtm-module

🧱 Step 2: Create a GTM Provider

To keep things clean and maintainable, let’s create a GTM Provider:

  1. Inside your frontend project, create a new folder:
  2. src/providers/GTMProvider
  3. Inside it, create a file:
  4. index.tsx

Here’s a simple example of what your provider might look like:

import { useEffect } from "react";
import TagManager from "react-gtm-module";

const GTM_ID = process.env.GOOGLE_TAG_MANAGER_ID || "GTM-N6WRXXC3";

const GTMProvider = ({ children }: { children: React.ReactNode }) => {
    useEffect(() => {
        if (GTM_ID) {
            TagManager.initialize({ gtmId: GTM_ID });
        }
    }, []);

    return children;
};

export default GTMProvider;

Now your GTM container will load automatically as your app initializes. ✅

🧪 Step 3: Test with Google Tag Assistant

Once implemented:

  • Open Tag Assistant
  • Connect it to your localhost:8080
  • You should see events like container loaded, DOM ready, and other GTM triggers firing correctly

Go to Google Analytics (GA4) and head to Real-Time Reports — you’ll begin seeing live data from your local app.

💡 Pro Tip: Use .env Files for GTM IDs

Avoid hardcoding your GTM ID. Use an .env file to store it securely:

REACT_APP_GTM_ID=GTM-XXXXXXX

Then restart your dev server to apply the changes.

🌐 Why This Matters

Using GTM in your React app gives you flexibility to:

  • Fire marketing tags (GA4, Facebook Pixel, LinkedIn, etc.)
  • Track user behavior (scrolls, clicks, form submissions)
  • Set up conversion funnels and retargeting audiences
  • Maintain separation of concerns between dev and marketing teams

And with React’s component-based structure, wrapping this logic in a provider makes your app modular and scalable.

🤝 Need Help with GTM or Advanced Tracking?

Whether you're just getting started or looking to build advanced funnels using GTM + GA4 + server-side tracking, I can help you set it up the right way.

📅 Schedule your FREE consultation now and let's get your React app firing real-time data into the tools that matter for your growth.

Like what you read? Share this post, subscribe for updates, and let’s build smarter apps together.

Himanshu Batra

Himanshu Batra

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