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

Commit c2d5c12

Browse files
feat(page): add custom route pageviewTemplate
add the possibility to create a custom pageview template directly in a single route closes #65
1 parent e770ad0 commit c2d5c12

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

docs/page-tracking.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,38 @@ Vue.use(VueAnalytics, {
114114
})
115115
```
116116

117+
It is also possible to add custom data structure for each route, using the meta object
118+
119+
```js
120+
import Vue from 'vue'
121+
import VueAnalytics from 'vue-analytics'
122+
import VueRouter from 'vue-router'
123+
124+
const router = new VueRouter({
125+
routes: [
126+
{
127+
name: 'home',
128+
path: '/',
129+
component: {...},
130+
meta: {
131+
analytics: {
132+
pageviewTemplate (route) {
133+
return {
134+
title: 'This is my custom title',
135+
path: route.path,
136+
location: 'www.mydomain.com'
137+
}
138+
}
139+
}
140+
}
141+
}
142+
]
143+
})
144+
145+
```
146+
important: the route pageviewTemplate has always priority over the global one.
147+
148+
117149
## Avoid trasnforming route query object into querystring
118150
It is possible to avoid route query to be sent as querystring using the `transformRouteParams` property
119151

src/helpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ export function getQueryString (queryMap) {
6464
export function isRouteIgnored (name) {
6565
return config.ignoreRoutes.indexOf(name) !== -1
6666
}
67+
68+
export function isRoute (route) {
69+
// just check some random properties that we know
70+
// are inside the route object
71+
return route.query && route.params
72+
}

src/lib/page.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import config from '../config'
22
import set from 'lib/set'
33
import query from 'lib/query'
4-
import { noop, getQueryString, isRouteIgnored } from '../helpers'
4+
import {
5+
noop,
6+
getQueryString,
7+
isRouteIgnored,
8+
getRouteAnalytics,
9+
isRoute
10+
} from '../helpers'
511

612
export default function page (...args) {
7-
if (typeof args[0] !== 'string' && 'currentRoute' in args[0]) {
13+
if (isRoute(args[0])) {
814
const { transformQueryString } = config.autoTracking
9-
const route = args[0].currentRoute
15+
const route = args[0]
1016
const queryString = getQueryString(route.query)
1117
const path = route.path + (transformQueryString ? queryString : '')
1218

1319
set('page', path)
20+
1421
query('send', 'pageview', {
1522
page: path,
1623
title: route.name,
@@ -24,14 +31,16 @@ export default function page (...args) {
2431
query('send', 'pageview', ...args)
2532
}
2633

27-
export function trackRoute (proxy, router) {
28-
const { currentRoute } = router
29-
30-
if (isRouteIgnored(currentRoute.name)) {
34+
export function trackRoute (route) {
35+
if (isRouteIgnored(route)) {
3136
return
3237
}
33-
34-
page(proxy ? proxy(currentRoute) : router)
38+
39+
const { autoTracking } = config
40+
const { meta: { analytics = {} } } = route
41+
const proxy = analytics.pageviewTemplate || autoTracking.pageviewTemplate
42+
43+
page(proxy ? proxy(route) : route)
3544
}
3645

3746
export function startAutoTracking () {
@@ -42,13 +51,13 @@ export function startAutoTracking () {
4251
}
4352

4453
if (autoTracking.pageviewOnLoad) {
45-
trackRoute(autoTracking.pageviewTemplate, router)
54+
trackRoute(router.currentRoute)
4655
}
4756

4857
config.router.afterEach(function () {
58+
// https://github.com/MatteoGabriele/vue-analytics/issues/44
4959
setTimeout(function () {
50-
// https://github.com/MatteoGabriele/vue-analytics/issues/44
51-
trackRoute(autoTracking.pageviewTemplate, router)
60+
trackRoute(router.currentRoute)
5261
}, 0)
5362
})
5463
}

0 commit comments

Comments
 (0)