Skip to content

Commit 104d8f3

Browse files
committed
feat: enhance Mixpanel integration with improved tracking and initialization logic
1 parent 69b0bec commit 104d8f3

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
'use client';
1+
"use client"
22

3-
import { useEffect } from 'react';
4-
import { initMixpanel } from '@/lib/mixpanel';
3+
import { useEffect } from "react"
4+
import { usePathname, useSearchParams } from "next/navigation"
5+
6+
import { initMixpanel, trackPageView } from "@/lib/mixpanel"
57

68
export function MixpanelInit() {
7-
useEffect(() => {
8-
initMixpanel();
9-
}, []);
9+
const pathname = usePathname()
10+
const searchParams = useSearchParams()
11+
12+
useEffect(() => {
13+
initMixpanel()
14+
}, [])
15+
16+
useEffect(() => {
17+
if (!pathname) {
18+
return
19+
}
20+
21+
const search = searchParams?.toString()
22+
trackPageView(pathname, search ? `?${search}` : undefined)
23+
}, [pathname, searchParams])
1024

11-
return null; // This component doesn't render anything
12-
}
25+
return null
26+
}

lib/mixpanel.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
import mixpanel from "mixpanel-browser";
1+
import mixpanel from "mixpanel-browser"
22

3-
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN!;
3+
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN
4+
5+
let isMixpanelInitialized = false
46

57
export const initMixpanel = () => {
6-
if (typeof window !== "undefined") {
7-
mixpanel.init(MIXPANEL_TOKEN, { debug: process.env.NODE_ENV !== "production" });
8-
}
9-
};
8+
if (typeof window === "undefined" || isMixpanelInitialized) {
9+
return
10+
}
1011

11-
export const track = (event: string, props?: Record<string, any>) => {
12-
if (typeof window !== "undefined") {
13-
mixpanel.track(event, props);
12+
if (!MIXPANEL_TOKEN) {
13+
if (process.env.NODE_ENV !== "production") {
14+
console.warn("Mixpanel token is missing. Set NEXT_PUBLIC_MIXPANEL_TOKEN to enable analytics.")
1415
}
15-
};
16+
return
17+
}
18+
19+
mixpanel.init(MIXPANEL_TOKEN, {
20+
debug: process.env.NODE_ENV !== "production",
21+
ignore_dnt: true,
22+
})
23+
24+
isMixpanelInitialized = true
25+
}
26+
27+
export const track = (event: string, props?: Record<string, unknown>) => {
28+
if (typeof window === "undefined" || !isMixpanelInitialized) {
29+
return
30+
}
31+
32+
mixpanel.track(event, props)
33+
}
1634

1735
export const identify = (userId: string) => {
18-
if (typeof window !== "undefined") {
19-
mixpanel.identify(userId);
20-
}
21-
};
36+
if (typeof window === "undefined" || !isMixpanelInitialized) {
37+
return
38+
}
39+
40+
mixpanel.identify(userId)
41+
}
42+
43+
export const trackPageView = (path: string, search?: string) => {
44+
track("Page View", {
45+
path,
46+
search: search || undefined,
47+
})
48+
}

0 commit comments

Comments
 (0)