Remove usage of sessionStorage in matomoClient

This commit is contained in:
Daniel Egger 2023-04-05 12:28:30 +02:00
parent b27e13fa12
commit be432bb70f
3 changed files with 22 additions and 32 deletions

View File

@ -4,46 +4,42 @@ import {
CHAPTER_TITLE_TYPE, CHAPTER_TITLE_TYPE,
CONTENT_TYPE, CONTENT_TYPE,
OBJECTIVE_GROUP_TYPE, OBJECTIVE_GROUP_TYPE,
OBJECTIVE_TYPE OBJECTIVE_TYPE,
} from '@/consts/types'; } from '@/consts/types';
const matomoLastUrlKey = 'matomoLastUrl'; export type matomoUserRole = 'Student' | 'Teacher';
const matomoLastUserIdKey = 'matomoLastUserId';
const matomoLastEventDataKey = 'matomoLastEventData';
let matomoLastUrl = '';
let matomoLastUserId = '';
let matomoLastEventData = '';
export function matomoTrackPageView(absolutePath: string = '/', title?: string) { export function matomoTrackPageView(absolutePath: string = '/', title?: string) {
// see https://developer.matomo.org/guides/spa-tracking for details // see https://developer.matomo.org/guides/spa-tracking for details
const url = window.location.origin + absolutePath; const url = window.location.origin + absolutePath;
const lastUrl = window.sessionStorage.getItem(matomoLastUrlKey);
if (lastUrl !== url) { if (matomoLastUrl !== url) {
log.debug('trackMatomoPageView', {url, lastUrl, title}); // do not track the same url twice
log.debug('trackMatomoPageView', { url, matomoLastUrl, title });
// @ts-ignore // @ts-ignore
window._paq.push(['setCustomUrl', url]); window._paq.push(['setCustomUrl', url]);
// @ts-ignore // @ts-ignore
window._paq.push(['trackPageView', title]); window._paq.push(['trackPageView', title]);
window.sessionStorage.setItem(matomoLastUrlKey, url); matomoLastUrl = url;
} else {
// do not track the same url twice
} }
} }
export function matomoTrackEvent(category: string, action: string, name?: string, value?: number) { export function matomoTrackEvent(category: string, action: string, name?: string, value?: number) {
const data = {category, action, name, value}; const data = { category, action, name, value };
const dataJSON = JSON.stringify(data); const dataJSON = JSON.stringify(data);
const lastEventData = window.sessionStorage.getItem(matomoLastEventDataKey);
if (lastEventData !== dataJSON) { if (matomoLastEventData !== dataJSON) {
log.debug('trackEvent', {category, action, name, value}); log.debug('matomoTrackEvent', { category, action, name, value });
// @ts-ignore // @ts-ignore
window._paq.push(['trackEvent', category, action, name, value]); window._paq.push(['trackEvent', category, action, name, value]);
window.sessionStorage.setItem(matomoLastEventDataKey, dataJSON); matomoLastEventData = dataJSON;
} }
} }
export function matomoTrackModuleVisibilityEvent(blockType: string, block: any, hidden: boolean) { export function matomoTrackModuleVisibilityEvent(blockType: string, block: any, hidden: boolean) {
let eventAction = ''; let eventAction = '';
let eventName = undefined; let eventName = undefined;
@ -74,13 +70,10 @@ export function matomoTrackModuleVisibilityEvent(blockType: string, block: any,
matomoTrackEvent('Modul Anpassen - Sichtbarkeit', eventAction, eventName); matomoTrackEvent('Modul Anpassen - Sichtbarkeit', eventAction, eventName);
} }
export function matomoTrackUser(userId: string, role: matomoUserRole = 'Student') {
export function matomoTrackUser(userId: string, role: "Student" | "Teacher" = 'Student') {
// see https://developer.matomo.org/guides/tracking-javascript-guide#user-id for the process // see https://developer.matomo.org/guides/tracking-javascript-guide#user-id for the process
const lastUserId = window.sessionStorage.getItem(matomoLastUserIdKey); if (matomoLastUserId !== userId) {
log.debug('matomoTrackUser', { userId, matomoLastUserId, role });
if (lastUserId !== userId) {
log.debug('trackUser', {userId, lastUserId, role});
if (userId) { if (userId) {
// @ts-ignore // @ts-ignore
@ -89,7 +82,7 @@ export function matomoTrackUser(userId: string, role: "Student" | "Teacher" = 'S
window._paq.push(['setCustomDimension', 1, role]); window._paq.push(['setCustomDimension', 1, role]);
} else { } else {
// @ts-ignore // @ts-ignore
window._paq.push(['deleteCustomDimension', 1]) window._paq.push(['deleteCustomDimension', 1]);
// User has just logged out, we reset the User ID // User has just logged out, we reset the User ID
// @ts-ignore // @ts-ignore
window._paq.push(['resetUserId']); window._paq.push(['resetUserId']);
@ -103,6 +96,6 @@ export function matomoTrackUser(userId: string, role: "Student" | "Teacher" = 'S
window._paq.push(['appendToTrackingUrl', '']); window._paq.push(['appendToTrackingUrl', '']);
} }
window.sessionStorage.setItem(matomoLastUserIdKey, userId || ''); matomoLastUserId = userId;
} }
} }

View File

@ -1,7 +1,7 @@
import ME_QUERY from '@/graphql/gql/queries/meQuery.gql'; import ME_QUERY from '@/graphql/gql/queries/meQuery.gql';
import { computed } from 'vue'; import { computed } from 'vue';
import { useQuery } from '@vue/apollo-composable'; import { useQuery } from '@vue/apollo-composable';
import {matomoTrackPageView, matomoTrackUser} from '@/helpers/matomo-client'; import { matomoTrackPageView, matomoTrackUser } from '@/helpers/matomo-client';
export interface Me { export interface Me {
selectedClass: { selectedClass: {
@ -111,7 +111,6 @@ const meMixin = {
return getTopicRoute(this.$data.me); return getTopicRoute(this.$data.me);
}, },
schoolClass(): any { schoolClass(): any {
// console.log('schoolClass legacy');
// @ts-ignore // @ts-ignore
return getSelectedClass(this.$data.me); return getSelectedClass(this.$data.me);
}, },

View File

@ -1,5 +1,4 @@
import { createRouter, createWebHistory } from 'vue-router'; import { createRouter, createWebHistory } from 'vue-router';
import log from 'loglevel';
import moduleRoutes from './module.routes'; import moduleRoutes from './module.routes';
import portfolioRoutes from './portfolio.routes'; import portfolioRoutes from './portfolio.routes';
@ -66,7 +65,7 @@ const routes = [
path: '/instrument/:slug', path: '/instrument/:slug',
name: 'instrument', name: 'instrument',
component: instrument, component: instrument,
meta: { layout: LAYOUT_SIMPLE, matomoUrlCallback: (to) => `/instrument/${to.params.slug}` } meta: { layout: LAYOUT_SIMPLE, matomoUrlCallback: (to) => `/instrument/${to.params.slug}` },
}, },
{ path: '/submission/:id', name: 'submission', component: submission, meta: { layout: LAYOUT_SIMPLE } }, { path: '/submission/:id', name: 'submission', component: submission, meta: { layout: LAYOUT_SIMPLE } },
{ {
@ -80,7 +79,8 @@ const routes = [
path: '/join-class', path: '/join-class',
name: 'join-class', name: 'join-class',
component: joinClass, component: joinClass,
meta: { layout: LAYOUT_SIMPLE, matomoUrl: '/join-class' } }, meta: { layout: LAYOUT_SIMPLE, matomoUrl: '/join-class' },
},
{ {
path: '/survey/:id', path: '/survey/:id',
component: surveyPage, component: surveyPage,
@ -148,8 +148,6 @@ router.afterEach(() => {
}); });
router.afterEach((to) => { router.afterEach((to) => {
log.debug('matomo router.afterEach', to);
if (to.meta.matomoUrl) { if (to.meta.matomoUrl) {
matomoTrackPageView(to.meta.matomoUrl); matomoTrackPageView(to.meta.matomoUrl);
} }