React Google Tag Manager Module w/ Add Nonce Monkey Patch
npm install react-gtm-module-nonce-patch---



This is a Javascript module to React based apps that implement Google Tag Manager. It is designed to use GTM snippet.
You can easily use custom dataLayer, multiple dataLayers and additional events.
npm:
``bash`
npm install react-gtm-module-nonce-patch --save
Initializing GTM Module:
`js
import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
gtmId: 'GTM-000000'
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(
`
`js
import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
gtmId: 'GTM-000000',
dataLayer: {
userId: '001',
userProject: 'project'
}
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(
`
If you need send multiple custom dataLayer you can initialize GTM Module on different components sending different dataLayers
You can initialize it normally:
`js
import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
gtmId: 'GTM-000000',
dataLayerName: 'PageDataLayer'
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(
`
And send your data in each page you want
`js
import React from 'react'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
dataLayer: {
userId: '001',
userProject: 'project',
page: 'home'
},
dataLayerName: 'PageDataLayer'
}
...
const Home = () => {
...
TagManager.dataLayer(tagManagerArgs)
...
return (
export default Home
`
`js
import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
gtmId: 'GTM-000000',
events: {
sendUserInfo: 'userInfo'
}
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(
`
Configure how Tag Manager will works between development and production server environments.
`js
import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module-nonce-patch'
const tagManagerArgs = {
gtmId: 'GTM-000000',
auth: '6sBOnZx1hqPcO01xPOytLK',
preview: 'env-2'
}
TagManager.initialize(tagManagerArgs)
`
##### How can I find auth and preview?
Go to Google Tag Manager -> ADMIN -> Environments -> Actions -> Get Snippet.
Look for gtm_auth and gtm_preview
##### Don't know to use GTM environments?
- https://support.google.com/tagmanager/answer/6311518
- https://www.simoahava.com/analytics/better-qa-with-google-tag-manager-environments/
| Value | Type | Required | Notes |
| ------------- | -------- | -------- | ---------------------------------------------------------------------------------------- |
| gtmId | String | Yes | GTM id, must be something like GTM-000000. |Object
| dataLayer | | No | Object that contains all of the information that you want to pass to Google Tag Manager. |String
| dataLayerName | | No | Custom name for dataLayer object. |Object
| events | | No | Additional events such as 'gtm.start': new Date().getTime(),event:'gtm.js'. |String
| auth | | No | used to set environments. |String
| preview | | No | used to set environments, something like env-00. |String` | No | used to add a nonce |
| nonce |
- Disabling javascript in the browser can prevent the correct operation of this library if React is only being rendered on the client side.
- Before implementing GTM in your application ensure that you have at least one published container, otherwise Google Tag Manager snippet will return 404.