@nuxtjs/gtm
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.1.0](https://github.com/nuxt-community/gtm-module/compare/v2.0.1...v2.1.0) (2020-02-11) | ||
### Features | ||
* allow manually init with multiple ids (closes [#1](https://github.com/nuxt-community/gtm-module/issues/1), [#4](https://github.com/nuxt-community/gtm-module/issues/4), [#5](https://github.com/nuxt-community/gtm-module/issues/5)) ([accfa77](https://github.com/nuxt-community/gtm-module/commit/accfa77f92fa918412b19742ca13dbeedddef732)) | ||
### [2.0.1](https://github.com/nuxt-community/gtm-module/compare/v2.0.0...v2.0.1) (2020-02-10) | ||
@@ -7,0 +14,0 @@ |
const path = require('path') | ||
// doNotTrack polyfill | ||
// https://gist.github.com/pi0/a76fd97c4ea259c89f728a4a8ebca741 | ||
const dnt = "(function(w,n,d,m,e,p){w[d]=(w[d]==1||n[d]=='yes'||n[d]==1||n[m]==1||(w[e]&&w[e].p&&e[e][p]()))?1:0})(window,'navigator','doNotTrack','msDoNotTrack','external','msTrackingProtectionEnabled')" | ||
const defaults = { | ||
@@ -37,9 +41,2 @@ dev: true, | ||
// Don't include when no GTM id is given | ||
if (!options.id) { | ||
// eslint-disable-next-line no-console | ||
console.warn('[@nuxtjs/gtm] Disabling module because no id is provided!') | ||
return | ||
} | ||
// Async id evaluation | ||
@@ -52,3 +49,2 @@ if (typeof (options.id) === 'function') { | ||
const query = { | ||
id: options.id, | ||
// Default is dataLayer for google | ||
@@ -63,20 +59,16 @@ l: options.layer !== 'dataLayer' ? options.layer : null, | ||
// doNotTrack polyfill | ||
// https://gist.github.com/pi0/a76fd97c4ea259c89f728a4a8ebca741 | ||
const dnt = "(function(w,n,d,m,e,p){w[d]=(w[d]==1||n[d]=='yes'||n[d]==1||n[m]==1||(w[e]&&w[e].p&&e[e][p]()))?1:0})(window,'navigator','doNotTrack','msDoNotTrack','external','msTrackingProtectionEnabled')" | ||
// Compile scripts | ||
const initLayer = "w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'})" // deps: w,l | ||
// Compile <script> | ||
const injectScript = `var f=d.getElementsByTagName(s)[0],j=d.createElement(s);j.${options.scriptDefer ? 'defer' : 'async'}=true;j.src='${options.scriptURL + '?id=\'+i' + (queryString ? (`+'&${queryString}` + '\'') : '')};f.parentNode.insertBefore(j,f)` // deps: d,s,i | ||
const initLayer = "w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'})" // deps: w,l | ||
let script = `${dnt};(function(w,l){${initLayer}})(window,'${options.layer}');window._gtm_inject=function(i){(function(d,s){${injectScript}})(document,'script');}` | ||
const injectScript = `var f=d.getElementsByTagName(s)[0],j=d.createElement(s);j.${options.scriptDefer ? 'defer' : 'async'}=true;j.src='${options.scriptURL + '?' + queryString}';f.parentNode.insertBefore(j,f)` // deps: d,s | ||
let script | ||
if (options.autoInit) { | ||
script = `(function(w,d,s,l){${initLayer};${injectScript}})(window,document,'script','${options.layer}')` | ||
if (options.respectDoNotTrack) { | ||
script = `${dnt};window.doNotTrack||${script}` | ||
if (options.id) { | ||
script += `;${options.respectDoNotTrack ? 'window.doNotTrack||' : ''}window._gtm_inject('${options.id}')` | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn('[@nuxtjs/gtm] `autoInit` set but no id provided!') | ||
} | ||
} else { | ||
script = `${dnt};(function(w,l){${initLayer}})(window,'${options.layer}');window.$initGTM=function(){(function(d,s){${injectScript}})(document,'script');}` | ||
} | ||
@@ -83,0 +75,0 @@ |
export default function (ctx, inject) { | ||
const layer = '<%= options.layer %>' | ||
let initalized = false | ||
const initialized = {} | ||
ctx.$gtm = { | ||
init() { | ||
if (!initalized && window.$initGTM) { | ||
window.$initGTM() | ||
init(id<%= options.id ? ` = '${options.id}'` : '' %>) { | ||
if (initialized[id] || !window._gtm_inject) { | ||
return | ||
} | ||
initalized = true | ||
window._gtm_inject(id) | ||
initialized[id] = true | ||
}, | ||
@@ -11,0 +12,0 @@ push(obj) { |
{ | ||
"name": "@nuxtjs/gtm", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Google Tag Manager Module for Nuxt.js", | ||
@@ -5,0 +5,0 @@ "repository": "nuxt-community/gtm-module", |
15922
118