Skip to content

Commit

Permalink
feat: skip tracking dynamically
Browse files Browse the repository at this point in the history
Useful to ignore some lazyloaded pages or avoid
app-specific duplicate events with pageviewTemplate
  • Loading branch information
abruere committed Apr 17, 2019
1 parent 0809338 commit 9e01270
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
18 changes: 18 additions & 0 deletions __tests__/lib/page.spec.js
Expand Up @@ -67,3 +67,21 @@ it ('should set and track page with a VueRouter instance', () => {
expect(window.ga).toBeCalledWith('set', 'page', '/')
expect(window.ga).toBeCalledWith('send', 'pageview', '/')
})

it ('should skip tracking when page first argument is a falsy value', () => {
$vm.$ga.page(null)
$vm.$ga.page(false)
$vm.$ga.page(undefined)
// Google officially states that page path must begin with '/'
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#page
$vm.$ga.page('')

expect(window.ga).not.toHaveBeenCalled()
expect(window.ga).not.toHaveBeenCalled()

// Skip behavior must be explicit
$vm.$ga.page()

expect(window.ga).toHaveBeenCalled()
expect(window.ga).toHaveBeenCalled()
})
6 changes: 5 additions & 1 deletion docs/page-tracking.md
Expand Up @@ -164,6 +164,10 @@ const router = new VueRouter({
```
important: the route pageviewTemplate has always priority over the global one.

`pageviewTemplate` can return a falsy value to skip tracking, which can be useful for specific needs:

- `shouldRouterUpdate` documented below is more appropriate for tracking control based on routing, but is not enough when you need to disable initial tracking on some pages, since it only applies to navigation after initial page load.
- `pageviewOnLoad: false` is global and can’t depend on current route.

## Avoid transforming route query object into querystring
It is possible to avoid route query to be sent as querystring using the `transformQueryString` property
Expand Down Expand Up @@ -206,7 +210,7 @@ Vue.use(VueAnalytics, {
})
```

For other use cases it is also possible to use the `shouldRouterUpdate`, accessable in the plugin configuartion object, inside the `autoTracking` property.
For other use cases it is also possible to use the `shouldRouterUpdate`, accessible in the plugin configuration object, inside the `autoTracking` property.
The methods has the previous and current route as parameters and it needs to return a truthy or falsy value.

```js
Expand Down
5 changes: 5 additions & 0 deletions src/lib/page.js
Expand Up @@ -11,6 +11,11 @@ import {
} from '../helpers'

export default function page (...args) {
if (args.length && !args[0]) {
// allows to dynamically prevent tracking in pageviewTemplate proxy
return
}

let route

if (args.length && isRouter(args[0])) {
Expand Down

0 comments on commit 9e01270

Please sign in to comment.