Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 9e01270

Browse files
committed
feat: skip tracking dynamically
Useful to ignore some lazyloaded pages or avoid app-specific duplicate events with pageviewTemplate
1 parent 0809338 commit 9e01270

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

__tests__/lib/page.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,21 @@ it ('should set and track page with a VueRouter instance', () => {
6767
expect(window.ga).toBeCalledWith('set', 'page', '/')
6868
expect(window.ga).toBeCalledWith('send', 'pageview', '/')
6969
})
70+
71+
it ('should skip tracking when page first argument is a falsy value', () => {
72+
$vm.$ga.page(null)
73+
$vm.$ga.page(false)
74+
$vm.$ga.page(undefined)
75+
// Google officially states that page path must begin with '/'
76+
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#page
77+
$vm.$ga.page('')
78+
79+
expect(window.ga).not.toHaveBeenCalled()
80+
expect(window.ga).not.toHaveBeenCalled()
81+
82+
// Skip behavior must be explicit
83+
$vm.$ga.page()
84+
85+
expect(window.ga).toHaveBeenCalled()
86+
expect(window.ga).toHaveBeenCalled()
87+
})

docs/page-tracking.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ const router = new VueRouter({
164164
```
165165
important: the route pageviewTemplate has always priority over the global one.
166166

167+
`pageviewTemplate` can return a falsy value to skip tracking, which can be useful for specific needs:
168+
169+
- `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.
170+
- `pageviewOnLoad: false` is global and can’t depend on current route.
167171

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

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

212216
```js

src/lib/page.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import {
1111
} from '../helpers'
1212

1313
export default function page (...args) {
14+
if (args.length && !args[0]) {
15+
// allows to dynamically prevent tracking in pageviewTemplate proxy
16+
return
17+
}
18+
1419
let route
1520

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

0 commit comments

Comments
 (0)