Skip to content

Commit

Permalink
feat(google-tag-manager): add <noscript> fallback (#263) (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
derz authored and pi0 committed Aug 31, 2019
1 parent a5bb897 commit f0dd65f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/google-tag-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ id: () => {
id: 'GTM-XXXXXXX',
layer: 'dataLayer',
pageTracking: false,
respectDoNotTrack: false,
dev: true, // set to false to disable in dev mode
query: {
// query params...
gtm_auth: '...',
gtm_preview: '...',
gtm_cookies_win: '...'
},
scriptURL: '//example.com'
scriptURL: '//example.com',
noscriptURL: '//example.com'
}
```

### Router Integration

You can optionally set `pageTracking` option to `true` to track page views.
You can optionally set `pageTracking` option to `true` to track page views.

This is disabled by default to prevent double events when using alongside with Google Analytics so take care before enabling this option.

Expand Down
22 changes: 20 additions & 2 deletions packages/google-tag-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module.exports = async function nuxtTagManager(_options) {
pageTracking: false,
respectDoNotTrack: false,
dev: true,
env: {}, // env is supported for backward compability and is alias of query
query: {}
query: {},
scriptURL: '//www.googletagmanager.com/gtm.js',
noscriptURL: '//www.googletagmanager.com/ns.html',
env: {} // env is supported for backward compability and is alias of query
})

// Don't include when run in dev mode unless dev: true is configured
Expand Down Expand Up @@ -40,12 +42,28 @@ module.exports = async function nuxtTagManager(_options) {
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`)
.join('&')

// sanitization before to avoid errors like "cannot push to undefined"
this.options.head = this.options.head || {}
this.options.head.noscript = this.options.head.noscript || []
this.options.head.script = this.options.head.script || []

// Add google tag manager script to head
this.options.head.script.push({
src: (options.scriptURL || '//www.googletagmanager.com/gtm.js') + '?' + queryString,
async: true
})

// prepend google tag manager <noscript> fallback to <body>
this.options.head.noscript.push({
vmid: 'gtm-noscript',
innerHTML: `<iframe src="${(options.noscriptURL || '//www.googletagmanager.com/ns.html')}?${queryString}" height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
pbody: true
})

// disables sanitazion for gtm noscript as we're using .innerHTML
this.options.head.__dangerouslyDisableSanitizersByTagID = this.options.head.__dangerouslyDisableSanitizersByTagID || {};
this.options.head.__dangerouslyDisableSanitizersByTagID['gtm-noscript'] = ['innerHTML']

// Register plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.js'),
Expand Down

0 comments on commit f0dd65f

Please sign in to comment.