
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
gtm-react-hook
Advanced tools
A lightweight, fully typed React hook library for seamless Google Tag Manager (GTM) integration.
✅ 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
# Using npm
npm install gtm-react-hook
# Using yarn
yarn add gtm-react-hook
# Using pnpm
pnpm install gtm-react-hook
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 <>...</>;
};
useGTM()const { runGTM, eventGTM } = useGTM();
runGTM({ tagId, dataLayerName, environment, domain, script, nonce, devMode })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)data – Payload for the dataLayer (e.g., action, URL, customer ID, etc.).const { eventGTM } = useGTM();
const { pathname } = useLocation();
useEffect(() => {
eventGTM("page_view", { pathname });
}, [pathname]);
const { eventGTM } = useGTM();
const saveCustomerInfo = (customer) => {
eventGTM("customer_info", {
customerId: customer.customerId,
customerRegion: customer.customerRegion,
});
};
return <button onClick={saveCustomerInfo}>Submit</button>;
const { runGTM } = useGTM();
useEffect(() => {
runGTM({
gtmId: "GTM-XXXXXXX",
dataLayerName: "myGTMLayer", // Events stored in `window.myGTMLayer`
});
}, []);
const { runGTM } = useGTM();
useEffect(() => {
if (isUserConfirmAnalytics) {
runGTM({
gtmId: "GTM-XXXXXXX",
});
}
}, [isUserConfirmAnalytics]);
const { runGTM } = useGTM();
useEffect(() => {
runGTM({
gtmId: "GTM-XXXXXXX",
devMode: true, // Enables GTM logs in browser console
});
}, []);
We welcome contributions! Feel free to submit issues and PRs to make this library even better.
This project is licensed under the MIT License.
gtm-react-hook! 🎉FAQs
Easy-to-use React hooks for Google Tag Manager
We found that gtm-react-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.