From 0cb2eb27d3e4c4c122b507e072013ba2f0a5d227 Mon Sep 17 00:00:00 2001 From: Alexandre Philibeaux Date: Wed, 10 Apr 2024 16:31:32 +0000 Subject: [PATCH] fix(segment): use right object for consent --- .changeset/olive-cars-double.md | 5 ++++ .../SegmentConsentMiddleware.tsx | 27 ++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 .changeset/olive-cars-double.md diff --git a/.changeset/olive-cars-double.md b/.changeset/olive-cars-double.md new file mode 100644 index 000000000..e4ad285a2 --- /dev/null +++ b/.changeset/olive-cars-double.md @@ -0,0 +1,5 @@ +--- +"@scaleway/cookie-consent": patch +--- + +update consent object diff --git a/packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx b/packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx index 9e4995e0b..4274f34c2 100644 --- a/packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx +++ b/packages/cookie-consent/src/CookieConsentProvider/SegmentConsentMiddleware.tsx @@ -2,11 +2,17 @@ import { useSegment } from '@scaleway/use-segment' import cookie from 'cookie' import type { PropsWithChildren } from 'react' import { useCookieConsent } from './CookieConsentProvider' -import { type CategoryKind, isCategoryKind } from './helpers' +import { type CategoryKind } from './helpers' export const AMPLITUDE_INTEGRATION_NAME = 'Amplitude (Actions)' const COOKIE_SESSION_ID_NAME = 'analytics_session_id' +type ConsentObject = { + defaultDestinationBehavior: null + destinationPreferences: null + categoryPreferences: Partial> +} + export const getSessionId = () => { const sessionId = cookie.parse(document.cookie)[COOKIE_SESSION_ID_NAME] if (sessionId) { @@ -29,25 +35,20 @@ export const SegmentConsentMiddleware = ({ const { analytics } = useSegment() const { categoriesConsent } = useCookieConsent() - const categoriesPreferencesAccepted: CategoryKind[] = [] - - for (const [key, value] of Object.entries(categoriesConsent)) { - if (value && isCategoryKind(key)) { - categoriesPreferencesAccepted.push(key) - } - } - analytics ?.addSourceMiddleware(({ payload, next }) => { if (payload.obj.context) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-param-reassign - payload.obj.context['consent'] = { - ...payload.obj.context['consent'], + const consent: ConsentObject = { + // ...payload.obj.context['consent'], defaultDestinationBehavior: null, // Need to be handle if we let the user choose per destination and not per categories. destinationPreferences: null, - categoryPreferences: categoriesPreferencesAccepted, + // https://segment.com/docs/privacy/consent-management/consent-in-segment-connections/#consent-object + categoryPreferences: categoriesConsent, } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-param-reassign + payload.obj.context['consent'] = consent } // actually there is a bug on the default script.