@@ -2,11 +2,17 @@ import { useSegment } from '@scaleway/use-segment'
22import cookie from 'cookie'
33import type { PropsWithChildren } from 'react'
44import { useCookieConsent } from './CookieConsentProvider'
5- import { type CategoryKind , isCategoryKind } from './helpers'
5+ import { type CategoryKind } from './helpers'
66
77export const AMPLITUDE_INTEGRATION_NAME = 'Amplitude (Actions)'
88const 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+
1016export 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