Skip to content

Commit b2a4f52

Browse files
authored
fix(analytics): analytics props (#2674)
Signed-off-by: protobuf-ci-cd <[email protected]>
1 parent b56128a commit b2a4f52

File tree

8 files changed

+101
-48
lines changed

8 files changed

+101
-48
lines changed

.changeset/good-pears-help.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+
fix issue with destination load and naming

biome.json

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
{
22
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
3-
"organizeImports": { "enabled": false },
4-
"linter": { "enabled": false },
3+
"assist": { "actions": { "source": { "organizeImports": "off" } } },
4+
"linter": {
5+
"enabled": false,
6+
"rules": {
7+
"style": {
8+
"noParameterAssign": "error",
9+
"useAsConstAssertion": "error",
10+
"useDefaultParameterLast": "error",
11+
"useEnumInitializers": "error",
12+
"useSelfClosingElements": "error",
13+
"useSingleVarDeclarator": "error",
14+
"noUnusedTemplateLiteral": "error",
15+
"useNumberNamespace": "error",
16+
"noInferrableTypes": "error",
17+
"noUselessElse": "error"
18+
}
19+
}
20+
},
521
"vcs": {
622
"clientKind": "git",
723
"useIgnoreFile": true
@@ -14,17 +30,18 @@
1430
"lineEnding": "lf",
1531
"lineWidth": 80,
1632
"attributePosition": "auto",
17-
"ignore": [
18-
".next/",
19-
"coverage/",
20-
"**/node_modules/",
21-
"**/storybook-static",
22-
"**/dist/",
23-
"**/pnpm-lock.yaml",
24-
"**/package.json",
25-
"**/CHANGELOG.md",
26-
"*.snap",
27-
"**/__snapshots__/"
33+
"includes": [
34+
"**",
35+
"!**/.next/",
36+
"!**/coverage/",
37+
"!**/node_modules/",
38+
"!**/storybook-static",
39+
"!**/dist/",
40+
"!**/pnpm-lock.yaml",
41+
"!**/package.json",
42+
"!**/CHANGELOG.md",
43+
"!**/*.snap",
44+
"!**/__snapshots__/"
2845
]
2946
},
3047
"javascript": {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const defaultConsentOptions: ConsentOptions = {
44
trackConsent: true,
55
discardPreConsentEvents: true,
66
storage: {
7-
type: 'localStorage',
7+
type: 'cookieStorage',
88
},
99
consentManagement: {
1010
enabled: true,
@@ -47,7 +47,7 @@ export const defaultLoadOptions: LoadOptions = {
4747
integrations: {
4848
All: true,
4949
},
50-
loadIntegration: false,
50+
loadIntegration: true,
5151
secureCookie: true,
5252
anonymousIdOptions: {
5353
autoCapture: {

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ export type AnalyticsProviderProps<T> = {
5555
loadOptions?: LoadOptions
5656

5757
/**
58-
* This option help you in case you don't want to load analytics
58+
* This option force provider to render children only when isAnalytics is ready
5959
*/
60-
shouldLoadAnalytics?: boolean
60+
shouldRenderOnlyWhenReady?: boolean
6161
/**
62-
* // This option force provider to render children only when isAnalytics is ready
62+
* used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time.
6363
*/
64-
shouldRenderOnlyWhenReady?: boolean
64+
needConsent?: boolean
6565
allowedConsents: CategoryKind[]
6666
deniedConsents: CategoryKind[]
6767
onError?: (err: Error) => void
@@ -79,18 +79,26 @@ export function AnalyticsProvider<T extends Events>({
7979
settings,
8080
loadOptions,
8181
shouldRenderOnlyWhenReady = false,
82+
needConsent = true,
8283
onError,
8384
onEventError,
8485
allowedConsents,
8586
deniedConsents,
8687
events,
88+
onLoaded,
8789
}: AnalyticsProviderProps<T>) {
8890
const [isAnalyticsReady, setIsAnalyticsReady] = useState(false)
8991
const [internalAnalytics, setAnalytics] = useState<Analytics | undefined>(
9092
undefined,
9193
)
9294

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

95103
useDeepCompareEffectNoCheck(() => {
96104
if (shouldLoad && settings) {
@@ -112,6 +120,8 @@ export function AnalyticsProvider<T extends Events>({
112120
},
113121
})
114122

123+
onLoaded(rudderAnalytics)
124+
115125
setIsAnalyticsReady(true)
116126
},
117127
...loadOptions,
@@ -122,10 +132,11 @@ export function AnalyticsProvider<T extends Events>({
122132
setAnalytics({ ...analytics, trackLink: trackLink(analytics) })
123133
setIsAnalyticsReady(true)
124134
})
125-
} else if (!shouldLoad) {
126-
// When user has refused tracking, set ready anyway
127-
setIsAnalyticsReady(true)
128135
}
136+
// else if (!shouldLoad && !needConsent ) {
137+
// // When user has refused tracking, set ready anyway
138+
// setIsAnalyticsReady(true)
139+
// }
129140
}, [onError, settings, loadOptions, shouldLoad])
130141

131142
const value = useMemo<AnalyticsContextInterface<T>>(() => {
@@ -144,7 +155,18 @@ export function AnalyticsProvider<T extends Events>({
144155
}
145156
}, [events, internalAnalytics, isAnalyticsReady, onEventError])
146157

147-
const shouldRender = !shouldRenderOnlyWhenReady || isAnalyticsReady
158+
const shouldRender =
159+
!shouldRenderOnlyWhenReady || (isAnalyticsReady && !needConsent)
160+
161+
useDeepCompareEffectNoCheck(() => {
162+
internalAnalytics?.consent({
163+
consentManagement: {
164+
enabled: true,
165+
allowedConsentIds: allowedConsents,
166+
deniedConsentIds: deniedConsents,
167+
},
168+
})
169+
}, [allowedConsents, deniedConsents])
148170

149171
return (
150172
<AnalyticsContext.Provider value={value}>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const transformConfigToDestinations = (
1313
): AnalyticsIntegration[] => {
1414
const { destinations } = config.source
1515

16-
const integrations = destinations.map(
16+
const dest = destinations.map(
1717
({ destinationDefinition, config: { consentManagement } }) => ({
1818
name: destinationDefinition.name,
1919
displayName: destinationDefinition.displayName,
@@ -23,7 +23,7 @@ const transformConfigToDestinations = (
2323
}),
2424
)
2525

26-
return integrations
26+
return dest
2727
}
2828

2929
/**
@@ -67,9 +67,6 @@ export const useDestinations = (config: Config) => {
6767
.catch(() => {
6868
setDestinations([])
6969
})
70-
.finally(() => {
71-
setDestinations([])
72-
})
7370
}, [setDestinations, config.analytics])
7471

7572
return {

packages/use-analytics/src/cookies-consent/CookieConsentProvider.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ import { uniq } from '../helpers/array'
2222
import { IS_CLIENT } from '../helpers/isClient'
2323
import { stringToHash } from '../helpers/misc'
2424
import { isCategoryKind } from '../types'
25-
import type { Config, Consent, Integration, Integrations } from '../types'
25+
import type {
26+
Config,
27+
Consent,
28+
Destination,
29+
Destinations,
30+
EssentialDestination,
31+
} from '../types'
2632

2733
type Context = {
28-
destinations: Integrations
34+
destinations: Destinations
2935
needConsent: boolean
3036
isDestinationsLoaded: boolean
3137
categories: typeof CATEGORIES
@@ -50,14 +56,16 @@ export const CookieConsentProvider = ({
5056
children,
5157
isConsentRequired,
5258
essentialDestinations,
59+
manualDestinations,
5360
config,
5461
cookiePrefix = COOKIE_PREFIX,
5562
consentMaxAge = CONSENT_MAX_AGE,
5663
consentAdvertisingMaxAge = CONSENT_ADVERTISING_MAX_AGE,
5764
cookiesOptions = COOKIES_OPTIONS,
5865
}: PropsWithChildren<{
5966
isConsentRequired: boolean
60-
essentialDestinations: string[]
67+
essentialDestinations: EssentialDestination[]
68+
manualDestinations?: Destinations
6169
config: Config
6270
cookiePrefix?: string
6371
consentMaxAge?: number
@@ -74,25 +82,28 @@ export const CookieConsentProvider = ({
7482
isLoaded: isDestinationsLoaded,
7583
} = useDestinations(config)
7684

77-
const destinations: Integrations = useMemo(
85+
const destinations: Destinations = useMemo(
7886
() =>
7987
uniq([
8088
...(analyticsDestinations ?? []).map(
8189
dest =>
8290
({
8391
name: dest.name,
92+
displayName: dest.displayName,
8493
category: dest.consents[0] ?? 'essential',
85-
}) satisfies Integration,
94+
}) satisfies Destination,
8695
),
8796
...essentialDestinations.map(
8897
dest =>
8998
({
90-
name: dest,
99+
name: dest.name,
100+
displayName: dest.displayName,
91101
category: 'essential',
92-
}) satisfies Integration,
102+
}) satisfies Destination,
93103
),
104+
...(manualDestinations ?? []),
94105
]),
95-
[analyticsDestinations, essentialDestinations],
106+
[analyticsDestinations, essentialDestinations, manualDestinations],
96107
)
97108

98109
// We compute a hash with all the integrations that are enabled
@@ -101,20 +112,17 @@ export const CookieConsentProvider = ({
101112
const destinationsHash = useMemo(
102113
() =>
103114
stringToHash(
104-
uniq([
105-
...destinations.map(({ name }) => name),
106-
...essentialDestinations,
107-
])
115+
uniq(destinations.map(({ name }) => name))
108116
.sort()
109117
.join(undefined),
110118
),
111-
[destinations, essentialDestinations],
119+
[destinations],
112120
)
113121

114122
useEffect(() => {
115-
// We set needConsent at false until we have an answer from segment
123+
// We set needConsent at false until we have an answer from the source
116124
// This is to avoid showing setting needConsent to true only to be set
117-
// to false after receiving segment answer and flicker the UI
125+
// to false after receiving source answer and flicker the UI
118126

119127
setNeedsConsent(
120128
isConsentRequired &&

packages/use-analytics/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type {
1111
} from './analytics/useAnalytics'
1212
export type {
1313
AnalyticsIntegration,
14+
EssentialDestination,
15+
Destination,
1416
CategoryKind,
1517
Consents,
1618
Consent,

packages/use-analytics/src/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type ResolutionStrategy = 'and' | 'or'
1111

1212
export type Consents = { consent: CategoryKind }[]
1313

14-
type Destination = {
14+
type RudderDestination = {
1515
id: string
1616
name: string
1717
enabled: boolean
@@ -44,7 +44,7 @@ export type AnalyticsConfig = {
4444
config: Record<string, any>
4545
enabled: boolean
4646
workspaceId: string
47-
destinations: Destination[]
47+
destinations: RudderDestination[]
4848
}
4949
}
5050

@@ -56,12 +56,14 @@ export type AnalyticsIntegration = {
5656

5757
export type Consent = { [K in CategoryKind]: boolean }
5858

59-
export type Integration = {
59+
export type Destination = {
6060
category: CategoryKind
6161
name: string
62+
displayName: string
6263
}
64+
export type EssentialDestination = Pick<Destination, 'displayName' | 'name'>
6365

64-
export type Integrations = Integration[]
66+
export type Destinations = Destination[]
6567

6668
export type Config = {
6769
analytics?: {

0 commit comments

Comments
 (0)