Gathering detailed insights and metrics for @sooro-io/react-gtm-module
Gathering detailed insights and metrics for @sooro-io/react-gtm-module
Gathering detailed insights and metrics for @sooro-io/react-gtm-module
Gathering detailed insights and metrics for @sooro-io/react-gtm-module
npm install @sooro-io/react-gtm-module
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
24 Stars
1 Commits
2 Forks
1 Watching
1 Branches
1 Contributors
Updated on 26 Oct 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
25.8%
2,176
Compared to previous day
Last week
16.8%
8,604
Compared to previous week
Last month
-18%
38,559
Compared to previous month
Last year
0%
186,776
Compared to previous year
This is a JS module to React-based apps that implement Google Tag Manager (GTM). It is designed so that the GTM snippet can be injected and used with minimal effort.
It was originally created and maintained by @alinemorelli, but has not been further developed since September 2020. A whole series of feature requests and pull requests have remained unprocessed since then, which have been partly included here. On top a few further changes and a fix were applied. You can find out more about it in the comparison section.
A migration guide is provided too.
Open up your terminal and install the package with your preferred package manager.
1npm install @sooro-io/react-gtm-module 2# OR 3yarn add @sooro-io/react-gtm-module
You need to adjust the code in your React application's entry file. If you started your application via Create React App, it's about src/index.js
or src/index.ts
.
1import React from 'react' 2import ReactDOM from 'react-dom/client' 3import './index.css' 4import App from './App' 5 6// start changes 7import TagManager from '@sooro-io/react-gtm-module' 8 9const tagManagerArgs = { 10 gtmId: 'GTM-xxxxxx', // replace with your GTM container ID 11} 12 13TagManager.initialize(tagManagerArgs) 14// end changes 15 16const root = ReactDOM.createRoot(document.getElementById('root')) 17root.render( 18 <React.StrictMode> 19 <App /> 20 </React.StrictMode>, 21)
That's it, you have successfully added the GTM to your react application. You can start a preview session of your GTM container to confirm that everything is working.
Alternatively, you can go to the console in your browser tab and enter google_tag_manager
. You should see a few informations and functions.
If you are facing problems, please check if you receive a 404 error for the GTM script. If so, no changes have been published to the GTM container yet. Once you have done this, the error will disappear and the GTM script will be injected.
You can interact with the dataLayer (to trigger events or push new data to it) in your components like this:
1import React from 'react'
2import TagManager from 'react-gtm-module'
3
4const Home = () => {
5 TagManager.dataLayer({
6 dataLayer: {
7 event: 'home_viewed',
8 // add other properties to set a value
9 // to unset a property use undefined as value
10 },
11 })
12
13 return (
14 <div>
15 <h1>Home</h1>
16 </div>
17 )
18}
19
20export default Home
If you are using multiple dataLayers you can define to which dataLayer the object is added by using the dataLayerName
property.
1TagManager.dataLayer({
2 dataLayer: {
3 event: 'identified',
4 userId: 'dc26b3de-5186-4fa5-a89a-60762111a5b4',
5 },
6 dataLayerName: 'personalInformation',
7})
To adapt your GTM configuration to your needs, a number of options are available. You can find examples for each of them below the table.
Value | Type | Required | Notes |
---|---|---|---|
gtmId | String | Yes | The ID of your GTM Container. |
dataLayer | Object | No | Information that should be added to the dataLayer before initialization. |
dataLayerName | String | No | Customize the name of the dataLayer object. |
events | Array of Objects | No | Additional events which will be added to the dataLayer during initialization. |
auth | String | No | If you use GTM's environment function, you need to pass the gtm_auth query parameter here. |
preview | String | No | If you use GTM's environment function, you need to pass the gtm_preview query parameter here. |
nonce | String | No | Set the nonce if you use a Content Security Policy. |
source | String | No | Customize the GTM script URL if you serve the Google scripts through your tagging servers and/or mask your GTM script. |
Information that should be added to the dataLayer before initialization. The information will be added before gtm.js
event.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 dataLayer: { 4 currency: 'USD', 5 language: 'en', 6 }, 7}
Customize the name of the dataLayer object.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 dataLayerName: 'personalInformation', 4}
Additional events which will be added to the dataLayer during initialization (after gtm.js
event).
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 events: [ 4 { 5 event: 'consent_loaded', 6 consentAnalytics: true, 7 consentAds: false, 8 consentPreferences: true, 9 }, 10 ], 11}
You have to set both properties to interact with an certain environment. Environments are an advanced GTM feature. Here is a guide which helps you to implement it if you are interested.
You have to manually extract the necessary information from the environment snippet. Inside your GTM container go to Admin -> Environments. On this page you see a list of all your environments. On the right you have the Actions for each entry. Click on it and use the function Get Snippet.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 auth: '6sBOnZx1hqPcO01xPOytLK', // add here the value of gtm_auth 4 preview: 'env-staging', // add here the value of gtm_preview 5}
Please note that >m_cookies_win=x
will be automatically added to the GTM script URL as soon the two properties are set. Please check the article of Simo Ahava for more details and challenges about it.
Content Security Policy (CSP) helps to mitigate certain types of attacks. This includes especially cross-site scripting as only certain sources of scripts (domains) are allowed to be executed. GTM supports to feature natively, you can find a whole guide in docs.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 nonce: 'KCenr5lELncZ6JJlHmerd9aIjddJfBEZ', // from you server 4}
If you use your GTM Tagging Servers to serve Google scripts, you have to use a custom URL. This property allows you to overwrite the default one https://googletagmanager.com/gtm.js
with a custom value.
For more information about this feature read the article in GTM docs. It contains also a step-by-step guide.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3 source: 'https://gtm.example.com/gtm.js', // URL including script! 4}
Basically it is still the JS module you know. However, there are a number of additional functions and a bug fix, which is a breaking change.
Changes and improvements:
Bug fix:
The events
argument was inteded to add events into the dataLayer before GTM gets initialized. Against the description only properties could be added. We fixed this problem and you are now able to add events. Properties can be still added by the setting the initial dataLayer (dataLayer
arg).
Dependencies
All you have to do is change the package. The previous TypeScript definitions are no longer required as the types are now included in the package.
1npm uninstall react-gtm-module @types/react-gtm-module 2npm install @sooro-io/react-gtm-module 3 4# OR 5 6yarn remove react-gtm-module @types/react-gtm-module 7yarn install @sooro-io/react-gtm-module
Imports
1- import TagManager from 'react-gtm-module' 2+ import TagManager from '@sooro-io/react-gtm-module'
events
arg
If you use the events
arg in the initialization you need to switch to the dataLayer
arg. If you want to add events, please check the example.
1const tagManagerArgs = { 2 gtmId: 'GTM-xxxxxx', 3- events: { 4- currency: 'USD', 5- language: 'en', 6- }, 7+ dataLayer: { 8+ currency: 'USD', 9+ language: 'en', 10+ }, 11}
No vulnerabilities found.
No security vulnerabilities found.