Skip to content

Commit e4674ae

Browse files
authored
fix(analytics): add timeout option (#2716)
1 parent 2ddefbb commit e4674ae

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@rudderstack/analytics-js": "catalog:",
2727
"@scaleway/eslint-config-react": "workspace:*",
2828
"@scaleway/tsconfig": "workspace:*",
29+
"@scaleway/use-segment": "catalog:",
2930
"@testing-library/jest-dom": "catalog:",
3031
"@testing-library/react": "catalog:",
3132
"@types/jest": "catalog:",

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

Lines changed: 11 additions & 14 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(() => {
@@ -150,12 +148,11 @@ export function AnalyticsProvider<T extends Events>({
150148
})
151149

152150
analytics.ready(() => {
153-
// @ts-expect-error tracklink is added to the analytics setup to simplify migration from segment, should be remove.
154-
setAnalytics({ ...analytics, trackLink: trackLink(analytics) })
151+
setAnalytics(analytics)
155152
setIsAnalyticsReady(true)
156153
})
157154
}
158-
}, [onError, settings, loadOptions, shouldLoad])
155+
}, [settings, loadOptions, shouldLoad])
159156

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

pnpm-lock.yaml

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ catalog:
2121
'@formatjs/icu-messageformat-parser': 2.11.2
2222
'@growthbook/growthbook-react': 1.6.0
2323
'@rudderstack/analytics-js': 3.21.0
24+
'@scaleway/use-segment': 1.1.11
2425
'@segment/analytics-next': 1.81.1
2526
'@stylistic/eslint-plugin': 4.4.1
2627
'@testing-library/jest-dom': 6.6.3

0 commit comments

Comments
 (0)