Skip to content

Commit

Permalink
feat: add router automatic PageView tracking (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDASILVA committed Nov 1, 2020
1 parent 50ce676 commit 9771c44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Add `nuxt-facebook-pixel-module` to `modules` section of `nuxt.config.js`.
/* module options */
track: 'PageView',
pixelId: 'FACEBOOK_PIXEL_ID',
autoPageView: true,
disabled: false
}],
]
Expand All @@ -60,11 +61,29 @@ or even
/* module options */
track: 'PageView',
pixelId: 'FACEBOOK_PIXEL_ID',
autoPageView: true,
disabled: false
},
}
```

## Automatically track PageView

By default, the module won't trigger any tracking event on route change. To enable this behaviour, you must specify the `autoPageView` option and set to `true` in the Nuxt module options.

```js
{
modules: [
'nuxt-facebook-pixel-module',
],
facebook: {
/* module options */
pixelId: 'FACEBOOK_PIXEL_ID',
autoPageView: true
},
}
```

## Disabling the pixel (for GDPR)

If you'd like to install the pixel disabled, and enable it later after the user has consented to its use, you can do so by setting `disabled: true` in the pixel configuration:
Expand Down Expand Up @@ -99,6 +118,7 @@ List of possible options in the module:
| track | PageView | false | Default tracking event. |
| version | 2.0 | false | Tracking version. |
| disabled | false | false | Disable the Pixel by default when initialized. Can be enabled later through `$fb.enable()` and disabled again with `$fb.disable()`.
| autoPageView | false | false | If set to `true`, automatically triggers a `PageView` track event on every page change.

## Facebook pixel instance

Expand Down
1 change: 1 addition & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = function facebookPixelModule (moduleOptions) {
const defaults = {
pixelId: null,
track: 'PageView',
autoPageView: false,
version: '2.0',
disabled: false,
debug: false
Expand Down
13 changes: 13 additions & 0 deletions lib/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ export default (ctx, inject) => {
}

const instance = new Fb(_fbq, <%= JSON.stringify(options) %>);

/**
* Automatically track PageView
*/
<% if (options.autoPageView) { %>
if (ctx.app && ctx.app.router) {
const router = ctx.app.router
router.afterEach(() => {
instance.track('PageView')
})
}
<% } %>

/* eslint-enable */
ctx.$fb = instance
inject('fb', instance)
Expand Down

0 comments on commit 9771c44

Please sign in to comment.