Skip to content

Commit 74bd988

Browse files
authored
fix(segment): use right object for consent (#1922)
1 parent 249071d commit 74bd988

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

.changeset/olive-cars-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/cookie-consent": patch
3+
---
4+
5+
update consent object

packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import { useSegment } from '@scaleway/use-segment'
22
import cookie from 'cookie'
33
import type { PropsWithChildren } from 'react'
44
import { useCookieConsent } from './CookieConsentProvider'
5-
import { type CategoryKind, isCategoryKind } from './helpers'
5+
import { type CategoryKind } from './helpers'
66

77
export const AMPLITUDE_INTEGRATION_NAME = 'Amplitude (Actions)'
88
const COOKIE_SESSION_ID_NAME = 'analytics_session_id'
99

10+
type ConsentObject = {
11+
defaultDestinationBehavior: null
12+
destinationPreferences: null
13+
categoryPreferences: Partial<Record<CategoryKind, boolean>>
14+
}
15+
1016
export const getSessionId = () => {
1117
const sessionId = cookie.parse(document.cookie)[COOKIE_SESSION_ID_NAME]
1218
if (sessionId) {
@@ -29,25 +35,20 @@ export const SegmentConsentMiddleware = ({
2935
const { analytics } = useSegment()
3036
const { categoriesConsent } = useCookieConsent()
3137

32-
const categoriesPreferencesAccepted: CategoryKind[] = []
33-
34-
for (const [key, value] of Object.entries(categoriesConsent)) {
35-
if (value && isCategoryKind(key)) {
36-
categoriesPreferencesAccepted.push(key)
37-
}
38-
}
39-
4038
analytics
4139
?.addSourceMiddleware(({ payload, next }) => {
4240
if (payload.obj.context) {
43-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-param-reassign
44-
payload.obj.context['consent'] = {
45-
...payload.obj.context['consent'],
41+
const consent: ConsentObject = {
42+
// ...payload.obj.context['consent'],
4643
defaultDestinationBehavior: null,
4744
// Need to be handle if we let the user choose per destination and not per categories.
4845
destinationPreferences: null,
49-
categoryPreferences: categoriesPreferencesAccepted,
46+
// https://segment.com/docs/privacy/consent-management/consent-in-segment-connections/#consent-object
47+
categoryPreferences: categoriesConsent,
5048
}
49+
50+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-param-reassign
51+
payload.obj.context['consent'] = consent
5152
}
5253

5354
// actually there is a bug on the default script.

0 commit comments

Comments
 (0)