npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

gtm-react-hook

v1.0.1

Published

Easy-to-use React hooks for Google Tag Manager

Downloads

416

Readme

🚀 gtm-react-hook: Easy-to-use React hooks for Google Tag Manager

A lightweight, fully typed React hook library for seamless Google Tag Manager (GTM) integration.


🎯 Features

✅ Simple & intuitive React hooks ✅ Fully typed with TypeScript ✅ Tiny bundle size (~1kB) ✅ Customizable GTM configuration ✅ Zero external dependencies (only React 19) ✅ Built-in event logging ✅ Test coverage ensured


📦 Installation

# Using npm
npm install gtm-react-hook

# Using yarn
yarn add gtm-react-hook

# Using pnpm
pnpm install gtm-react-hook

⚡ Quickstart

import { useEffect } from "react";
import { useGTM } from "gtm-react-hook";
import { useLocation } from "react-router-dom";

const App = () => {
  const { runGTM, eventGTM } = useGTM();
  const { pathname } = useLocation();

  useEffect(() => {
    runGTM({
      tagId: "GTM-XXXXXXX", // Provide your GTM Tag ID
    });
  }, []);

  useEffect(() => {
    eventGTM("page_view", { pathname });
  }, [pathname]);

  return <>...</>;
};

🔍 API Reference

🏗 useGTM()

const { runGTM, eventGTM } = useGTM();

runGTM({ tagId, dataLayerName, environment, domain, script, nonce, devMode })

  • tagId (required) – Your GTM measurement ID.
  • dataLayerName – Custom name for dataLayer object.
  • environment – GTM environment parameters (gtm_auth, gtm_preview).
  • domain – Custom GTM domain.
  • script – Custom GTM script name.
  • nonce – Security nonce.
  • devMode – Enables logging for GTM initialization & events.

eventGTM(eventName: string, data?: object)

  • eventName (required) – Name of the event.
  • data – Payload for the dataLayer (e.g., action, URL, customer ID, etc.).

📖 Usage Examples

📊 Page Tracking

const { eventGTM } = useGTM();
const { pathname } = useLocation();

useEffect(() => {
  eventGTM("page_view", { pathname });
}, [pathname]);

🎯 Track Custom Events

const { eventGTM } = useGTM();

const saveCustomerInfo = (customer) => {
  eventGTM("customer_info", {
    customerId: customer.customerId,
    customerRegion: customer.customerRegion,
  });
};

return <button onClick={saveCustomerInfo}>Submit</button>;

🛠 Custom Data Layer Name

const { runGTM } = useGTM();

useEffect(() => {
  runGTM({
    gtmId: "GTM-XXXXXXX",
    dataLayerName: "myGTMLayer", // Events stored in `window.myGTMLayer`
  });
}, []);

🔒 Install GTM Only After User Consent

const { runGTM } = useGTM();

useEffect(() => {
  if (isUserConfirmAnalytics) {
    runGTM({
      gtmId: "GTM-XXXXXXX",
    });
  }
}, [isUserConfirmAnalytics]);

🛠 Enable Debugging & Logging

const { runGTM } = useGTM();

useEffect(() => {
  runGTM({
    gtmId: "GTM-XXXXXXX",
    devMode: true, // Enables GTM logs in browser console
  });
}, []);

❤️ Contributing

We welcome contributions! Feel free to submit issues and PRs to make this library even better.


📜 License

This project is licensed under the MIT License.


🚀 Happy tracking with gtm-react-hook! 🎉