Files
Folo/apps/desktop/layer/renderer/src/initialize/analytics.ts
T
Innei 0e441f6075 feat: add attribution tracking for analytics
- Introduced attribution tracking by capturing UTM parameters from the URL.
- Implemented functions to save and retrieve attribution data from local storage.
- Enhanced analytics initialization to include attribution data as user properties.

Signed-off-by: Innei <tukon479@gmail.com>
2025-11-25 01:37:28 +08:00

49 lines
1.4 KiB
TypeScript

import { env } from "@follow/shared/env.desktop"
import { setFirebaseTracker, setPostHogTracker, tracker } from "@follow/tracker"
import { captureAttributionFromURL, getAttributionForAnalytics } from "@follow/utils"
import type { AuthSessionResponse } from "@follow-app/client-sdk"
import posthog from "posthog-js"
import { QUERY_PERSIST_KEY } from "~/constants/app"
import { ga4 } from "../lib/ga4"
export const initAnalytics = async () => {
// Capture attribution data from URL (preserves first attribution)
captureAttributionFromURL()
// Get attribution data for analytics
const attributionData = getAttributionForAnalytics()
tracker.manager.appendUserProperties({
build: ELECTRON ? "electron" : "web",
version: APP_VERSION,
hash: GIT_COMMIT_SHA,
language: navigator.language,
...attributionData,
})
setFirebaseTracker(ga4)
setPostHogTracker(
posthog.init(env.VITE_POSTHOG_KEY, {
api_host: env.VITE_POSTHOG_HOST,
person_profiles: "identified_only",
defaults: "2025-05-24",
}),
)
let session: AuthSessionResponse | undefined
try {
const queryData = JSON.parse(window.localStorage.getItem(QUERY_PERSIST_KEY) ?? "{}")
session = queryData.clientState.queries.find(
(query: any) => query.queryHash === JSON.stringify(["auth", "session"]),
)?.state.data.data
} catch {
// do nothing
}
if (session?.user) {
tracker.identify(session.user)
}
}