@ht-sdks/consent-manager 0.1.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.
Files changed (98) hide show
  1. package/commonjs/__tests__/consent-manager/dialog.test.js +33 -0
  2. package/commonjs/__tests__/consent-manager-builder/analytics.test.js +135 -46
  3. package/commonjs/__tests__/consent-manager-builder/index.todo.js +48 -87
  4. package/commonjs/__tests__/consent-manager-builder/preferences.test.js +136 -55
  5. package/commonjs/__tests__/index.test.js +12 -12
  6. package/commonjs/consent-manager/banner.js +35 -19
  7. package/commonjs/consent-manager/buttons.js +5 -5
  8. package/commonjs/consent-manager/cancel-dialog.js +26 -10
  9. package/commonjs/consent-manager/categories.js +2 -2
  10. package/commonjs/consent-manager/container.js +2 -3
  11. package/commonjs/consent-manager/dialog.js +39 -23
  12. package/commonjs/consent-manager/font-styles.js +2 -2
  13. package/commonjs/consent-manager/index.js +41 -25
  14. package/commonjs/consent-manager/preference-dialog.js +36 -20
  15. package/commonjs/consent-manager-builder/analytics.js +19 -7
  16. package/commonjs/consent-manager-builder/index.js +31 -30
  17. package/commonjs/consent-manager-builder/preferences.js +100 -24
  18. package/commonjs/index.js +5 -4
  19. package/commonjs/standalone.js +25 -11
  20. package/esm/__tests__/consent-manager/dialog.test.js +28 -0
  21. package/esm/__tests__/consent-manager-builder/analytics.test.js +123 -34
  22. package/esm/__tests__/consent-manager-builder/index.todo.js +34 -73
  23. package/esm/__tests__/consent-manager-builder/preferences.test.js +135 -54
  24. package/esm/consent-manager/banner.js +5 -3
  25. package/esm/consent-manager/buttons.js +4 -4
  26. package/esm/consent-manager/cancel-dialog.js +4 -2
  27. package/esm/consent-manager/categories.js +2 -2
  28. package/esm/consent-manager/container.js +1 -1
  29. package/esm/consent-manager/dialog.js +7 -5
  30. package/esm/consent-manager/font-styles.js +2 -2
  31. package/esm/consent-manager/index.js +19 -17
  32. package/esm/consent-manager/preference-dialog.js +7 -5
  33. package/esm/consent-manager-builder/analytics.js +18 -6
  34. package/esm/consent-manager-builder/index.js +28 -27
  35. package/esm/consent-manager-builder/preferences.js +96 -20
  36. package/esm/index.js +4 -3
  37. package/esm/standalone.js +3 -3
  38. package/esm/types.js +1 -0
  39. package/package.json +62 -67
  40. package/src/__tests__/consent-manager/dialog.test.tsx +42 -0
  41. package/src/__tests__/consent-manager-builder/analytics.test.ts +75 -33
  42. package/src/__tests__/consent-manager-builder/index.todo.js +52 -95
  43. package/src/__tests__/consent-manager-builder/preferences.test.ts +43 -27
  44. package/src/consent-manager/banner.tsx +11 -11
  45. package/src/consent-manager/buttons.tsx +26 -10
  46. package/src/consent-manager/cancel-dialog.tsx +1 -1
  47. package/src/consent-manager/categories.ts +2 -2
  48. package/src/consent-manager/container.tsx +14 -17
  49. package/src/consent-manager/dialog.tsx +4 -4
  50. package/src/consent-manager/font-styles.tsx +12 -2
  51. package/src/consent-manager/index.tsx +27 -29
  52. package/src/consent-manager/preference-dialog.tsx +13 -11
  53. package/src/consent-manager-builder/analytics.ts +20 -6
  54. package/src/consent-manager-builder/index.tsx +30 -29
  55. package/src/consent-manager-builder/preferences.ts +59 -30
  56. package/src/index.ts +8 -4
  57. package/src/standalone.tsx +1 -1
  58. package/src/types.ts +9 -3
  59. package/standalone/consent-manager.js +5 -10
  60. package/standalone/consent-manager.js.map +1 -1
  61. package/types/consent-manager/banner.d.ts +1 -1
  62. package/types/consent-manager/buttons.d.ts +6 -7
  63. package/types/consent-manager/cancel-dialog.d.ts +1 -1
  64. package/types/consent-manager/index.d.ts +3 -3
  65. package/types/consent-manager/preference-dialog.d.ts +1 -1
  66. package/types/consent-manager-builder/analytics.d.ts +2 -1
  67. package/types/consent-manager-builder/index.d.ts +7 -8
  68. package/types/consent-manager-builder/preferences.d.ts +4 -6
  69. package/types/index.d.ts +1 -1
  70. package/types/types.d.ts +15 -11
  71. package/commonjs/__tests__/consent-manager-builder/fetch-destinations.test.js +0 -155
  72. package/commonjs/consent-manager-builder/fetch-destinations.js +0 -104
  73. package/esm/__tests__/consent-manager-builder/fetch-destinations.test.js +0 -150
  74. package/esm/consent-manager-builder/fetch-destinations.js +0 -98
  75. package/src/__tests__/consent-manager-builder/fetch-destinations.test.ts +0 -89
  76. package/src/consent-manager-builder/fetch-destinations.ts +0 -50
  77. package/types/consent-manager-builder/fetch-destinations.d.ts +0 -2
  78. package/types/src/__tests__/consent-manager-builder/analytics.test.d.ts +0 -1
  79. package/types/src/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +0 -1
  80. package/types/src/__tests__/consent-manager-builder/preferences.test.d.ts +0 -1
  81. package/types/src/__tests__/index.test.d.ts +0 -1
  82. package/types/src/consent-manager/banner.d.ts +0 -21
  83. package/types/src/consent-manager/buttons.d.ts +0 -13
  84. package/types/src/consent-manager/cancel-dialog.d.ts +0 -19
  85. package/types/src/consent-manager/categories.d.ts +0 -3
  86. package/types/src/consent-manager/container.d.ts +0 -35
  87. package/types/src/consent-manager/dialog.d.ts +0 -29
  88. package/types/src/consent-manager/font-styles.d.ts +0 -2
  89. package/types/src/consent-manager/index.d.ts +0 -35
  90. package/types/src/consent-manager/preference-dialog.d.ts +0 -32
  91. package/types/src/consent-manager-builder/analytics.d.ts +0 -14
  92. package/types/src/consent-manager-builder/fetch-destinations.d.ts +0 -2
  93. package/types/src/consent-manager-builder/index.d.ts +0 -126
  94. package/types/src/consent-manager-builder/preferences.d.ts +0 -26
  95. package/types/src/index.d.ts +0 -7
  96. package/types/src/standalone.d.ts +0 -5
  97. package/types/src/types.d.ts +0 -130
  98. /package/types/__tests__/{consent-manager-builder/fetch-destinations.test.d.ts → consent-manager/dialog.test.d.ts} +0 -0
@@ -6,13 +6,13 @@ import {
6
6
  CategoryPreferences,
7
7
  Destination,
8
8
  ConsentManagerProps,
9
- PreferenceDialogTemplate
9
+ PreferenceDialogTemplate,
10
10
  } from '../types'
11
11
 
12
12
  const zeroValuePreferences: CategoryPreferences = {
13
13
  marketingAndAnalytics: null,
14
14
  advertising: null,
15
- functional: null
15
+ functional: null,
16
16
  }
17
17
 
18
18
  const defaultPreferencesDialogTemplate: PreferenceDialogTemplate = {
@@ -20,19 +20,19 @@ const defaultPreferencesDialogTemplate: PreferenceDialogTemplate = {
20
20
  allowValue: 'Allow',
21
21
  categoryValue: 'Category',
22
22
  purposeValue: 'Purpose',
23
- toolsValue: 'Tools'
23
+ toolsValue: 'Tools',
24
24
  },
25
25
  checkboxes: {
26
26
  noValue: 'No',
27
- yesValue: 'Yes'
27
+ yesValue: 'Yes',
28
28
  },
29
29
  actionButtons: {
30
30
  cancelValue: 'Cancel',
31
- saveValue: 'Save'
31
+ saveValue: 'Save',
32
32
  },
33
33
  cancelDialogButtons: {
34
34
  cancelValue: 'Yes, Cancel',
35
- backValue: 'Go Back'
35
+ backValue: 'Go Back',
36
36
  },
37
37
  categories: [
38
38
  {
@@ -40,7 +40,7 @@ const defaultPreferencesDialogTemplate: PreferenceDialogTemplate = {
40
40
  name: 'Functional',
41
41
  description:
42
42
  'To monitor the performance of our site and to enhance your browsing experience.',
43
- example: 'For example, these tools enable you to communicate with us via live chat.'
43
+ example: 'For example, these tools enable you to communicate with us via live chat.',
44
44
  },
45
45
  {
46
46
  key: 'marketing',
@@ -48,7 +48,7 @@ const defaultPreferencesDialogTemplate: PreferenceDialogTemplate = {
48
48
  description:
49
49
  'To understand user behavior in order to provide you with a more relevant browsing experience or personalize the content on our site.',
50
50
  example:
51
- 'For example, we collect information about which pages you visit to help us present more relevant information.'
51
+ 'For example, we collect information about which pages you visit to help us present more relevant information.',
52
52
  },
53
53
  {
54
54
  key: 'advertising',
@@ -56,16 +56,16 @@ const defaultPreferencesDialogTemplate: PreferenceDialogTemplate = {
56
56
  description:
57
57
  'To personalize and measure the effectiveness of advertising on our site and other websites.',
58
58
  example:
59
- 'For example, we may serve you a personalized ad based on the pages you visit on our site.'
59
+ 'For example, we may serve you a personalized ad based on the pages you visit on our site.',
60
60
  },
61
61
  {
62
62
  key: 'essential',
63
63
  name: 'Essential',
64
64
  description: 'We use browser cookies that are necessary for the site to work as intended.',
65
65
  example:
66
- 'For example, we store your website data collection preferences so we can honor them if you return to our site. You can disable these cookies in your browser settings but if you do the site may not work as intended.'
67
- }
68
- ]
66
+ 'For example, we store your website data collection preferences so we can honor them if you return to our site. You can disable these cookies in your browser settings but if you do the site may not work as intended.',
67
+ },
68
+ ],
69
69
  }
70
70
  export default class ConsentManager extends PureComponent<ConsentManagerProps, {}> {
71
71
  static displayName = 'ConsentManager'
@@ -106,7 +106,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
106
106
  cancelDialogContent:
107
107
  'Your preferences have not been saved. By continuing to use our website, you’re agreeing to our Privacy Policy.',
108
108
  defaultDestinationBehavior: 'disable',
109
- preferencesDialogTemplate: defaultPreferencesDialogTemplate
109
+ preferencesDialogTemplate: defaultPreferencesDialogTemplate,
110
110
  }
111
111
 
112
112
  render() {
@@ -132,9 +132,8 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
132
132
  cancelDialogContent,
133
133
  customCategories,
134
134
  defaultDestinationBehavior,
135
- cdnHost,
136
135
  preferencesDialogTemplate,
137
- onError
136
+ onError,
138
137
  } = this.props
139
138
 
140
139
  return (
@@ -151,7 +150,6 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
151
150
  mapCustomPreferences={this.handleMapCustomPreferences}
152
151
  customCategories={customCategories}
153
152
  defaultDestinationBehavior={defaultDestinationBehavior}
154
- cdnHost={cdnHost}
155
153
  >
156
154
  {({
157
155
  destinations,
@@ -163,7 +161,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
163
161
  resetPreferences,
164
162
  saveConsent,
165
163
  havePreferencesChanged,
166
- workspaceAddedNewDestinations
164
+ workspaceAddedNewDestinations,
167
165
  }) => {
168
166
  return (
169
167
  <Container
@@ -209,34 +207,34 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
209
207
 
210
208
  mergeTemplates = (
211
209
  newProps: PreferenceDialogTemplate,
212
- defaultPreferencesDialogTemplate: PreferenceDialogTemplate
210
+ defaultPreferencesDialogTemplate: PreferenceDialogTemplate,
213
211
  ): PreferenceDialogTemplate => {
214
212
  const headingsMerge = {
215
213
  ...defaultPreferencesDialogTemplate.headings,
216
- ...newProps.headings
214
+ ...newProps.headings,
217
215
  }
218
216
  const checkboxesMerge = {
219
217
  ...defaultPreferencesDialogTemplate.checkboxes,
220
- ...newProps.checkboxes
218
+ ...newProps.checkboxes,
221
219
  }
222
220
  const actionButtonsMerge = {
223
221
  ...defaultPreferencesDialogTemplate.actionButtons,
224
- ...newProps.actionButtons
222
+ ...newProps.actionButtons,
225
223
  }
226
224
  const cancelDialogButtonsMerge = {
227
225
  ...defaultPreferencesDialogTemplate.cancelDialogButtons,
228
- ...newProps.cancelDialogButtons
226
+ ...newProps.cancelDialogButtons,
229
227
  }
230
- const categoriesMerge = defaultPreferencesDialogTemplate?.categories!.map(category => ({
228
+ const categoriesMerge = defaultPreferencesDialogTemplate?.categories!.map((category) => ({
231
229
  ...category,
232
- ...newProps?.categories?.find(c => c.key === category.key)
230
+ ...newProps?.categories?.find((c) => c.key === category.key),
233
231
  }))
234
232
  return {
235
233
  headings: headingsMerge,
236
234
  checkboxes: checkboxesMerge,
237
235
  actionButtons: actionButtonsMerge,
238
236
  cancelDialogButtons: cancelDialogButtonsMerge,
239
- categories: categoriesMerge
237
+ categories: categoriesMerge,
240
238
  }
241
239
  }
242
240
 
@@ -251,7 +249,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
251
249
  }
252
250
 
253
251
  const initialCustomPreferences = {}
254
- Object.keys(customCategories).forEach(category => {
252
+ Object.keys(customCategories).forEach((category) => {
255
253
  initialCustomPreferences[category] = null
256
254
  })
257
255
 
@@ -273,7 +271,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
273
271
  }
274
272
  }
275
273
 
276
- destinations.forEach(destination => {
274
+ destinations.forEach((destination) => {
277
275
  // Mark custom categories
278
276
  Object.entries(customCategories).forEach(([categoryName, { integrations }]) => {
279
277
  const consentAlreadySetToFalse = destinationPreferences[destination.id] === false
@@ -302,7 +300,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
302
300
  for (const destination of destinations) {
303
301
  // Mark advertising destinations
304
302
  if (
305
- ADVERTISING_CATEGORIES.find(c => c === destination.category) &&
303
+ ADVERTISING_CATEGORIES.find((c) => c === destination.category) &&
306
304
  destinationPreferences[destination.id] !== false
307
305
  ) {
308
306
  destinationPreferences[destination.id] = customPrefs.advertising
@@ -310,7 +308,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
310
308
 
311
309
  // Mark function destinations
312
310
  if (
313
- FUNCTIONAL_CATEGORIES.find(c => c === destination.category) &&
311
+ FUNCTIONAL_CATEGORIES.find((c) => c === destination.category) &&
314
312
  destinationPreferences[destination.id] !== false
315
313
  ) {
316
314
  destinationPreferences[destination.id] = customPrefs.functional
@@ -7,7 +7,7 @@ import {
7
7
  Destination,
8
8
  CustomCategories,
9
9
  CategoryPreferences,
10
- PreferenceDialogTemplate
10
+ PreferenceDialogTemplate,
11
11
  } from '../types'
12
12
 
13
13
  const hideOnMobile = css`
@@ -93,7 +93,7 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
93
93
  static defaultProps = {
94
94
  marketingAndAnalytics: null,
95
95
  advertising: null,
96
- functional: null
96
+ functional: null,
97
97
  }
98
98
 
99
99
  render() {
@@ -111,17 +111,19 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
111
111
  title,
112
112
  content,
113
113
  preferences,
114
- preferencesDialogTemplate
114
+ preferencesDialogTemplate,
115
115
  } = this.props
116
116
 
117
117
  const { headings, checkboxes, actionButtons } = preferencesDialogTemplate!
118
118
 
119
- const functionalInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'functional')
120
- const marketingInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'marketing')
119
+ const functionalInfo = preferencesDialogTemplate?.categories!.find(
120
+ (c) => c.key === 'functional',
121
+ )
122
+ const marketingInfo = preferencesDialogTemplate?.categories!.find((c) => c.key === 'marketing')
121
123
  const advertisingInfo = preferencesDialogTemplate?.categories!.find(
122
- c => c.key === 'advertising'
124
+ (c) => c.key === 'advertising',
123
125
  )
124
- const essentialInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'essential')
126
+ const essentialInfo = preferencesDialogTemplate?.categories!.find((c) => c.key === 'essential')
125
127
 
126
128
  const buttons = (
127
129
  <div>
@@ -326,7 +328,7 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
326
328
  .join(', ')}
327
329
  </td> */}
328
330
  </Row>
329
- )
331
+ ),
330
332
  )}
331
333
  </tbody>
332
334
  </Table>
@@ -335,7 +337,7 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
335
337
  )
336
338
  }
337
339
 
338
- handleChange = e => {
340
+ handleChange = (e) => {
339
341
  const { onChange } = this.props
340
342
  onChange(e.target.name, e.target.value === 'true')
341
343
  }
@@ -347,7 +349,7 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
347
349
  marketingAndAnalytics,
348
350
  advertising,
349
351
  functional,
350
- customCategories
352
+ customCategories,
351
353
  } = this.props
352
354
  e.preventDefault()
353
355
  // Safe guard against browsers that don't prevent the
@@ -362,7 +364,7 @@ export default class PreferenceDialog extends PureComponent<PreferenceDialogProp
362
364
  // Safe guard against custom categories being null
363
365
  if (
364
366
  customCategories &&
365
- Object.keys(customCategories).some(category => preferences[category] === null)
367
+ Object.keys(customCategories).some((category) => preferences[category] === null)
366
368
  ) {
367
369
  return
368
370
  }
@@ -4,7 +4,7 @@ import {
4
4
  DefaultDestinationBehavior,
5
5
  CategoryPreferences,
6
6
  Middleware,
7
- HtEventsBrowserOptions
7
+ HtEventsBrowserOptions,
8
8
  } from '../types'
9
9
 
10
10
  interface AnalyticsParams {
@@ -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
@@ -22,7 +23,7 @@ interface AnalyticsParams {
22
23
  function getConsentMiddleware(
23
24
  destinationPreferences,
24
25
  categoryPreferences,
25
- defaultDestinationBehavior
26
+ defaultDestinationBehavior,
26
27
  ): Middleware {
27
28
  return ({ payload, next }) => {
28
29
  payload.obj.context.consent = {
@@ -31,7 +32,7 @@ function getConsentMiddleware(
31
32
  destinationPreferences,
32
33
  // give precedence to `context.consent` values set explicitly for the event
33
34
  // this allows us to immediately reflect changes made to consent
34
- ...payload.obj.context.consent
35
+ ...payload.obj.context.consent,
35
36
  }
36
37
  next(payload)
37
38
  }
@@ -44,9 +45,10 @@ export default function conditionallyLoadAnalytics({
44
45
  destinationPreferences,
45
46
  isConsentRequired,
46
47
  shouldReload = true,
48
+ consentUpdate,
47
49
  devMode = false,
48
50
  defaultDestinationBehavior,
49
- categoryPreferences
51
+ categoryPreferences,
50
52
  }: AnalyticsParams) {
51
53
  const wd = window as WindowWithHtEvents
52
54
  if (!wd.htevents) return
@@ -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
  }
@@ -98,7 +112,7 @@ export default function conditionallyLoadAnalytics({
98
112
  const middleware = getConsentMiddleware(
99
113
  destinationPreferences,
100
114
  categoryPreferences,
101
- defaultDestinationBehavior
115
+ defaultDestinationBehavior,
102
116
  )
103
117
  wd.htevents.addSourceMiddleware(middleware)
104
118
 
@@ -6,9 +6,9 @@ import {
6
6
  CategoryPreferences,
7
7
  CustomCategories,
8
8
  DefaultDestinationBehavior,
9
- HtEventsBrowserOptions
9
+ HtEventsBrowserOptions,
10
+ CookieAttributes,
10
11
  } from '../types'
11
- import { CookieAttributes } from 'js-cookie'
12
12
 
13
13
  function getNewDestinations(destinations: Destination[], preferences: CategoryPreferences) {
14
14
  const newDestinations: Destination[] = []
@@ -63,7 +63,7 @@ interface Props {
63
63
  */
64
64
  mapCustomPreferences?: (
65
65
  destinations: Destination[],
66
- preferences: CategoryPreferences
66
+ preferences: CategoryPreferences,
67
67
  ) => { destinationPreferences: CategoryPreferences; customPreferences: CategoryPreferences }
68
68
 
69
69
  /**
@@ -82,7 +82,8 @@ interface Props {
82
82
  onError?: (err: Error) => void | Promise<void>
83
83
 
84
84
  /**
85
- * CDN to fetch list of integrations from
85
+ * Accepted for backward compatibility. Destinations are not fetched from the CDN,
86
+ * so this value is unused.
86
87
  */
87
88
  cdnHost?: string
88
89
 
@@ -120,7 +121,7 @@ interface RenderProps {
120
121
  saveConsent: (
121
122
  newPreferences?: CategoryPreferences | boolean,
122
123
  shouldReload?: boolean,
123
- devMode?: boolean
124
+ devMode?: boolean,
124
125
  ) => void
125
126
  }
126
127
 
@@ -139,7 +140,7 @@ const DEFAULT_CATEGORIES = {
139
140
  functional: false,
140
141
  marketingAndAnalytics: false,
141
142
  advertising: false,
142
- essential: false
143
+ essential: false,
143
144
  }
144
145
 
145
146
  export default class ConsentManagerBuilder extends Component<Props, State> {
@@ -150,10 +151,9 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
150
151
  onError: undefined,
151
152
  shouldRequireConsent: () => true,
152
153
  initialPreferences: {},
153
- cdnHost: 'cdn.hightouch-events.com',
154
154
  shouldReload: true,
155
155
  devMode: false,
156
- useDefaultCategories: false
156
+ useDefaultCategories: false,
157
157
  }
158
158
 
159
159
  state = {
@@ -165,7 +165,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
165
165
  isConsentRequired: true,
166
166
  havePreferencesChanged: false,
167
167
  workspaceAddedNewDestinations: false,
168
- useDefaultCategories: false
168
+ useDefaultCategories: false,
169
169
  }
170
170
 
171
171
  render() {
@@ -178,7 +178,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
178
178
  isConsentRequired,
179
179
  havePreferencesChanged,
180
180
  workspaceAddedNewDestinations,
181
- destinationPreferences
181
+ destinationPreferences,
182
182
  } = this.state
183
183
  if (isLoading) {
184
184
  return null
@@ -195,7 +195,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
195
195
  destinationPreferences,
196
196
  setPreferences: this.handleSetPreferences,
197
197
  resetPreferences: this.handleResetPreferences,
198
- saveConsent: this.handleSaveConsent
198
+ saveConsent: this.handleSaveConsent,
199
199
  })
200
200
  }
201
201
 
@@ -221,17 +221,17 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
221
221
  mapCustomPreferences,
222
222
  defaultDestinationBehavior,
223
223
  cookieName,
224
- cdnHost = ConsentManagerBuilder.defaultProps.cdnHost,
225
224
  shouldReload = ConsentManagerBuilder.defaultProps.shouldReload,
226
225
  devMode = ConsentManagerBuilder.defaultProps.devMode,
227
- useDefaultCategories = ConsentManagerBuilder.defaultProps.useDefaultCategories
226
+ useDefaultCategories = ConsentManagerBuilder.defaultProps.useDefaultCategories,
228
227
  } = this.props
229
228
 
230
229
  // TODO: add option to run mapCustomPreferences on load so that the destination preferences automatically get updated
231
230
  let { destinationPreferences, customPreferences } = loadPreferences(cookieName)
232
231
  const [isConsentRequired, destinations] = await Promise.all([
233
232
  shouldRequireConsent(),
234
- Promise.resolve([])
233
+ // We don't support fetching destinations from the CDN.
234
+ Promise.resolve([]),
235
235
  ])
236
236
  const newDestinations = getNewDestinations(destinations, destinationPreferences || {})
237
237
  const workspaceAddedNewDestinations =
@@ -241,10 +241,10 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
241
241
 
242
242
  let preferences: CategoryPreferences | undefined
243
243
  const initialPrefencesHaveValue = Object.values(initialPreferences || {}).some(
244
- v => v === true || v === false || v === 'N/A'
244
+ (v) => v === true || v === false || v === 'N/A',
245
245
  )
246
246
  const emptyCustomPreferecences = Object.values(customPreferences || {}).every(
247
- v => v === null || v === undefined || v === 'N/A'
247
+ (v) => v === null || v === undefined || v === 'N/A',
248
248
  )
249
249
 
250
250
  if (mapCustomPreferences) {
@@ -275,7 +275,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
275
275
  shouldReload,
276
276
  devMode,
277
277
  defaultDestinationBehavior,
278
- categoryPreferences: preferences
278
+ categoryPreferences: preferences,
279
279
  })
280
280
 
281
281
  this.setState({
@@ -285,17 +285,17 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
285
285
  preferences,
286
286
  isConsentRequired,
287
287
  destinationPreferences,
288
- workspaceAddedNewDestinations: Boolean(workspaceAddedNewDestinations)
288
+ workspaceAddedNewDestinations: Boolean(workspaceAddedNewDestinations),
289
289
  })
290
290
  }
291
291
 
292
292
  handleSetPreferences = (newPreferences: CategoryPreferences) => {
293
- this.setState(prevState => {
293
+ this.setState((prevState) => {
294
294
  const { destinations, preferences: existingPreferences } = prevState
295
295
  const preferences = this.mergePreferences({
296
296
  destinations,
297
297
  newPreferences,
298
- existingPreferences
298
+ existingPreferences,
299
299
  })
300
300
  return { ...prevState, preferences, havePreferencesChanged: true }
301
301
  })
@@ -318,7 +318,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
318
318
  handleSaveConsent = (
319
319
  newPreferences: CategoryPreferences | undefined,
320
320
  shouldReload: boolean,
321
- devMode?: boolean
321
+ devMode?: boolean,
322
322
  ) => {
323
323
  const {
324
324
  writeKey,
@@ -328,16 +328,16 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
328
328
  cookieExpires,
329
329
  cookieAttributes,
330
330
  mapCustomPreferences,
331
- defaultDestinationBehavior
331
+ defaultDestinationBehavior,
332
332
  } = this.props
333
333
 
334
- this.setState(prevState => {
334
+ this.setState((prevState) => {
335
335
  const { destinations, preferences: existingPreferences, isConsentRequired } = prevState
336
336
 
337
337
  let preferences = this.mergePreferences({
338
338
  destinations,
339
339
  newPreferences,
340
- existingPreferences
340
+ existingPreferences,
341
341
  })
342
342
 
343
343
  let destinationPreferences: CategoryPreferences
@@ -368,15 +368,16 @@ 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,
375
375
  cookieName,
376
376
  cookieExpires,
377
- cookieAttributes
377
+ cookieAttributes,
378
378
  })
379
379
  conditionallyLoadAnalytics({
380
+ consentUpdate,
380
381
  writeKey,
381
382
  options,
382
383
  destinations,
@@ -385,14 +386,14 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
385
386
  shouldReload,
386
387
  devMode,
387
388
  defaultDestinationBehavior,
388
- categoryPreferences: customPreferences
389
+ categoryPreferences: customPreferences,
389
390
  })
390
391
 
391
392
  return {
392
393
  ...prevState,
393
394
  destinationPreferences,
394
395
  preferences,
395
- newDestinations
396
+ newDestinations,
396
397
  }
397
398
  })
398
399
  }
@@ -415,7 +416,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
415
416
  } else if (newPreferences) {
416
417
  preferences = {
417
418
  ...existingPreferences,
418
- ...newPreferences
419
+ ...newPreferences,
419
420
  }
420
421
  } else {
421
422
  preferences = existingPreferences!
@@ -1,7 +1,7 @@
1
1
  // TODO: remove duplicate cookie library from bundle
2
- import cookies, { CookieAttributes } from 'js-cookie'
2
+ import cookies from 'js-cookie'
3
3
  import topDomain from '@segment/top-domain'
4
- import { WindowWithHtEvents, Preferences, CategoryPreferences } from '../types'
4
+ import { WindowWithHtEvents, Preferences, CategoryPreferences, CookieAttributes } from '../types'
5
5
  import { EventEmitter } from 'events'
6
6
 
7
7
  export const DEFAULT_COOKIE_NAME = 'ht-cm-preferences'
@@ -22,15 +22,21 @@ export interface PreferencesManager {
22
22
  // TODO: harden against invalid cookies
23
23
  // TODO: harden against different versions of cookies
24
24
  export function loadPreferences(cookieName?: string): Preferences {
25
- const preferences = cookies.getJSON(cookieName || DEFAULT_COOKIE_NAME) as PreferencesCookie
25
+ const raw = cookies.get(cookieName || DEFAULT_COOKIE_NAME)
26
+ if (!raw) {
27
+ return {}
28
+ }
26
29
 
27
- if (!preferences) {
30
+ let preferences: PreferencesCookie
31
+ try {
32
+ preferences = JSON.parse(raw) as PreferencesCookie
33
+ } catch {
28
34
  return {}
29
35
  }
30
36
 
31
37
  return {
32
38
  destinationPreferences: preferences.destination,
33
- customPreferences: preferences.custom
39
+ customPreferences: preferences.custom,
34
40
  }
35
41
  }
36
42
 
@@ -60,45 +66,68 @@ export function savePreferences({
60
66
  cookieDomain,
61
67
  cookieName,
62
68
  cookieExpires,
63
- cookieAttributes = {}
69
+ cookieAttributes = {},
64
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
+
65
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
+
66
100
  if (wd.htevents) {
67
- wd.htevents.track(
68
- 'Consent Updated',
101
+ return trackUpdate(
102
+ wd.htevents,
69
103
  {
70
104
  destinationPreferences,
71
105
  // use `categoryPreferences` here for consistency with `context.consent.categoryPreferences`
72
- categoryPreferences: customPreferences
106
+ categoryPreferences: customPreferences,
73
107
  },
74
108
  {
109
+ integrations: { All: false, ...destinationPreferences },
75
110
  // update `context.consent` here to immediately reflect the updated preferences
76
111
  consent: {
77
112
  destinationPreferences,
78
- categoryPreferences: customPreferences
79
- }
80
- }
113
+ categoryPreferences: customPreferences,
114
+ },
115
+ },
81
116
  )
82
117
  } else {
83
118
  console.warn('window.htevents not found...is the SDK snippet included on the page?')
119
+ return undefined
84
120
  }
121
+ }
85
122
 
86
- const domain = cookieDomain || topDomain(window.location.href)
87
- const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES
88
- const value = {
89
- version: 1,
90
- destination: destinationPreferences,
91
- custom: customPreferences
92
- } as PreferencesCookie
93
-
94
- cookies.set(cookieName || DEFAULT_COOKIE_NAME, value, {
95
- expires,
96
- domain,
97
- ...cookieAttributes
98
- })
99
-
100
- emitter.emit('preferencesSaved', {
101
- destinationPreferences,
102
- customPreferences
103
- })
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
+ }
104
133
  }