Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crazy-suits-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/use-analytics": patch
---

add a timeout option to set analytics in case of silent failure
23 changes: 22 additions & 1 deletion packages/use-analytics/src/analytics/useAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RudderAnalytics } from '@rudderstack/analytics-js'
import type { LoadOptions } from '@rudderstack/analytics-js'
import { createContext, useContext, useMemo, useState } from 'react'
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
import type { ReactNode } from 'react'
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
Expand Down Expand Up @@ -51,6 +51,7 @@
settings?: {
writeKey: string
cdnURL: string
timeout: number
}
loadOptions?: LoadOptions

Expand Down Expand Up @@ -92,6 +93,26 @@
undefined,
)

// 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.
useEffect(() => {
let timer: ReturnType<typeof setTimeout> | undefined
if (!isAnalyticsReady && !internalAnalytics && settings?.timeout) {
if (shouldRenderOnlyWhenReady) {
timer = setTimeout(() => setIsAnalyticsReady(true), settings.timeout)
}
}

return () => {
clearTimeout(timer)
}
}, [
isAnalyticsReady,
internalAnalytics,
setIsAnalyticsReady,
shouldRenderOnlyWhenReady,
settings?.timeout,
])

const shouldLoad = useMemo(() => {
if (needConsent) {
return false
Expand Down Expand Up @@ -130,7 +151,7 @@

analytics.ready(() => {
// @ts-expect-error tracklink is added to the analytics setup to simplify migration from segment, should be remove.
setAnalytics({ ...analytics, trackLink: trackLink(analytics) })

Check warning on line 154 in packages/use-analytics/src/analytics/useAnalytics.tsx

View workflow job for this annotation

GitHub Actions / lint

`trackLink` is deprecated. this function is a wrapper of a Track to facilitate the migration from segment to rudderstack
setIsAnalyticsReady(true)
})
}
Expand Down
Loading