mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-25 19:15:18 +02:00
feat: Enhance PostHog integration with referral tracking and error handling
- Added referral code tracking in PostHog initialization to capture initial and last seen referral codes. - Wrapped PostHog capture calls in a safeCapture function to prevent app breakage due to ad-blockers. - Introduced PostHogReferral component in PostHogProvider for improved referral tracking.
This commit is contained in:
parent
88201d0209
commit
5e28125090
4 changed files with 156 additions and 80 deletions
|
|
@ -5,17 +5,17 @@ import posthog from "posthog-js";
|
|||
import type { ReactNode } from "react";
|
||||
import "../../instrumentation-client";
|
||||
import { PostHogIdentify } from "./PostHogIdentify";
|
||||
import { PostHogReferral } from "./PostHogReferral";
|
||||
|
||||
interface PostHogProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function PostHogProvider({ children }: PostHogProviderProps) {
|
||||
// posthog-js is initialized by importing instrumentation-client.ts above
|
||||
// We wrap the app with the PostHogProvider for hook access
|
||||
return (
|
||||
<PHProvider client={posthog}>
|
||||
<PostHogIdentify />
|
||||
<PostHogReferral />
|
||||
{children}
|
||||
</PHProvider>
|
||||
);
|
||||
|
|
|
|||
34
surfsense_web/components/providers/PostHogReferral.tsx
Normal file
34
surfsense_web/components/providers/PostHogReferral.tsx
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { trackReferralLanding } from "@/lib/posthog/events";
|
||||
|
||||
const REF_STORAGE_KEY = "surfsense_ref_code";
|
||||
|
||||
/**
|
||||
* Captures the ?ref=<code> URL parameter on first landing and fires a
|
||||
* PostHog event so marketing campaigns can be attributed.
|
||||
*
|
||||
* The ref code is persisted to sessionStorage so it survives client-side
|
||||
* navigations that strip query params (e.g. login redirect), but a fresh
|
||||
* event is fired for each new browser session with a ref param.
|
||||
*/
|
||||
export function PostHogReferral() {
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const ref = params.get("ref");
|
||||
|
||||
if (ref) {
|
||||
try {
|
||||
sessionStorage.setItem(REF_STORAGE_KEY, ref);
|
||||
} catch {
|
||||
// Private browsing may block sessionStorage
|
||||
}
|
||||
trackReferralLanding(ref, window.location.href);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue