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

Commit a571718

Browse files
feat(page): add skipSamePath and shouldRouterUpdate
add the possibility to skip router changes when current path is equal to the previous one or apply a custom logic with the shouldRouterUpdate method closes #73
1 parent e72430d commit a571718

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const defaultConfig = {
1717
},
1818

1919
autoTracking: {
20+
shouldRouterUpdate: null,
21+
skipSamePath: false,
2022
exception: false,
2123
page: true,
2224
transformQueryString: true,

src/helpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ export function isRoute (data) {
9797

9898
export function isRouter (data) {
9999
return data.currentRoute
100+
}
101+
102+
export function hasProps (props) {
103+
return Object.keys(props).length > 0
100104
}

src/lib/page.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
getRouteAnalytics,
99
isRoute,
1010
isRouter,
11-
getBasePath
11+
getBasePath,
12+
hasProps
1213
} from '../helpers'
1314

1415
export default function page (...args) {
@@ -76,7 +77,25 @@ export function autotracking () {
7677
trackRoute(router.currentRoute)
7778
}
7879

79-
config.router.afterEach(function () {
80+
config.router.afterEach(function (to, from) {
81+
const { skipSamePath, shouldRouterUpdate } = autoTracking
82+
const hasSamePath = to.path === from.path
83+
const hasQueryString = hasProps(to.query) || hasProps(from.query)
84+
85+
// Default behaviour of the router when the `skipSamePath` is turned on.
86+
// Skip router change when current and previous route have the same path
87+
// but query string props are empty on both sides
88+
// https://github.com/MatteoGabriele/vue-analytics/issues/73
89+
if (skipSamePath && (hasSamePath && !hasQueryString)) {
90+
return
91+
}
92+
93+
// Adds a custom way to define when the router should track
94+
if (typeof shouldRouterUpdate === 'function' && !shouldRouterUpdate(to, from)) {
95+
return
96+
}
97+
98+
// Add a 0 timeout so that the browser doesn't register the previous route
8099
// https://github.com/MatteoGabriele/vue-analytics/issues/44
81100
setTimeout(function () {
82101
trackRoute(router.currentRoute)

0 commit comments

Comments
 (0)