Skip to content

Add nonce parameter to the initialize function #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Look for gtm_auth and gtm_preview
|events| `Object`| No | Additional events such as 'gtm.start': new Date().getTime(),event:'gtm.js'.|
|auth| `String` | No | used to set environments. |
|preview| `String` | No | used to set environments, something like `env-00`. |
|nonce| `String` | No | used to add a [nonce](https://developers.google.com/tag-manager/web/csp) |


### Note:
Expand Down
12 changes: 8 additions & 4 deletions src/TagManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ const TagManager = {
return noscript
}

const script = () => {
const script = (nonce) => {
const script = document.createElement('script')
if (nonce) {
script.setAttribute('nonce', nonce);
}
script.innerHTML = snippets.script
return script
}
Expand All @@ -29,17 +32,18 @@ const TagManager = {
dataScript
}
},
initialize: function ({ gtmId, events = {}, dataLayer, dataLayerName = 'dataLayer', auth = '', preview = '' }) {
initialize: function ({ gtmId, events = {}, dataLayer, dataLayerName = 'dataLayer', auth = '', preview = '', nonce = undefined }) {
const gtm = this.gtm({
id: gtmId,
events: events,
dataLayer: dataLayer || undefined,
dataLayerName: dataLayerName,
auth,
preview
preview,
nonce
})
if (dataLayer) document.head.appendChild(gtm.dataScript)
document.head.insertBefore(gtm.script(), document.head.childNodes[0])
document.head.insertBefore(gtm.script(nonce), document.head.childNodes[0])
document.body.insertBefore(gtm.noScript(), document.body.childNodes[0])
},
dataLayer: function ({dataLayer, dataLayerName = 'dataLayer'}) {
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/TagManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ describe('TagManager', () => {
TagManager.initialize(gtmArgs)
expect(window.dataLayer).toHaveLength(1)
})

it('should render tagmanager with nonce', () => {
TagManager.initialize({gtmId: 'GTM-000000', nonce: 'foo'})
const scripts = window.document.getElementsByTagName('script');
expect(scripts[0].nonce).toBe('foo');
})

it('should render tagmanager without nonce', () => {
TagManager.initialize({gtmId: 'GTM-000000'})
const scripts = window.document.getElementsByTagName('script');
expect(scripts[0].nonce).toBe('');
})
})