Skip to content

Commit 32f9c15

Browse files
authored
docs: refactor analytics + remove segment (#13954)
* docs: refactor analytics + remove segment * small refactor of condition
1 parent baec64d commit 32f9c15

File tree

11 files changed

+146
-129
lines changed

11 files changed

+146
-129
lines changed

www/apps/api-reference/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ type ProvidersProps = {
1616

1717
const Providers = ({ children }: ProvidersProps) => {
1818
return (
19-
<AnalyticsProvider
20-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
21-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
22-
>
19+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
2320
<SiteConfigProvider config={config}>
2421
<PageLoadingProvider>
2522
<ScrollControllerProvider scrollableSelector="#main">

www/apps/book/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ type ProvidersProps = {
2323

2424
const Providers = ({ children, aiAssistantProps = {} }: ProvidersProps) => {
2525
return (
26-
<AnalyticsProvider
27-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
28-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
29-
>
26+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
3027
<SiteConfigProvider config={config}>
3128
<LearningPathProvider>
3229
<NotificationProvider>

www/apps/cloud/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ type ProvidersProps = {
2424

2525
const Providers = ({ children }: ProvidersProps) => {
2626
return (
27-
<AnalyticsProvider
28-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
29-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
30-
>
27+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
3128
<SiteConfigProvider config={config}>
3229
<MobileProvider>
3330
<ColorModeProvider>

www/apps/resources/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ type ProvidersProps = {
2121

2222
const Providers = ({ children }: ProvidersProps) => {
2323
return (
24-
<AnalyticsProvider
25-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
26-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
27-
>
24+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
2825
<SiteConfigProvider config={config}>
2926
<LearningPathProvider
3027
baseUrl={process.env.NEXT_PUBLIC_BASE_PATH || "/resources"}

www/apps/ui/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ type ProvidersProps = {
2525

2626
const Providers = ({ children }: ProvidersProps) => {
2727
return (
28-
<AnalyticsProvider
29-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
30-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
31-
>
28+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
3229
<SiteConfigProvider config={config}>
3330
<MobileProvider>
3431
<ColorModeProvider>

www/apps/user-guide/providers/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ type ProvidersProps = {
2424

2525
const Providers = ({ children }: ProvidersProps) => {
2626
return (
27-
<AnalyticsProvider
28-
segmentWriteKey={process.env.NEXT_PUBLIC_SEGMENT_API_KEY}
29-
reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}
30-
>
27+
<AnalyticsProvider reoDevKey={process.env.NEXT_PUBLIC_REO_DEV_CLIENT_ID}>
3128
<SiteConfigProvider config={config}>
3229
<MobileProvider>
3330
<ColorModeProvider>

www/packages/docs-ui/src/components/Feedback/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ export const Feedback = ({
9090
: showForm
9191
? inlineQuestionRef
9292
: inlineFeedbackRef
93-
const { loaded, track } = useAnalytics()
93+
const { track } = useAnalytics()
9494

9595
function handleFeedback(feedback: boolean) {
96-
if (!loaded) {
97-
return
98-
}
9996
setPositiveFeedback(feedback)
10097
setShowForm(true)
10198
submitFeedback(feedback)

www/packages/docs-ui/src/providers/Analytics/index.tsx

Lines changed: 18 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ import React, {
77
useEffect,
88
useState,
99
} from "react"
10-
import { Analytics, AnalyticsBrowser } from "@segment/analytics-next"
11-
import posthog from "posthog-js"
12-
13-
// @ts-expect-error Doesn't have a types package
14-
import { loadReoScript } from "reodotdev"
10+
import { useSegmentAnalytics } from "./providers/segment"
11+
import { usePostHogAnalytics } from "./providers/posthog"
12+
import { useReoDevAnalytics } from "./providers/reo-dev"
1513

1614
export type ExtraData = {
1715
section?: string
18-
[key: string]: any
16+
[key: string]: unknown
1917
}
2018

2119
export type AnalyticsContextType = {
22-
loaded: boolean
23-
analytics: Analytics | null
2420
track: ({
2521
event,
2622
instant,
@@ -34,7 +30,7 @@ type Trackers = "segment" | "posthog"
3430

3531
export type TrackedEvent = {
3632
event: string
37-
options?: Record<string, any>
33+
options?: Record<string, unknown>
3834
callback?: () => void
3935
tracker?: Trackers | Trackers[]
4036
}
@@ -47,73 +43,18 @@ export type AnalyticsProviderProps = {
4743
children?: React.ReactNode
4844
}
4945

50-
const LOCAL_STORAGE_KEY = "ajs_anonymous_id"
51-
5246
export const AnalyticsProvider = ({
53-
segmentWriteKey = "temp",
47+
segmentWriteKey,
5448
reoDevKey,
5549
children,
5650
}: AnalyticsProviderProps) => {
57-
// loaded is used to ensure that a connection has been made to segment
58-
// even if it failed. This is to ensure that the connection isn't
59-
// continuously retried
60-
const [loaded, setLoaded] = useState<boolean>(false)
61-
const [analytics, setAnalytics] = useState<Analytics | null>(null)
62-
const analyticsBrowser = new AnalyticsBrowser()
63-
const [queue, setQueue] = useState<TrackedEvent[]>([])
64-
65-
const initSegment = useCallback(() => {
66-
if (!loaded) {
67-
analyticsBrowser
68-
.load(
69-
{ writeKey: segmentWriteKey },
70-
{
71-
initialPageview: true,
72-
user: {
73-
localStorage: {
74-
key: LOCAL_STORAGE_KEY,
75-
},
76-
},
77-
}
78-
)
79-
.then((instance) => {
80-
setAnalytics(instance[0])
81-
})
82-
.catch((e) =>
83-
console.error(`Could not connect to Segment. Error: ${e}`)
84-
)
85-
.finally(() => setLoaded(true))
86-
}
87-
}, [loaded, segmentWriteKey])
88-
89-
const trackWithSegment = useCallback(
90-
async ({ event, options }: TrackedEvent) => {
91-
if (analytics) {
92-
void analytics.track(event, {
93-
...options,
94-
uuid: analytics.user().anonymousId(),
95-
})
96-
} else {
97-
// push the event into the queue
98-
setQueue((prevQueue) => [
99-
...prevQueue,
100-
{
101-
event,
102-
options,
103-
tracker: "segment",
104-
},
105-
])
106-
console.warn(
107-
"Segment is either not installed or not configured. Simulating success..."
108-
)
109-
}
110-
},
111-
[analytics, loaded]
112-
)
113-
114-
const trackWithPostHog = async ({ event, options }: TrackedEvent) => {
115-
posthog.capture(event, options)
116-
}
51+
const [eventsQueue, setEventsQueue] = useState<TrackedEvent[]>([])
52+
const { track: trackWithSegment } = useSegmentAnalytics({
53+
segmentWriteKey,
54+
setEventsQueue,
55+
})
56+
const { track: trackWithPostHog } = usePostHogAnalytics()
57+
useReoDevAnalytics({ reoDevKey })
11758

11859
const processEvent = useCallback(
11960
async (event: TrackedEvent) => {
@@ -137,22 +78,18 @@ export const AnalyticsProvider = ({
13778

13879
const track = ({ event }: { event: TrackedEvent }) => {
13980
// Always queue events - this makes tracking non-blocking
140-
setQueue((prevQueue) => [...prevQueue, event])
81+
setEventsQueue((prevQueue) => [...prevQueue, event])
14182

14283
// Process event callback immediately
14384
// This ensures that the callback is called even if the event is queued
14485
event.callback?.()
14586
}
14687

14788
useEffect(() => {
148-
initSegment()
149-
}, [initSegment])
150-
151-
useEffect(() => {
152-
if (analytics && queue.length) {
89+
if (eventsQueue.length) {
15390
// Process queue in background without blocking
154-
const currentQueue = [...queue]
155-
setQueue([])
91+
const currentQueue = [...eventsQueue]
92+
setEventsQueue([])
15693

15794
// Process events asynchronously in batches to avoid overwhelming the system
15895
const batchSize = 5
@@ -163,32 +100,12 @@ export const AnalyticsProvider = ({
163100
}, i * 10) // Small delay between batches
164101
}
165102
}
166-
}, [analytics, queue, trackWithSegment, trackWithPostHog, processEvent])
167-
168-
useEffect(() => {
169-
if (!reoDevKey) {
170-
return
171-
}
172-
173-
loadReoScript({
174-
clientID: reoDevKey,
175-
})
176-
.then((Reo: unknown) => {
177-
;(Reo as { init: (config: { clientID: string }) => void }).init({
178-
clientID: reoDevKey,
179-
})
180-
})
181-
.catch((e: Error) => {
182-
console.error(`Could not connect to Reodotdev. Error: ${e}`)
183-
})
184-
}, [reoDevKey])
103+
}, [eventsQueue, processEvent])
185104

186105
return (
187106
<AnalyticsContext.Provider
188107
value={{
189-
analytics,
190108
track,
191-
loaded,
192109
}}
193110
>
194111
{children}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import posthog from "posthog-js"
2+
import { TrackedEvent } from ".."
3+
4+
export const usePostHogAnalytics = () => {
5+
const track = async ({ event, options }: TrackedEvent) => {
6+
posthog.capture(event, options)
7+
}
8+
9+
return {
10+
track,
11+
}
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect } from "react"
2+
// @ts-expect-error Doesn't have a types package
3+
import { loadReoScript } from "reodotdev"
4+
5+
type UseReoDevAnalyticsProps = {
6+
reoDevKey: string | undefined
7+
}
8+
9+
export const useReoDevAnalytics = ({ reoDevKey }: UseReoDevAnalyticsProps) => {
10+
useEffect(() => {
11+
if (!reoDevKey) {
12+
return
13+
}
14+
15+
loadReoScript({
16+
clientID: reoDevKey,
17+
})
18+
.then((Reo: unknown) => {
19+
;(Reo as { init: (config: { clientID: string }) => void }).init({
20+
clientID: reoDevKey,
21+
})
22+
})
23+
.catch((e: Error) => {
24+
console.error(`Could not connect to Reodotdev. Error: ${e}`)
25+
})
26+
}, [reoDevKey])
27+
}

0 commit comments

Comments
 (0)