From 6133610d087019a9b5a1e523a3cea9b272099831 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Thu, 29 Sep 2022 17:24:42 +0300 Subject: [PATCH] feat(frontend): tracking visited routes (#1055) --- packages/frontend/src/main/router/index.js | 10 ++++++++++ packages/frontend/src/mixpanelManager.ts | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/frontend/src/main/router/index.js b/packages/frontend/src/main/router/index.js index e4e5b2f2d..1f43c4f84 100644 --- a/packages/frontend/src/main/router/index.js +++ b/packages/frontend/src/main/router/index.js @@ -3,6 +3,7 @@ import { AppLocalStorage } from '@/utils/localStorage' import { GlobalEvents } from '@/main/lib/core/helpers/eventHubHelper' import Vue from 'vue' import VueRouter from 'vue-router' +import { getMixpanel } from '@/mixpanelManager' Vue.use(VueRouter) @@ -398,6 +399,15 @@ router.afterEach((to) => { Vue.nextTick(() => { document.title = (to.meta && to.meta.title) || 'Speckle' }) + + // Report route to mixpanel + const mp = getMixpanel() + const pathDefinition = to.matched[to.matched.length - 1].path + const path = to.path + mp.track('Route Visited', { + path, + pathDefinition + }) }) export default router diff --git a/packages/frontend/src/mixpanelManager.ts b/packages/frontend/src/mixpanelManager.ts index 1c14e187a..66c5b96fb 100644 --- a/packages/frontend/src/mixpanelManager.ts +++ b/packages/frontend/src/mixpanelManager.ts @@ -5,6 +5,8 @@ import { AppLocalStorage } from '@/utils/localStorage' import md5 from '@/helpers/md5' import * as ThemeStateManager from '@/main/utils/themeStateManager' +let mixpanelInitialized = false + /** * Get mixpanel user ID, if user is authenticated and can be identified, or undefined otherwise */ @@ -23,6 +25,10 @@ export function getMixpanelServerId(): string { * Get current mixpanel instance */ export function getMixpanel(): OverridedMixpanel { + if (!mixpanelInitialized) { + throw new Error('Attempting to use uninitialized mixpanel instance') + } + return mixpanel } @@ -33,7 +39,7 @@ export function initialize(params: { hostApp: string hostAppDisplayName: string }): void { - const mp = getMixpanel() + const mp = mixpanel const { hostApp, hostAppDisplayName } = params // Register session @@ -52,4 +58,6 @@ export function initialize(params: { // Track app visit mp.track(`Visit ${hostAppDisplayName}`) + + mixpanelInitialized = true }