Skip to content

Commit

Permalink
fix(plugin): add "debug" option to toggle logging and disable by default
Browse files Browse the repository at this point in the history
A new "debug" option for toggling whether $gtm API calls are logged to
the console. Also changes default behavior to not log anything.

Resolves nuxt-community#39
  • Loading branch information
rchl committed Jul 29, 2020
1 parent 35d392b commit eddbe65
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Defaults:
export default {
gtm: {
enabled: undefined, /* see below */
debug: false,

id: undefined,
layer: 'dataLayer',
Expand Down Expand Up @@ -78,6 +79,10 @@ export default {
}
```

### `debug`

Whether `$gtm` API calls like `init` and `push` are logged to the console.

### Manual GTM Initialization

There are several use cases that you may need more control over initialization:
Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const defaults = {
enabled: undefined,
debug: false,

id: undefined,
layer: 'dataLayer',
Expand Down
6 changes: 6 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ module.exports = async function gtmModule (_options) {
delete options.dev
}

this.addTemplate({
src: path.resolve(__dirname, 'plugin.utils.js'),
fileName: 'gtm.utils.js',
options
})

if (!options.enabled) {
// Register mock plugin
this.addPlugin({
Expand Down
6 changes: 6 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { log } from './gtm.utils'

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

Expand All @@ -9,12 +11,14 @@ function gtmClient(ctx, initialized) {
}
window._gtm_inject(id)
initialized[id] = true
log('init', id)
},
push(obj) {
if (!window[_layer]) {
window[_layer] = []
}
window[_layer].push(obj)
log('push', obj)
}
}
}
Expand Down Expand Up @@ -50,9 +54,11 @@ function gtmServer(ctx, initialized) {
}
inits.push(id)
initialized[id] = true
log('init', id)
},
push(obj) {
events.push(obj)
log('push', JSON.stringify(obj))
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions lib/plugin.mock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This is a mock version because gtm module is disabled
// You can explicitly enable module using `gtm.enabled: true` in nuxt.config
import { log } from './gtm.utils'

const _layer = '<%= options.layer %>'
const _id = '<%= options.id %>'
Expand All @@ -19,10 +20,6 @@ function startPageTracking (ctx) {
}

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)
Expand Down
6 changes: 6 additions & 0 deletions lib/plugin.utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const logSyle = 'background: #2E495E;border-radius: 0.5em;color: white;font-weight: bold;padding: 2px 0.5em;'

export function log(...args) {
// eslint-disable-next-line no-console
<% if (options.debug) { %>console.log('%cGTM', logSyle, ...args)<% } %>
}

0 comments on commit eddbe65

Please sign in to comment.