1.0.1 • Published 2 months ago

gtm-react-hook v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

šŸš€ 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! šŸŽ‰

1.0.1

2 months ago

1.0.0

2 months ago

0.0.17

10 months ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.14

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago