-
Notifications
You must be signed in to change notification settings - Fork 386
/
analyticsStore.ts
86 lines (71 loc) · 2.94 KB
/
analyticsStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { AnalyticsHttpApi } from '@/api/analytics';
import { AnalyticsErrorEventSource, AnalyticsEvent } from '@/utils/constants/analyticsEventNames';
type Plausible = (event: 'pageview', data: { u: string; props: { source: string } }) => void;
export const useAnalyticsStore = defineStore('analytics', () => {
const analytics: AnalyticsHttpApi = new AnalyticsHttpApi();
const plausible = ref<Plausible>();
async function loadPlausible(data: {
domain: string;
scriptURL: string;
}) {
try {
await new Promise((resolve, reject) => {
const head = document.head || document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = data.scriptURL;
script.setAttribute('data-domain', data.domain);
head.appendChild(script);
script.onload = resolve;
script.onerror = reject;
});
plausible.value = window['plausible'];
} catch (_) { /*empty*/ }
}
function eventTriggered(eventName: AnalyticsEvent, props?: Map<string, string>): void {
analytics.eventTriggered(eventName, props).catch(_ => { });
}
function linkEventTriggered(eventName: AnalyticsEvent, link: string): void {
analytics.linkEventTriggered(eventName, link).catch(_ => { });
}
function pageVisit(pagePath: string, source: string): void {
analytics.pageVisit(pagePath).catch(_ => { });
if (!plausible.value) {
return;
}
let url: string;
if (pagePath.includes('http')) {
// external link
url = pagePath;
} else {
url = window.location.protocol + '//' + window.location.host + pagePath;
if (window.location.search) {
// remove sensitive query params
const avoidKeys = ['token', 'email', 'inviter_email', 'projectID'];
const filteredParams: string[] = [];
const params = window.location.search.replace('?', '').split('&');
for (const param of params) {
if (!avoidKeys.find((k) => param.includes(k))) {
filteredParams.push(param);
}
}
if (filteredParams.length > 0) url = url + '?' + filteredParams.join('&');
}
}
plausible.value('pageview', { u: url, props: { source } });
}
function errorEventTriggered(source: AnalyticsErrorEventSource): void {
analytics.errorEventTriggered(source).catch(_ => { });
}
return {
loadPlausible,
eventTriggered,
errorEventTriggered,
linkEventTriggered,
pageVisit,
};
});