gtm-react-hook v1.0.1
š 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 fordataLayer
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
! š
2 months ago
2 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago