Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
How can I add a GTM event handler on a button using React or Next?

In my Next app, I am using react-gtm-module with GTM ID. Now I want an event handler on the button so that when it is clicked, an event should be added to the Google Tag Manager data layer.

export const MyButton = () => (
  <button
    onClick={() => {
      window.dataLayer.push({ event: "some-event" });
    }}
  >
    Click Me!
  </button>
);

But it is giving me the error "Type error: Property 'dataLayer' does not exist on type 'Window & type of globalThis'."

In _app.tsx I have initialized GTM with the following code:

import type { AppProps } from "next/app";
import { useEffect } from "react";
import TagManager from "react-gtm-module";

function MyApp({ Component, pageProps }: AppProps) {
  useEffect(() => {
    TagManager.initialize({
      gtmId: "GTM-XXXXXXX",
      events: {
        sendUserInfo: "userInfo",
      },
      dataLayer: {
        userId: "001",
        userProject: "project",
      },
    });
  }, []);
return <Component {...pageProps} />;
}

export default MyApp;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!