From 5784629af9724f9db04d9406ed79c1533b925f4a Mon Sep 17 00:00:00 2001 From: narciero Date: Tue, 26 Mar 2024 16:57:46 -0600 Subject: [PATCH] update consent changed event handling --- README.md | 24 +++---------- .../consent-manager-builder/index.todo.js | 7 ++-- .../preferences.test.ts | 36 +++++-------------- src/consent-manager-builder/preferences.ts | 26 ++++++++------ src/types.ts | 1 - 5 files changed, 34 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index ca48274..9ce3d32 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ _Hightouch works to ensure Consent Manager works with most of our product pipeli ### How it works -1. The consent manager delays loading the events SDK until user consent is given, and will not load the SDK at all if the user opts out of everything. The user's tracking preferences are saved to a cookie and sent as an `identify` trait and a `Consent Updated` track event (if they haven't opted out of everything), ensuring consent data is propagated to your destinations. +1. The consent manager delays loading the events SDK until user consent is given, and will not load the SDK at all if the user opts out of everything. The user's tracking preferences are saved to a cookie and sent as a `Consent Updated` track event (if they haven't opted out of everything), ensuring consent data is propagated to your destinations. 2. **All** events will include the latest consent metadata inside the `context.consent` object: @@ -43,29 +43,15 @@ _Hightouch works to ensure Consent Manager works with most of our product pipeli } ``` -3. Whenever a user updates their consent preferences, the following events will be emitted - - `identify` with updated preferences in `traits` - ```json - { - "type": "identify", - "traits": { - "destinationTrackingPreferences": {}, - "categoryTrackingPreferences": { - "marketingAndAnalytics": true, - "advertising": true, - "functional": true - } - } - } - ``` +3. Whenever a user updates their consent preferences, the following event will be emitted - `track` named `Consent Updated` with updated preferences ```json { "type": "track", "event": "Consent Updated", "properties": { - "destinationTrackingPreferences": {}, - "categoryTrackingPreferences": { + "destinationPreferences": {}, + "categoryPreferences": { "marketingAndAnalytics": true, "advertising": true, "functional": true @@ -784,7 +770,7 @@ Resets the [preferences][] state to the value saved in the cookie. Useful for re **Type**: `function(object|boolean)` -Saves the preferences currently in state to a cookie called `ht-cm-preferences`, triggers an `identify` event with `destinationTrackingPreferences` and `categoryTrackingPreferences` traits, triggers a `track` event called `Consent Updated`, and then reloads the Browser SDK using the new preferences. It can also be passed preferences like [setPreferences][] to do a final update before saving. +Saves the preferences currently in state to a cookie called `ht-cm-preferences`, triggers a `track` event named `Consent Updated`, and then reloads the Browser SDK using the new preferences. It can also be passed preferences like [setPreferences][] to do a final update before saving. ##### onError diff --git a/src/__tests__/consent-manager-builder/index.todo.js b/src/__tests__/consent-manager-builder/index.todo.js index e32ee35..bd48926 100644 --- a/src/__tests__/consent-manager-builder/index.todo.js +++ b/src/__tests__/consent-manager-builder/index.todo.js @@ -172,7 +172,11 @@ describe('ConsentManagerBuilder', () => { test.skip('if defaultDestinationBehavior is set to imply and category is set to true, loads new destination', done => { document.cookie = 'ht-cm-preferences={%22version%22:1%2C%22destinations%22:{%22Amplitude%22:true}%2C%22custom%22:{%22advertising%22:false%2C%22marketingAndAnalytics%22:true%2C%22functional%22:true}}' - window.htevents = { load() {}, identify() {}, track() {}, addSourceMiddleware() {} } + window.htevents = { + load() {}, + track() {}, + addSourceMiddleware() {} + } nock('https://cdn.hightouch-events.com') .get('/v1/projects/123/integrations') @@ -248,7 +252,6 @@ describe('ConsentManagerBuilder', () => { 'ht-cm-preferences={%22version%22:1%2C%22destinations%22:{%22Amplitude%22:true}%2C%22custom%22:{%22advertising%22:false%2C%22marketingAndAnalytics%22:false%2C%22functional%22:true}}' window.htevents = { load() {}, - identify() {}, track() {}, addSourceMiddleware() {} } diff --git a/src/__tests__/consent-manager-builder/preferences.test.ts b/src/__tests__/consent-manager-builder/preferences.test.ts index cb1f49c..f49682e 100644 --- a/src/__tests__/consent-manager-builder/preferences.test.ts +++ b/src/__tests__/consent-manager-builder/preferences.test.ts @@ -53,7 +53,7 @@ describe('preferences', () => { }) test('savePreferences() saves the preferences', () => { - const htevents = { identify: sinon.spy(), track: sinon.spy() } + const htevents = { track: sinon.spy() } ;(window as any).htevents = htevents document.cookie = '' @@ -70,17 +70,11 @@ describe('preferences', () => { cookieDomain: undefined }) - expect(htevents.identify.calledOnce).toBe(true) - expect(htevents.identify.args[0][0]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: customPreferences - }) - expect(htevents.track.calledOnce).toBe(true) expect(htevents.track.args[0][0]).toBe('Consent Updated') expect(htevents.track.args[0][1]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: customPreferences + destinationPreferences: destinationPreferences, + categoryPreferences: customPreferences }) expect( @@ -91,7 +85,7 @@ describe('preferences', () => { }) test('savePreferences() sets the cookie domain', () => { - const htevents = { identify: sinon.spy(), track: sinon.spy() } + const htevents = { track: sinon.spy() } ;(window as any).htevents = htevents document.cookie = '' @@ -105,17 +99,11 @@ describe('preferences', () => { cookieDomain: 'example.com' }) - expect(htevents.identify.calledOnce).toBe(true) - expect(htevents.identify.args[0][0]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: undefined - }) - expect(htevents.track.calledOnce).toBe(true) expect(htevents.track.args[0][0]).toBe('Consent Updated') expect(htevents.track.args[0][1]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: undefined + destinationPreferences: destinationPreferences, + categoryPreferences: undefined }) // TODO: actually check domain @@ -123,7 +111,7 @@ describe('preferences', () => { }) test('savePreferences() sets the cookie with custom key', () => { - const htevents = { identify: sinon.spy(), track: sinon.spy() } + const htevents = { track: sinon.spy() } ;(window as any).htevents = htevents document.cookie = '' @@ -138,17 +126,11 @@ describe('preferences', () => { cookieName: 'custom-tracking-preferences' }) - expect(htevents.identify.calledOnce).toBe(true) - expect(htevents.identify.args[0][0]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: undefined - }) - expect(htevents.track.calledOnce).toBe(true) expect(htevents.track.args[0][0]).toBe('Consent Updated') expect(htevents.track.args[0][1]).toMatchObject({ - destinationTrackingPreferences: destinationPreferences, - categoryTrackingPreferences: undefined + destinationPreferences: destinationPreferences, + categoryPreferences: undefined }) expect(document.cookie.includes('custom-tracking-preferences')).toBe(true) diff --git a/src/consent-manager-builder/preferences.ts b/src/consent-manager-builder/preferences.ts index b3e51e4..e90ebcc 100644 --- a/src/consent-manager-builder/preferences.ts +++ b/src/consent-manager-builder/preferences.ts @@ -64,17 +64,21 @@ export function savePreferences({ }: SavePreferences) { const wd = window as WindowWithHtEvents if (wd.htevents) { - wd.htevents.identify({ - destinationTrackingPreferences: destinationPreferences, - // use `categoryTrackingPreferences` here for consistency with `context.consent.categoryPreferences` - categoryTrackingPreferences: customPreferences - }) - - wd.htevents.track('Consent Updated', { - destinationTrackingPreferences: destinationPreferences, - // use `categoryTrackingPreferences` here for consistency with `context.consent.categoryPreferences` - categoryTrackingPreferences: customPreferences - }) + wd.htevents.track( + 'Consent Updated', + { + destinationPreferences, + // use `categoryPreferences` here for consistency with `context.consent.categoryPreferences` + categoryPreferences: customPreferences + }, + { + // update `context.consent` here to immediately reflect the updated preferences + consent: { + destinationPreferences, + categoryPreferences: customPreferences + } + } + ) } else { console.warn('window.htevents not found...is the SDK snippet included on the page?') } diff --git a/src/types.ts b/src/types.ts index f444ad9..59a5b77 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,7 +11,6 @@ export type HtEventsBrowserOptions = { export type HtEventsBrowser = { initialized: boolean load: (writeKey: string, options?: HtEventsBrowserOptions) => void - identify: (properties: Record) => void track: (event: string, properties: Record, options?: any, callback?: any) => void addSourceMiddleware: (middleware: Middleware) => void }