react-gtm-module is a JavaScript library designed for React applications to streamline the integration of Google Tag Manager (GTM). It provides a programmatic API for managing the GTM snippet, initializing GTM with a specific container ID, and interacting with the `dataLayer` object. The current stable version is 2.0.11, last updated in November 2022. While the package remains functional, the original repository (alinemorelli/react-gtm) has seen limited activity since late 2020, with many pull requests and issues unaddressed. A community-maintained fork, `@sooro-io/react-gtm-module`, has emerged to provide continued development, TypeScript support, and additional features like CSP nonce and custom GTM script URLs. This original package (`react-gtm-module`) offers a straightforward abstraction for common GTM operations, simplifying custom `dataLayer` pushes, multiple `dataLayer` instances, and environment-specific GTM configurations without direct `window.dataLayer` manipulation.
npm install react-gtm-moduleVerified import paths — ran on the pinned version, not inferred.
Initializes Google Tag Manager with a specified ID and initial `dataLayer` values. It also demonstrates how to push a custom event to the `dataLayer` after initialization using a button click, simulating common tracking scenarios.
Verify the GTM ID in your Google Tag Manager account settings and ensure it matches the ID passed to `TagManager.initialize`.
For the original `react-gtm-module`, carefully review the `events` functionality. For consistent and updated behavior, consider migrating to `@sooro-io/react-gtm-module` and consult its migration guide.
Remove any manually placed GTM `<script>` tags from your `public/index.html` or equivalent file once `TagManager.initialize` is used.
Always pass the `dataLayerName` option to both `TagManager.initialize` and `TagManager.dataLayer` if you are using a custom `dataLayer` name.
Migrate your project to use `@sooro-io/react-gtm-module` by uninstalling `react-gtm-module` and installing `@sooro-io/react-gtm-module`, then updating import paths.
Ensure `import TagManager from 'react-gtm-module'` is present at the top of your file before `TagManager` is used.
Pass a valid GTM container ID (e.g., `GTM-XXXXXX`) as the `gtmId` property to the `TagManager.initialize` function.
Double-check your `gtmId` for typos. Ensure you have published at least one version of your GTM container in the Google Tag Manager interface.