Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): add debug option and disable by default #61

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 2 additions & 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,7 @@ function startPageTracking (ctx) {
}

export default function (ctx, inject) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
// 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)

log('Using mocked API. Real GTM events will not be reported.')
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)<% } %>
}