diff --git a/src/lib/actions/analytics.ts b/src/lib/actions/analytics.ts index ca4f4667ea..46a0b11dd7 100644 --- a/src/lib/actions/analytics.ts +++ b/src/lib/actions/analytics.ts @@ -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,27 @@ 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, + label: event, + url: window.location.origin + path, + data: { + email, + name, + ...data + } + }) + }); +} + function isTrackingAllowed() { if (window.navigator?.doNotTrack) { if (navigator.doNotTrack === '1' || navigator.doNotTrack === 'yes') {