Skip to content

Commit ffc89b6

Browse files
committed
fix(analytics): add timeout option
1 parent 2ddefbb commit ffc89b6

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

packages/use-analytics/src/analytics/useAnalytics.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
77
import type { CategoryKind } from '../types'
88
import { defaultConsentOptions, defaultLoadOptions } from './constants'
9-
import { trackLink } from './segments/trackLink'
10-
import type { TrackLink } from './segments/trackLink'
119
import { userMigrationsTraits } from './segments/userMigrationsTraits'
1210

13-
type Analytics = RudderAnalytics & {
14-
trackLink: TrackLink
15-
}
16-
11+
type Analytics = RudderAnalytics
1712
export type { Analytics }
1813

1914
export type OnEventError = (error: Error) => Promise<void> | void
@@ -51,7 +46,6 @@ export type AnalyticsProviderProps<T> = {
5146
settings?: {
5247
writeKey: string
5348
cdnURL: string
54-
timeout: number
5549
}
5650
loadOptions?: LoadOptions
5751

@@ -60,9 +54,10 @@ export type AnalyticsProviderProps<T> = {
6054
*/
6155
shouldRenderOnlyWhenReady?: boolean
6256
/**
63-
* used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time.
57+
* used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time. You can also set a timeout to prevent blocking indefinitely.
6458
*/
6559
needConsent?: boolean
60+
timeout?: number
6661
allowedConsents: CategoryKind[]
6762
deniedConsents: CategoryKind[]
6863
onError?: (err: Error) => void
@@ -87,6 +82,7 @@ export function AnalyticsProvider<T extends Events>({
8782
deniedConsents,
8883
events,
8984
onLoaded,
85+
timeout,
9086
}: AnalyticsProviderProps<T>) {
9187
const [isAnalyticsReady, setIsAnalyticsReady] = useState(false)
9288
const [internalAnalytics, setAnalytics] = useState<Analytics | undefined>(
@@ -96,9 +92,10 @@ export function AnalyticsProvider<T extends Events>({
9692
// This effect will unlock the case where we have a failure with the load of the analytics.load as rudderstack doesn't provider any solution for this case.
9793
useEffect(() => {
9894
let timer: ReturnType<typeof setTimeout> | undefined
99-
if (!isAnalyticsReady && !internalAnalytics && settings?.timeout) {
95+
if (!isAnalyticsReady && !internalAnalytics && timeout) {
10096
if (shouldRenderOnlyWhenReady) {
101-
timer = setTimeout(() => setIsAnalyticsReady(true), settings.timeout)
97+
timer = setTimeout(() => setIsAnalyticsReady(true), timeout)
98+
onError?.(new Error('Analytics Setup Timeout'))
10299
}
103100
}
104101

@@ -110,7 +107,8 @@ export function AnalyticsProvider<T extends Events>({
110107
internalAnalytics,
111108
setIsAnalyticsReady,
112109
shouldRenderOnlyWhenReady,
113-
settings?.timeout,
110+
timeout,
111+
onError,
114112
])
115113

116114
const shouldLoad = useMemo(() => {
@@ -151,11 +149,11 @@ export function AnalyticsProvider<T extends Events>({
151149

152150
analytics.ready(() => {
153151
// @ts-expect-error tracklink is added to the analytics setup to simplify migration from segment, should be remove.
154-
setAnalytics({ ...analytics, trackLink: trackLink(analytics) })
152+
setAnalytics(analytics)
155153
setIsAnalyticsReady(true)
156154
})
157155
}
158-
}, [onError, settings, loadOptions, shouldLoad])
156+
}, [settings, loadOptions, shouldLoad])
159157

160158
const value = useMemo<AnalyticsContextInterface<T>>(() => {
161159
const curiedEvents = Object.entries(events).reduce(

0 commit comments

Comments
 (0)