From cc858f7da658749469ef2e2c093e6d3f966f95f5 Mon Sep 17 00:00:00 2001 From: Matteo Gabriele Date: Sat, 28 Dec 2019 14:45:37 +0100 Subject: [PATCH] fix(page): pageview resolved before routes ready closes #267 --- src/lib/page.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/lib/page.js b/src/lib/page.js index e61ce6f..59d4f70 100644 --- a/src/lib/page.js +++ b/src/lib/page.js @@ -85,28 +85,26 @@ export function autoTracking () { return } - if (autoTracking.pageviewOnLoad && router.history.ready) { - trackRoute(router.currentRoute) - } + router.onReady(() => { + if (autoTracking.pageviewOnLoad && router.history.ready) { + trackRoute(router.currentRoute) + } - config.router.afterEach(function (to, from) { - const { skipSamePath, shouldRouterUpdate } = autoTracking + router.afterEach(function (to, from) { + const { skipSamePath, shouldRouterUpdate } = autoTracking - // Default behaviour of the router when the `skipSamePath` is turned on. - // Skip router change when current and previous route have the same path - // https://github.com/MatteoGabriele/vue-analytics/issues/73 - if (skipSamePath && to.path === from.path) { - return - } + // Default behaviour of the router when the `skipSamePath` is turned on. + // Skip router change when current and previous route have the same path + // https://github.com/MatteoGabriele/vue-analytics/issues/73 + if (skipSamePath && to.path === from.path) { + return + } - // Adds a custom way to define when the router should track - if (typeof shouldRouterUpdate === 'function' && !shouldRouterUpdate(to, from)) { - return - } + // Adds a custom way to define when the router should track + if (typeof shouldRouterUpdate === 'function' && !shouldRouterUpdate(to, from)) { + return + } - // Fire tracking after the nextTick or it will still register the previous route - // https://github.com/MatteoGabriele/vue-analytics/issues/44 - config.$vue.nextTick().then(() => { trackRoute(router.currentRoute) }) })