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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@rudderstack/analytics-js": "catalog:",
"@scaleway/eslint-config-react": "workspace:*",
"@scaleway/tsconfig": "workspace:*",
"@scaleway/use-segment": "catalog:",
"@testing-library/jest-dom": "catalog:",
"@testing-library/react": "catalog:",
"@types/jest": "catalog:",
Expand Down
25 changes: 11 additions & 14 deletions packages/use-analytics/src/analytics/useAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
import type { CategoryKind } from '../types'
import { defaultConsentOptions, defaultLoadOptions } from './constants'
import { trackLink } from './segments/trackLink'
import type { TrackLink } from './segments/trackLink'
import { userMigrationsTraits } from './segments/userMigrationsTraits'

type Analytics = RudderAnalytics & {
trackLink: TrackLink
}

type Analytics = RudderAnalytics
export type { Analytics }

export type OnEventError = (error: Error) => Promise<void> | void
Expand Down Expand Up @@ -51,7 +46,6 @@ export type AnalyticsProviderProps<T> = {
settings?: {
writeKey: string
cdnURL: string
timeout: number
}
loadOptions?: LoadOptions

Expand All @@ -60,9 +54,10 @@ export type AnalyticsProviderProps<T> = {
*/
shouldRenderOnlyWhenReady?: boolean
/**
* used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time.
* used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time. You can also set a timeout to prevent blocking indefinitely.
*/
needConsent?: boolean
timeout?: number
allowedConsents: CategoryKind[]
deniedConsents: CategoryKind[]
onError?: (err: Error) => void
Expand All @@ -87,6 +82,7 @@ export function AnalyticsProvider<T extends Events>({
deniedConsents,
events,
onLoaded,
timeout,
}: AnalyticsProviderProps<T>) {
const [isAnalyticsReady, setIsAnalyticsReady] = useState(false)
const [internalAnalytics, setAnalytics] = useState<Analytics | undefined>(
Expand All @@ -96,9 +92,10 @@ export function AnalyticsProvider<T extends Events>({
// 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 (!isAnalyticsReady && !internalAnalytics && timeout) {
if (shouldRenderOnlyWhenReady) {
timer = setTimeout(() => setIsAnalyticsReady(true), settings.timeout)
timer = setTimeout(() => setIsAnalyticsReady(true), timeout)
onError?.(new Error('Analytics Setup Timeout'))
}
}

Expand All @@ -110,7 +107,8 @@ export function AnalyticsProvider<T extends Events>({
internalAnalytics,
setIsAnalyticsReady,
shouldRenderOnlyWhenReady,
settings?.timeout,
timeout,
onError,
])

const shouldLoad = useMemo(() => {
Expand Down Expand Up @@ -150,12 +148,11 @@ export function AnalyticsProvider<T extends Events>({
})

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) })
setAnalytics(analytics)
setIsAnalyticsReady(true)
})
}
}, [onError, settings, loadOptions, shouldLoad])
}, [settings, loadOptions, shouldLoad])

const value = useMemo<AnalyticsContextInterface<T>>(() => {
const curiedEvents = Object.entries(events).reduce(
Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ catalog:
'@formatjs/icu-messageformat-parser': 2.11.2
'@growthbook/growthbook-react': 1.6.0
'@rudderstack/analytics-js': 3.21.0
'@scaleway/use-segment': 1.1.11
'@segment/analytics-next': 1.81.1
'@stylistic/eslint-plugin': 4.4.1
'@testing-library/jest-dom': 6.6.3
Expand Down
Loading