Skip to content

Commit 9e46551

Browse files
authored
fix(analytics): analytics interface (#2671)
Signed-off-by: protobuf-ci-cd <[email protected]>
1 parent e0a7159 commit 9e46551

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

.changeset/fine-eyes-unite.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+
remove use effect

packages/use-analytics/src/analytics/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const defaultConsentOptions: ConsentOptions = {
1111
allowedConsentIds: [],
1212
deniedConsentIds: [],
1313
},
14-
}
14+
} as const
1515

1616
export const defaultLoadOptions: LoadOptions = {
1717
logLevel: 'NONE',
@@ -58,4 +58,4 @@ export const defaultLoadOptions: LoadOptions = {
5858
autoTrack: true,
5959
timeout: 500,
6060
},
61-
}
61+
} as const

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

Lines changed: 4 additions & 22 deletions
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, useEffect, useMemo, useState } from 'react'
3+
import { createContext, useContext, useMemo, useState } from 'react'
44
import type { ReactNode } from 'react'
55
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
@@ -79,7 +79,6 @@ export function AnalyticsProvider<T extends Events>({
7979
settings,
8080
loadOptions,
8181
shouldRenderOnlyWhenReady = false,
82-
shouldLoadAnalytics = false,
8382
onError,
8483
onEventError,
8584
allowedConsents,
@@ -91,13 +90,7 @@ export function AnalyticsProvider<T extends Events>({
9190
undefined,
9291
)
9392

94-
const shouldLoad = useMemo(() => {
95-
if (shouldLoadAnalytics) {
96-
return !!settings?.writeKey
97-
}
98-
99-
return false
100-
}, [shouldLoadAnalytics, settings?.writeKey])
93+
const shouldLoad = useMemo(() => !!settings?.writeKey, [settings?.writeKey])
10194

10295
useDeepCompareEffectNoCheck(() => {
10396
if (shouldLoad && settings) {
@@ -129,22 +122,11 @@ export function AnalyticsProvider<T extends Events>({
129122
setAnalytics({ ...analytics, trackLink: trackLink(analytics) })
130123
setIsAnalyticsReady(true)
131124
})
132-
} else if (shouldLoadAnalytics && !shouldLoad) {
125+
} else if (!shouldLoad) {
133126
// When user has refused tracking, set ready anyway
134127
setIsAnalyticsReady(true)
135128
}
136-
}, [onError, settings, loadOptions, shouldLoad, shouldLoadAnalytics])
137-
138-
useEffect(() => {
139-
if (isAnalyticsReady) {
140-
internalAnalytics?.consent({
141-
consentManagement: {
142-
allowedConsentIds: allowedConsents,
143-
deniedConsentIds: deniedConsents,
144-
},
145-
})
146-
}
147-
}, [internalAnalytics, isAnalyticsReady, allowedConsents, deniedConsents])
129+
}, [onError, settings, loadOptions, shouldLoad])
148130

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

0 commit comments

Comments
 (0)