feat: send events to growth

This commit is contained in:
Torsten Dittmann committed 2022-11-15 17:22:50 +01:00
1 parent d7491462f2
commit 3175c8c42f
1 file changed
+24 -2
+24 -2
View File
@@ -2,6 +2,7 @@ import Analytics from 'analytics';
import googleAnalytics from '@analytics/google-analytics';
import { get } from 'svelte/store';
import { page } from '$app/stores';
import { user } from '$lib/stores/user';
const analytics = Analytics({
app: 'appwrite',
@@ -16,8 +17,9 @@ export function trackEvent(name: string, data: object = null): void {
if (!isTrackingAllowed()) {
return;
}
analytics.track(name, { ...data, path: get(page).routeId });
const path = get(page).routeId;
analytics.track(name, { ...data, path });
sendEventToGrowth(name, path, data);
}
export function trackPageView(path: string) {
@@ -30,6 +32,26 @@ export function trackPageView(path: string) {
});
}
function sendEventToGrowth(event: string, path: string, data: object = null): void {
const { email, name } = get(user);
fetch('https://growth.appwrite.io/v1/analytics', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
action: event,
url: window.location.origin + path,
data: {
email,
name,
...data
}
})
});
}
function isTrackingAllowed() {
if (window.navigator?.doNotTrack) {
if (navigator.doNotTrack === '1' || navigator.doNotTrack === 'yes') {