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

@jwiedeman/gtm-kit-react

v1.1.6

Published

React hooks and provider for GTM Kit - Google Tag Manager integration. Supports React 16.8+.

Readme

@jwiedeman/gtm-kit-react

CI Coverage npm version Bundle Size TypeScript License: MIT React

React hooks and provider for Google Tag Manager. StrictMode-safe. Zero double-fires.

The modern React adapter for GTM Kit - uses hooks and Context API for clean, idiomatic React code.


Installation

npm install @jwiedeman/gtm-kit @jwiedeman/gtm-kit-react
yarn add @jwiedeman/gtm-kit @jwiedeman/gtm-kit-react
pnpm add @jwiedeman/gtm-kit @jwiedeman/gtm-kit-react

Quick Start

Step 1: Wrap Your App

// App.tsx or index.tsx
import { GtmProvider } from '@jwiedeman/gtm-kit-react';

function App() {
  return (
    <GtmProvider config={{ containers: 'GTM-XXXXXX' }}>
      <YourApp />
    </GtmProvider>
  );
}

Step 2: Push Events

import { useGtmPush } from '@jwiedeman/gtm-kit-react';

function BuyButton() {
  const push = useGtmPush();

  const handleClick = () => {
    push({ event: 'purchase', value: 49.99 });
  };

  return <button onClick={handleClick}>Buy Now</button>;
}

That's it! GTM is now running.


Features

| Feature | Description | | ------------------- | ----------------------------------------- | | StrictMode-Safe | No double-fires in React development mode | | Hooks-Based | Modern React patterns with Context API | | React 16.8+ | Works with any modern React version | | TypeScript | Full type definitions included | | Consent Mode v2 | Built-in GDPR compliance hooks | | SSR Compatible | Safe for Next.js, Remix, etc. |


Available Hooks

useGtmPush()

Get a push function to send events to GTM.

const push = useGtmPush();

push({ event: 'button_click', button_id: 'cta-main' });

useGtm()

Get the full GTM context with all methods.

const { client, push, updateConsent, setConsentDefaults } = useGtm();

useGtmConsent()

Manage consent state.

const { updateConsent, setConsentDefaults } = useGtmConsent();

// After user accepts cookies
updateConsent({
  ad_storage: 'granted',
  analytics_storage: 'granted'
});

useGtmClient()

Get the raw GTM client instance.

const client = useGtmClient();

useGtmReady()

Get a function that resolves when GTM is fully loaded.

const whenReady = useGtmReady();

useEffect(() => {
  whenReady().then(() => {
    console.log('GTM is ready!');
  });
}, [whenReady]);

Consent Mode v2 (GDPR)

Setting Consent Defaults Before GTM Loads

To set consent defaults before GTM initializes, use useGtmConsent in a component that renders early:

import { GtmProvider, useGtmConsent } from '@jwiedeman/gtm-kit-react';
import { consentPresets } from '@jwiedeman/gtm-kit';

// Component that sets consent defaults on mount
function ConsentInitializer({ children }) {
  const { setConsentDefaults } = useGtmConsent();

  useEffect(() => {
    setConsentDefaults(consentPresets.eeaDefault, { region: ['EEA'] });
  }, [setConsentDefaults]);

  return <>{children}</>;
}

// App wrapper
function App() {
  return (
    <GtmProvider config={{ containers: 'GTM-XXXXXX' }}>
      <ConsentInitializer>
        <YourApp />
      </ConsentInitializer>
    </GtmProvider>
  );
}

Cookie Banner Component

import { useGtmConsent } from '@jwiedeman/gtm-kit-react';
import { consentPresets } from '@jwiedeman/gtm-kit';

function CookieBanner() {
  const { updateConsent } = useGtmConsent();

  // Accept all tracking
  const acceptAll = () => updateConsent(consentPresets.allGranted);

  // Reject all tracking
  const rejectAll = () => updateConsent(consentPresets.eeaDefault);

  // Analytics only (mixed consent)
  const analyticsOnly = () => updateConsent(consentPresets.analyticsOnly);

  // Granular: update specific categories
  const customChoice = () =>
    updateConsent({
      analytics_storage: 'granted',
      ad_storage: 'denied',
      ad_user_data: 'denied',
      ad_personalization: 'denied'
    });

  return (
    <div>
      <button onClick={acceptAll}>Accept All</button>
      <button onClick={rejectAll}>Reject All</button>
      <button onClick={analyticsOnly}>Analytics Only</button>
    </div>
  );
}

Partial Updates - Only update what changed:

// User later opts into ads from preference center
updateConsent({ ad_storage: 'granted', ad_user_data: 'granted' });
// Other categories (analytics_storage, ad_personalization) unchanged

Provider Options

<GtmProvider
  config={{
    containers: 'GTM-XXXXXX', // Required
    dataLayerName: 'dataLayer', // Optional
    host: 'https://custom.host.com', // Optional
    scriptAttributes: { nonce: '...' } // Optional: CSP
  }}
>
  {children}
</GtmProvider>

React Router Integration

import { useLocation } from 'react-router-dom';
import { useGtmPush } from '@jwiedeman/gtm-kit-react';
import { useEffect, useRef } from 'react';

function PageTracker() {
  const location = useLocation();
  const push = useGtmPush();
  const lastPath = useRef('');

  useEffect(() => {
    const path = location.pathname + location.search;
    if (path !== lastPath.current) {
      lastPath.current = path;
      push({ event: 'page_view', page_path: path });
    }
  }, [location, push]);

  return null;
}

// Add to your app
function App() {
  return (
    <GtmProvider config={{ containers: 'GTM-XXXXXX' }}>
      <BrowserRouter>
        <PageTracker />
        <Routes>...</Routes>
      </BrowserRouter>
    </GtmProvider>
  );
}

Why StrictMode-Safe Matters

In React development mode with StrictMode, components mount twice. This causes most GTM libraries to fire events twice. GTM Kit handles this automatically - you get exactly one initialization and no duplicate events.


Requirements

  • React 16.8+ (hooks support)
  • @jwiedeman/gtm-kit (peer dependency)

Related Packages


Support

Have a question, found a bug, or need help?

Open an issue on GitHub — we're actively maintaining this project and respond quickly.


License

MIT