Skip to content

Commit bf7df55

Browse files
authored
feat(analytics): add timeout option to unblock in case of silent failure (thanks to Brave browser) (#2711)
Signed-off-by: protobuf-ci-cd <[email protected]>
1 parent 106068d commit bf7df55

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.changeset/crazy-suits-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/use-analytics": patch
3+
---
4+
5+
add a timeout option to set analytics in case of silent failure

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RudderAnalytics } from '@rudderstack/analytics-js'
22
import type { LoadOptions } from '@rudderstack/analytics-js'
3-
import { createContext, useContext, useMemo, useState } from 'react'
3+
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
44
import type { ReactNode } from 'react'
55
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
@@ -51,6 +51,7 @@ export type AnalyticsProviderProps<T> = {
5151
settings?: {
5252
writeKey: string
5353
cdnURL: string
54+
timeout: number
5455
}
5556
loadOptions?: LoadOptions
5657

@@ -92,6 +93,26 @@ export function AnalyticsProvider<T extends Events>({
9293
undefined,
9394
)
9495

96+
// 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.
97+
useEffect(() => {
98+
let timer: ReturnType<typeof setTimeout> | undefined
99+
if (!isAnalyticsReady && !internalAnalytics && settings?.timeout) {
100+
if (shouldRenderOnlyWhenReady) {
101+
timer = setTimeout(() => setIsAnalyticsReady(true), settings.timeout)
102+
}
103+
}
104+
105+
return () => {
106+
clearTimeout(timer)
107+
}
108+
}, [
109+
isAnalyticsReady,
110+
internalAnalytics,
111+
setIsAnalyticsReady,
112+
shouldRenderOnlyWhenReady,
113+
settings?.timeout,
114+
])
115+
95116
const shouldLoad = useMemo(() => {
96117
if (needConsent) {
97118
return false

0 commit comments

Comments
 (0)