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

Commit d0dcdf0

Browse files
fix(base): handle router base path
when provided, the router base path will be added to the current router path closes #68
1 parent a6ab734 commit d0dcdf0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const defaultConfig = {
1515
transformQueryString: true,
1616
pageviewOnLoad: true,
1717
pageviewTemplate: null,
18-
untracked: true
18+
untracked: true,
19+
prependBase: true
1920
},
2021

2122
debug: {

src/helpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ export function loadScript (url) {
1717
})
1818
}
1919

20+
export function getBasePath (base, path) {
21+
const pathAsArray = path.split('/')
22+
const baseAsArray = base.split('/')
23+
24+
if (pathAsArray[0] === '' && base[base.length - 1] === '/') {
25+
pathAsArray.shift()
26+
}
27+
28+
return baseAsArray.join('/') + pathAsArray.join('/')
29+
}
30+
2031
export function merge (obj, src) {
2132
Object.keys(src).forEach(function (key) {
2233
if (obj[key] && typeof obj[key] === 'object') {

src/lib/page.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
isRouteIgnored,
88
getRouteAnalytics,
99
isRoute,
10-
isRouter
10+
isRouter,
11+
getBasePath
1112
} from '../helpers'
1213

1314
export default function page (...args) {
@@ -22,9 +23,19 @@ export default function page (...args) {
2223
}
2324

2425
if (route) {
25-
const { transformQueryString } = config.autoTracking
26+
const {
27+
router,
28+
autoTracking: {
29+
transformQueryString,
30+
prependBase
31+
}
32+
} = config
33+
2634
const queryString = getQueryString(route.query)
27-
const path = route.path + (transformQueryString ? queryString : '')
35+
const needsBase = prependBase && router.options.base
36+
37+
let path = route.path + (transformQueryString ? queryString : '')
38+
path = needsBase ? getBasePath(router.options.base, path) : path
2839

2940
set('page', path)
3041

0 commit comments

Comments
 (0)