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

Commit

Permalink
fix(base): handle router base path
Browse files Browse the repository at this point in the history
when provided, the router base path will be added to the current router path

closes #68
  • Loading branch information
MatteoGabriele committed Nov 11, 2017
1 parent a6ab734 commit d0dcdf0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const defaultConfig = {
transformQueryString: true,
pageviewOnLoad: true,
pageviewTemplate: null,
untracked: true
untracked: true,
prependBase: true
},

debug: {
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ export function loadScript (url) {
})
}

export function getBasePath (base, path) {
const pathAsArray = path.split('/')
const baseAsArray = base.split('/')

if (pathAsArray[0] === '' && base[base.length - 1] === '/') {
pathAsArray.shift()
}

return baseAsArray.join('/') + pathAsArray.join('/')
}

export function merge (obj, src) {
Object.keys(src).forEach(function (key) {
if (obj[key] && typeof obj[key] === 'object') {
Expand Down
17 changes: 14 additions & 3 deletions src/lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isRouteIgnored,
getRouteAnalytics,
isRoute,
isRouter
isRouter,
getBasePath
} from '../helpers'

export default function page (...args) {
Expand All @@ -22,9 +23,19 @@ export default function page (...args) {
}

if (route) {
const { transformQueryString } = config.autoTracking
const {
router,
autoTracking: {
transformQueryString,
prependBase
}
} = config

const queryString = getQueryString(route.query)
const path = route.path + (transformQueryString ? queryString : '')
const needsBase = prependBase && router.options.base

let path = route.path + (transformQueryString ? queryString : '')
path = needsBase ? getBasePath(router.options.base, path) : path

set('page', path)

Expand Down

0 comments on commit d0dcdf0

Please sign in to comment.