@nuxtjs/gtm
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -5,2 +5,19 @@ # Changelog | ||
## [2.2.0](https://github.com/nuxt-community/gtm-module/compare/v2.1.1...v2.2.0) (2020-02-23) | ||
### Features | ||
* enable noscript by default ([f511404](https://github.com/nuxt-community/gtm-module/commit/f511404d2a46d082e4f730daeb618543692d748d)) | ||
* push page title to dataLayer ([#8](https://github.com/nuxt-community/gtm-module/issues/8)) ([9434562](https://github.com/nuxt-community/gtm-module/commit/943456256c5eba9ec0104ca5a9b2f8aee31f66ea)) | ||
* ssr iframe injection ([d678f16](https://github.com/nuxt-community/gtm-module/commit/d678f16d46343e54ba788d0e13399d3b3c2234f7)) | ||
* ssr support ([d896618](https://github.com/nuxt-community/gtm-module/commit/d896618ef23b4a6fbc723133d3b6b7266f67aad5)) | ||
### Bug Fixes | ||
* add id to noscript iframe ([b345db7](https://github.com/nuxt-community/gtm-module/commit/b345db7deddf2fe2e11e5ee2925deda51c739b95)) | ||
* call startPageTracking on client only ([774fccf](https://github.com/nuxt-community/gtm-module/commit/774fccffd8c9712229c9c4d90512f88546fe5ce4)) | ||
* pageTitle is not available on SSR ([228d796](https://github.com/nuxt-community/gtm-module/commit/228d796c8b3c982520c26c296e9a2af937f90e28)) | ||
### [2.1.1](https://github.com/nuxt-community/gtm-module/compare/v2.1.0...v2.1.1) (2020-02-11) | ||
@@ -7,0 +24,0 @@ |
const path = require('path') | ||
const defaults = require('./defaults') | ||
@@ -7,24 +8,2 @@ // doNotTrack polyfill | ||
const defaults = { | ||
dev: true, | ||
id: null, | ||
layer: 'dataLayer', | ||
variables: {}, | ||
pageTracking: false, | ||
pageViewEventName: 'nuxtRoute', | ||
autoInit: true, | ||
respectDoNotTrack: true, | ||
scriptId: 'gtm-script', | ||
scriptDefer: false, | ||
scriptURL: 'https://www.googletagmanager.com/gtm.js', | ||
noscript: false, | ||
noscriptId: 'gtm-noscript', | ||
noscriptURL: 'https://www.googletagmanager.com/ns.html' | ||
} | ||
module.exports = async function gtmModule (_options) { | ||
@@ -63,7 +42,7 @@ const options = { | ||
let script = `${dnt};(function(w,l){${initLayer}})(window,'${options.layer}');window._gtm_inject=function(i){(function(d,s){${injectScript}})(document,'script');}` | ||
let script = `${initLayer};w[x]={};w._gtm_inject=function(i){if(w.doNotTrack||w[x][i])return;w[x][i]=1;${injectScript};}` | ||
if (options.autoInit) { | ||
if (options.id) { | ||
script += `;${options.respectDoNotTrack ? 'window.doNotTrack||' : ''}window._gtm_inject('${options.id}')` | ||
script += `;w[y]('${options.id}')` | ||
} else { | ||
@@ -75,2 +54,5 @@ // eslint-disable-next-line no-console | ||
// Add doNotTrack polyfill and wrap to IIFE | ||
script = `${dnt};(function(w,d,s,l,x,y){${script}})(window,document,'script','${options.layer}','_gtm_ids','_gtm_inject')` | ||
// Guard against double IIFE executation in SPA mode (#3) | ||
@@ -87,3 +69,4 @@ script = `if(!window._gtm_init){window._gtm_init=1;${script}}` | ||
// Prepend google tag manager <noscript> fallback to <body> | ||
if (options.noscript) { | ||
const renderIframe = id => `<iframe src="${options.noscriptURL + '?id=' + id + '&' + queryString}" height="0" width="0" style="display:none;visibility:hidden" title="gtm"></iframe>` | ||
if (options.noscript && options.id) { | ||
this.options.head.noscript = this.options.head.noscript || [] | ||
@@ -93,3 +76,3 @@ this.options.head.noscript.push({ | ||
pbody: true, | ||
innerHTML: `<iframe src="${options.noscriptURL + '?' + queryString}" height="0" width="0" style="display:none;visibility:hidden" title="gtm"></iframe>` | ||
innerHTML: options.id ? renderIframe(options.id) : '' /* placeholder for SSR calls */ | ||
}) | ||
@@ -107,4 +90,6 @@ } | ||
fileName: 'gtm.js', | ||
ssr: false, | ||
options | ||
options: { | ||
...options, | ||
renderIframe | ||
} | ||
}) | ||
@@ -111,0 +96,0 @@ } |
@@ -1,6 +0,7 @@ | ||
export default function (ctx, inject) { | ||
const layer = '<%= options.layer %>' | ||
const initialized = {<%= (options.autoInit && options.id) ? ` '${options.id}': true ` : '' %>} | ||
ctx.$gtm = { | ||
init(id<%= options.id ? ` = '${options.id}'` : '' %>) { | ||
const _layer = '<%= options.layer %>' | ||
const _id = '<%= options.id %>' | ||
function gtmClient(ctx, initialized) { | ||
return { | ||
init(id = _id) { | ||
if (initialized[id] || !window._gtm_inject) { | ||
@@ -13,22 +14,68 @@ return | ||
push(obj) { | ||
if (!window[layer]) { | ||
window[layer] = [{ 'gtm.start': new Date().getTime(), event: 'gtm.js' }] | ||
if (!window[_layer]) { | ||
window[_layer] = [{ 'gtm.start': new Date().getTime(), event: 'gtm.js' }] | ||
} | ||
window[layer].push(obj) | ||
window[_layer].push(obj) | ||
} | ||
} | ||
inject('gtm', ctx.$gtm) | ||
} | ||
<% if (options.pageTracking) { %> | ||
function gtmServer(ctx, initialized) { | ||
const events = [] | ||
const inits = [] | ||
ctx.beforeNuxtRender(() => { | ||
if (!inits.length && !events.length) { | ||
return | ||
} | ||
const gtmScript = ctx.app.head.script.find(s => s.hid = '<%= options.scriptId %>') | ||
if (inits.length) { | ||
gtmScript.innerHTML += `;${JSON.stringify(inits)}.forEach(function(i){window._gtm_inject(i)})` | ||
} | ||
<% if (options.noscript) { %> | ||
const gtmIframe = ctx.app.head.noscript.find(s => s.hid = '<%= options.noscriptId %>') | ||
const renderIframe = id => `<%= options.renderIframe('${id}') %>` | ||
if (inits.length) { | ||
gtmIframe.innerHTML += inits.map(renderIframe) | ||
} | ||
<% } %> | ||
if (events.length) { | ||
gtmScript.innerHTML += `;${JSON.stringify(events)}.forEach(function(e){window['${_layer}'].push(e)})` | ||
} | ||
}) | ||
return { | ||
init(id = _id) { | ||
if (initialized[id]) { | ||
return | ||
} | ||
inits.push(id) | ||
initialized[id] = true | ||
}, | ||
push(obj) { | ||
events.push(obj) | ||
} | ||
} | ||
} | ||
function startPageTracking(ctx) { | ||
ctx.app.router.afterEach((to) => { | ||
setImmediate(() => { | ||
setTimeout(() => { | ||
ctx.$gtm.push(to.gtm || { | ||
routeName: to.name, | ||
pageType: 'PageView', | ||
pageUrl: to.fullPath, | ||
pageUrl: to.fullPath, | ||
pageTitle: (typeof document !== 'undefined' && document.title) || '', | ||
event: '<%= options.pageViewEventName %>' | ||
}) | ||
}) | ||
}) | ||
}, 250) | ||
}) | ||
<% } %> | ||
} | ||
export default function (ctx, inject) { | ||
const initialized = {<%= (options.autoInit && options.id) ? ` '${options.id}': true ` : '' %>} | ||
ctx.$gtm = process.client ? gtmClient(ctx, initialized) : gtmServer(ctx, initialized) | ||
inject('gtm', ctx.$gtm) | ||
<% if (options.pageTracking) { %>if (process.client) { startPageTracking(ctx); }<% } %> | ||
} |
{ | ||
"name": "@nuxtjs/gtm", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Google Tag Manager Module for Nuxt.js", | ||
@@ -18,2 +18,3 @@ "repository": "nuxt-community/gtm-module", | ||
"dev": "nuxt example", | ||
"generate": "nuxt generate example", | ||
"lint": "eslint --ext .js,.vue example lib test", | ||
@@ -20,0 +21,0 @@ "release": "yarn test && standard-version && git push --follow-tags && npm publish", |
@@ -38,2 +38,4 @@ # @nuxtjs/gtm | ||
Defaults: | ||
```js | ||
@@ -58,3 +60,3 @@ { | ||
noscript: false, | ||
noscript: true, | ||
noscriptId: 'gtm-noscript', | ||
@@ -93,3 +95,3 @@ noscriptURL: 'https://www.googletagmanager.com/ns.html' | ||
2. Install dependencies using `yarn install` or `npm install` | ||
3. Start development server using `npm run dev` | ||
3. Start development server using `yarn dev` or `GTM_ID=<your gtm id> yarn dev` if you want to provide custom GTM_ID. | ||
@@ -96,0 +98,0 @@ ## License |
19098
7
166
116