Track landing page view before router redirect
See original GitHub issueIn our app we have router redirect to sign in page if user’s auth token has expired. If we want additionally track UTM parameters (utm_source, utm_medium, etc), we pass it in the URL. For example:
/dashboard?utm_source=push&utm_medium=welcome
If a user is signed in, the VueGtag takes this URL with utm_* params and tracks them correctly. If a user is not signed in and we redirect it immediately to /sign-up, and it tracks the latter URL. Probably to the race condition.
Of course possible solutions are:
- Pass utm_* params to redirected URL. However there is not the only such case and dragging querystring through all redirects is a bit ugly in a large app.
- Wait for the tracking event to happen and redirect then. We tried:
Vue.use(VueGtag, {
config: { id: "..." },
onAfterTrack () {
redirectNow()
}
}, router)
But for some reason onAfterTrack() is never called (vue-gtag version 1.16.1). Moreover it’s not clear if it is called always. For example when adblocker is active, or what if there is a network latency or Analytics service down.
If there any recommendation how to handle such race conditions in a best way or do we miss something?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (1 by maintainers)

Top Related StackOverflow Question
@ipa1981 in case you still want to solve this issue I worked on a third-party library (https://github.com/sneko/utm-synapse) that I integrated into
vue-gtag. Please see https://github.com/MatteoGabriele/vue-gtag/pull/445 for details 👍PS: about your wondering on
onAfterTrack(), this one is not called for the first track, just for subsequent ones (see https://github.com/MatteoGabriele/vue-gtag/blob/8c2e39555fb867a38251ca48c6011256603df69d/src/add-routes-tracker.js#L18-L47).Hi,
@MatteoGabriele is there any reason why the hooks
onBeforeTrack()andonAfterTrack()are not called on the landing page (first track)? They could be called at least with the current route, liketrack.