Skip to content

Commit 56c8f90

Browse files
committed
fix(segment): use right object for consent (#1922)
1 parent 249071d commit 56c8f90

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
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 & 12 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,21 @@ 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'] = {
41+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
42+
const consent: ConsentObject = {
4543
...payload.obj.context['consent'],
4644
defaultDestinationBehavior: null,
4745
// Need to be handle if we let the user choose per destination and not per categories.
4846
destinationPreferences: null,
49-
categoryPreferences: categoriesPreferencesAccepted,
47+
// https://segment.com/docs/privacy/consent-management/consent-in-segment-connections/#consent-object
48+
categoryPreferences: categoriesConsent,
5049
}
50+
51+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-param-reassign
52+
payload.obj.context['consent'] = consent
5153
}
5254

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

0 commit comments

Comments
 (0)