Skip to content

Commit

Permalink
feat: use mock version for nuxt dev
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
pooya parsa committed Jun 1, 2020
1 parent d2fa3c0 commit ec85699
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dist
coverage

# Plugin
lib/plugin.js
lib/plugin*.js
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ Defaults:
```js
export default {
gtm: {
// Set to false to disable module in development mode
dev: true,

id: null,
enabled: undefined, /* see below */

id: undefined,
layer: 'dataLayer',
variables: {},

Expand All @@ -64,6 +63,21 @@ export default {
}
```

### Enabled

GTM module uses a debug-only version of `$gtm` during development (`nuxt dev`).

You can explicitly enable or disable it using `enabled` option:

```js
export default {
gtm: {
// Always send real GTM events (also when using `nuxt dev`)
enabled: true
}
}
```

### Manual GTM Initialization

🚨 IMPORTANT 🚨
Expand Down
4 changes: 2 additions & 2 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const defaults = {
dev: true,
enabled: undefined,

id: null,
id: undefined,
layer: 'dataLayer',
variables: {},

Expand Down
23 changes: 21 additions & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@ module.exports = async function gtmModule (_options) {
...this.options.gtm
}

// Don't include when run in dev mode unless dev: true is configured
if (this.options.dev && !options.dev) {
// By default enable only for non development
if (options.enabled === undefined) {
options.enabled = !this.options.dev
}

if (options.dev !== undefined) {
// eslint-disable-next-line no-console
console.warn('[gtm] `dev` option is deprecated! Please use `enabled`')
if (options.dev === true && this.options.dev) {
options.enabled = true
}
delete options.dev
}

if (!options.enabled) {
// Register mock plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.mock.js'),
fileName: 'gtm.js',
options
})
return
}

Expand Down
38 changes: 38 additions & 0 deletions lib/plugin.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// This is a mock version because gtm module is disabled
// You can explicitly enable module using `gtm.enabled: true` in nuxt.config

const _layer = '<%= options.layer %>'
const _id = '<%= options.id %>'

function startPageTracking (ctx) {
ctx.app.router.afterEach((to) => {
setTimeout(() => {
ctx.$gtm.push(to.gtm || {
routeName: to.name,
pageType: 'PageView',
pageUrl: '<%= options.routerBase %>' + to.fullPath,
pageTitle: (typeof document !== 'undefined' && document.title) || '',
event: '<%= options.pageViewEventName %>'
})
}, 250)
})
}

export default function (ctx, inject) {
// eslint-disable-next-line no-console
const logSyle = 'background: #2E495E;border-radius: 0.5em;color: white;font-weight: bold;padding: 2px 0.5em;'
const log = (...args) => console.log('%cGTM', logSyle, ...args)

const gtm = {
init: (id) => {
log('init', id)
},
push: (event) => {
log('push', process.client ? event : JSON.stringify(event))
}
}

ctx.$gtm = gtm
inject('gtm', gtm)
<% if (options.pageTracking) { %>if (process.client) { startPageTracking(ctx); }<% } %>
}

0 comments on commit ec85699

Please sign in to comment.