Skip to content

Commit b5326ba

Browse files
committed
feat: refactor Mixpanel initialization logic and improve global state handling
1 parent fa4b667 commit b5326ba

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

components/MixpanelInit/MixpanelInit.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import { useEffect } from "react"
44
import { usePathname, useSearchParams } from "next/navigation"
55

6-
import { initMixpanel, trackPageView } from "@/lib/mixpanel"
6+
import { trackPageView } from "@/lib/mixpanel"
77

88
export function MixpanelInit() {
99
const pathname = usePathname()
1010
const searchParams = useSearchParams()
1111

12-
useEffect(() => {
13-
initMixpanel()
14-
}, [])
15-
1612
useEffect(() => {
1713
if (!pathname) {
1814
return

lib/mixpanel.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import mixpanel from "mixpanel-browser"
22

33
import { ANALYTICS_EVENTS, type AnalyticsEvent } from "@/lib/analytics-events"
44

5-
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN
5+
declare global {
6+
interface Window {
7+
__mixpanel_initialized?: boolean
8+
}
9+
}
610

7-
let isMixpanelInitialized = false
11+
const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN
812

9-
export const initMixpanel = () => {
10-
if (typeof window === "undefined" || isMixpanelInitialized) {
13+
const initializeMixpanel = () => {
14+
if (typeof window === "undefined" || window.__mixpanel_initialized) {
1115
return
1216
}
1317

@@ -19,23 +23,57 @@ export const initMixpanel = () => {
1923
}
2024

2125
mixpanel.init(MIXPANEL_TOKEN, {
22-
debug: process.env.NODE_ENV !== "production",
23-
ignore_dnt: true,
26+
debug: false,
27+
track_pageview: false,
28+
persistence: "localStorage",
2429
})
2530

26-
isMixpanelInitialized = true
31+
window.__mixpanel_initialized = true
32+
mixpanel.track("Page Load", { path: window.location.pathname })
33+
}
34+
35+
if (typeof window !== "undefined") {
36+
initializeMixpanel()
2737
}
2838

39+
export const initMixpanel = initializeMixpanel
40+
2941
export const track = (event: AnalyticsEvent, props?: Record<string, unknown>) => {
30-
if (typeof window === "undefined" || !isMixpanelInitialized) {
42+
if (typeof window === "undefined") {
43+
return
44+
}
45+
46+
if (process.env.NODE_ENV === "development") {
47+
console.log("[mixpanel track]", event, props)
48+
return
49+
}
50+
51+
if (!window.__mixpanel_initialized) {
52+
initializeMixpanel()
53+
}
54+
55+
if (!window.__mixpanel_initialized) {
3156
return
3257
}
3358

3459
mixpanel.track(event, props)
3560
}
3661

3762
export const identify = (userId: string) => {
38-
if (typeof window === "undefined" || !isMixpanelInitialized) {
63+
if (typeof window === "undefined") {
64+
return
65+
}
66+
67+
if (process.env.NODE_ENV === "development") {
68+
console.log("[mixpanel identify]", userId)
69+
return
70+
}
71+
72+
if (!window.__mixpanel_initialized) {
73+
initializeMixpanel()
74+
}
75+
76+
if (!window.__mixpanel_initialized) {
3977
return
4078
}
4179

0 commit comments

Comments
 (0)