Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #209 from stelace/dynamic-skipping
Browse files Browse the repository at this point in the history
feat: skip tracking dynamically starting from first page
  • Loading branch information
MatteoGabriele authored May 30, 2019
2 parents 32a74ed + 9e01270 commit 9b71254
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 9b71254

Please sign in to comment.