@ht-sdks/consent-manager 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,7 +84,7 @@ describe('preferences', () => {
84
84
  })
85
85
  })
86
86
 
87
- test('savePreferences() saves the preferences', () => {
87
+ test('savePreferences() saves the preferences', async () => {
88
88
  const destinationPreferences = {
89
89
  Amplitude: true,
90
90
  }
@@ -93,7 +93,7 @@ describe('preferences', () => {
93
93
  functional: true,
94
94
  }
95
95
 
96
- savePreferences({
96
+ await savePreferences({
97
97
  destinationPreferences,
98
98
  customPreferences,
99
99
  cookieDomain: undefined,
@@ -106,14 +106,20 @@ describe('preferences', () => {
106
106
  )
107
107
  })
108
108
 
109
- test('savePreferences() sets the cookie domain', () => {
109
+ test('saves an opt-out without sending a consent update', async () => {
110
+ await savePreferences({ destinationPreferences: { Amplitude: false } })
111
+ expect(loadPreferences().destinationPreferences).toEqual({ Amplitude: false })
112
+ expect(htevents.track.notCalled).toBe(true)
113
+ })
114
+
115
+ test('savePreferences() sets the cookie domain', async () => {
110
116
  const destinationPreferences = {
111
117
  Amplitude: true,
112
118
  }
113
119
 
114
120
  const customPreferences = undefined
115
121
 
116
- savePreferences({
122
+ await savePreferences({
117
123
  destinationPreferences,
118
124
  customPreferences,
119
125
  cookieDomain: 'example.com',
@@ -124,14 +130,14 @@ describe('preferences', () => {
124
130
  // ToDo: verify cookie domain is set (would need to mock js-cookie since document.cookie doesn't have domain info)
125
131
  })
126
132
 
127
- test('savePreferences() sets the cookie with custom key', () => {
133
+ test('savePreferences() sets the cookie with custom key', async () => {
128
134
  const destinationPreferences = {
129
135
  Amplitude: true,
130
136
  }
131
137
 
132
138
  const customPreferences = undefined
133
139
 
134
- savePreferences({
140
+ await savePreferences({
135
141
  destinationPreferences,
136
142
  customPreferences,
137
143
  cookieDomain: undefined,
@@ -13,6 +13,7 @@ interface AnalyticsParams {
13
13
  destinations: Destination[]
14
14
  destinationPreferences: CategoryPreferences | null | undefined
15
15
  isConsentRequired: boolean
16
+ consentUpdate?: Promise<void>
16
17
  shouldReload?: boolean
17
18
  devMode?: boolean
18
19
  defaultDestinationBehavior?: DefaultDestinationBehavior
@@ -44,6 +45,7 @@ export default function conditionallyLoadAnalytics({
44
45
  destinationPreferences,
45
46
  isConsentRequired,
46
47
  shouldReload = true,
48
+ consentUpdate,
47
49
  devMode = false,
48
50
  defaultDestinationBehavior,
49
51
  categoryPreferences,
@@ -82,7 +84,19 @@ export default function conditionallyLoadAnalytics({
82
84
  if (wd.htevents.initialized) {
83
85
  if (shouldReload) {
84
86
  console.debug('reloading to apply new tracking preferences')
85
- window.location.reload()
87
+ if (consentUpdate) {
88
+ let expired = false
89
+ const deadline = setTimeout(() => {
90
+ expired = true
91
+ window.location.reload()
92
+ }, 500)
93
+ void consentUpdate.then(() => {
94
+ clearTimeout(deadline)
95
+ if (!expired) window.location.reload()
96
+ })
97
+ } else {
98
+ window.location.reload()
99
+ }
86
100
  }
87
101
  return
88
102
  }
@@ -368,7 +368,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
368
368
  ) {
369
369
  shouldReload = true
370
370
  }
371
- savePreferences({
371
+ const consentUpdate = savePreferences({
372
372
  destinationPreferences,
373
373
  customPreferences,
374
374
  cookieDomain,
@@ -377,6 +377,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
377
377
  cookieAttributes,
378
378
  })
379
379
  conditionallyLoadAnalytics({
380
+ consentUpdate,
380
381
  writeKey,
381
382
  options,
382
383
  destinations,
@@ -68,16 +68,45 @@ export function savePreferences({
68
68
  cookieExpires,
69
69
  cookieAttributes = {},
70
70
  }: SavePreferences) {
71
+ const domain = cookieDomain || topDomain(window.location.href)
72
+ const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES
73
+ const value = {
74
+ version: 1,
75
+ destination: destinationPreferences,
76
+ custom: customPreferences,
77
+ } as PreferencesCookie
78
+
79
+ cookies.set(cookieName || DEFAULT_COOKIE_NAME, JSON.stringify(value), {
80
+ expires,
81
+ domain,
82
+ ...cookieAttributes,
83
+ })
84
+
85
+ emitter.emit('preferencesSaved', {
86
+ destinationPreferences,
87
+ customPreferences,
88
+ })
89
+
71
90
  const wd = window as WindowWithHtEvents
91
+ if (
92
+ ![
93
+ ...Object.values(destinationPreferences || {}),
94
+ ...Object.values(customPreferences || {}),
95
+ ].includes(true)
96
+ ) {
97
+ return
98
+ }
99
+
72
100
  if (wd.htevents) {
73
- wd.htevents.track(
74
- 'Consent Updated',
101
+ return trackUpdate(
102
+ wd.htevents,
75
103
  {
76
104
  destinationPreferences,
77
105
  // use `categoryPreferences` here for consistency with `context.consent.categoryPreferences`
78
106
  categoryPreferences: customPreferences,
79
107
  },
80
108
  {
109
+ integrations: { All: false, ...destinationPreferences },
81
110
  // update `context.consent` here to immediately reflect the updated preferences
82
111
  consent: {
83
112
  destinationPreferences,
@@ -87,24 +116,18 @@ export function savePreferences({
87
116
  )
88
117
  } else {
89
118
  console.warn('window.htevents not found...is the SDK snippet included on the page?')
119
+ return undefined
90
120
  }
121
+ }
91
122
 
92
- const domain = cookieDomain || topDomain(window.location.href)
93
- const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES
94
- const value = {
95
- version: 1,
96
- destination: destinationPreferences,
97
- custom: customPreferences,
98
- } as PreferencesCookie
99
-
100
- cookies.set(cookieName || DEFAULT_COOKIE_NAME, JSON.stringify(value), {
101
- expires,
102
- domain,
103
- ...cookieAttributes,
104
- })
105
-
106
- emitter.emit('preferencesSaved', {
107
- destinationPreferences,
108
- customPreferences,
109
- })
123
+ async function trackUpdate(
124
+ htevents: NonNullable<WindowWithHtEvents['htevents']>,
125
+ properties: Record<string, unknown>,
126
+ options: object,
127
+ ) {
128
+ try {
129
+ await htevents.track('Consent Updated', properties, options)
130
+ } catch (error) {
131
+ console.warn('Unable to track consent update', error)
132
+ }
110
133
  }
package/src/types.ts CHANGED
@@ -13,7 +13,7 @@ export type HtEventsBrowserOptions = {
13
13
  export type HtEventsBrowser = {
14
14
  initialized: boolean
15
15
  load: (writeKey: string, options?: HtEventsBrowserOptions) => void
16
- track: (event: string, properties: Record<string, any>, options?: any, callback?: any) => void
16
+ track: (event: string, properties: Record<string, any>, options?: any, callback?: any) => void | Promise<unknown>
17
17
  addSourceMiddleware: (middleware: Middleware) => void
18
18
  }
19
19