@ht-sdks/consent-manager 0.0.1 → 0.2.0

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 (99) hide show
  1. package/README.md +5 -19
  2. package/commonjs/__tests__/consent-manager/dialog.test.js +33 -0
  3. package/commonjs/__tests__/consent-manager-builder/analytics.test.js +53 -46
  4. package/commonjs/__tests__/consent-manager-builder/index.todo.js +52 -88
  5. package/commonjs/__tests__/consent-manager-builder/preferences.test.js +59 -71
  6. package/commonjs/__tests__/index.test.js +12 -12
  7. package/commonjs/consent-manager/banner.js +35 -19
  8. package/commonjs/consent-manager/buttons.js +5 -5
  9. package/commonjs/consent-manager/cancel-dialog.js +26 -10
  10. package/commonjs/consent-manager/categories.js +2 -2
  11. package/commonjs/consent-manager/container.js +2 -3
  12. package/commonjs/consent-manager/dialog.js +39 -23
  13. package/commonjs/consent-manager/font-styles.js +2 -2
  14. package/commonjs/consent-manager/index.js +41 -25
  15. package/commonjs/consent-manager/preference-dialog.js +36 -20
  16. package/commonjs/consent-manager-builder/analytics.js +3 -7
  17. package/commonjs/consent-manager-builder/index.js +30 -30
  18. package/commonjs/consent-manager-builder/preferences.js +28 -21
  19. package/commonjs/index.js +5 -4
  20. package/commonjs/standalone.js +25 -11
  21. package/esm/__tests__/consent-manager/dialog.test.js +28 -0
  22. package/esm/__tests__/consent-manager-builder/analytics.test.js +41 -34
  23. package/esm/__tests__/consent-manager-builder/index.todo.js +38 -74
  24. package/esm/__tests__/consent-manager-builder/preferences.test.js +55 -67
  25. package/esm/consent-manager/banner.js +5 -3
  26. package/esm/consent-manager/buttons.js +4 -4
  27. package/esm/consent-manager/cancel-dialog.js +4 -2
  28. package/esm/consent-manager/categories.js +2 -2
  29. package/esm/consent-manager/container.js +1 -1
  30. package/esm/consent-manager/dialog.js +7 -5
  31. package/esm/consent-manager/font-styles.js +2 -2
  32. package/esm/consent-manager/index.js +19 -17
  33. package/esm/consent-manager/preference-dialog.js +7 -5
  34. package/esm/consent-manager-builder/analytics.js +2 -6
  35. package/esm/consent-manager-builder/index.js +26 -26
  36. package/esm/consent-manager-builder/preferences.js +23 -16
  37. package/esm/index.js +4 -3
  38. package/esm/standalone.js +3 -3
  39. package/esm/types.js +1 -0
  40. package/package.json +62 -68
  41. package/src/__tests__/consent-manager/dialog.test.tsx +42 -0
  42. package/src/__tests__/consent-manager-builder/analytics.test.ts +40 -33
  43. package/src/__tests__/consent-manager-builder/index.todo.js +56 -96
  44. package/src/__tests__/consent-manager-builder/preferences.test.ts +64 -75
  45. package/src/consent-manager/banner.tsx +11 -11
  46. package/src/consent-manager/buttons.tsx +26 -10
  47. package/src/consent-manager/cancel-dialog.tsx +1 -1
  48. package/src/consent-manager/categories.ts +2 -2
  49. package/src/consent-manager/container.tsx +14 -17
  50. package/src/consent-manager/dialog.tsx +4 -4
  51. package/src/consent-manager/font-styles.tsx +12 -2
  52. package/src/consent-manager/index.tsx +27 -29
  53. package/src/consent-manager/preference-dialog.tsx +13 -11
  54. package/src/consent-manager-builder/analytics.ts +8 -5
  55. package/src/consent-manager-builder/index.tsx +28 -28
  56. package/src/consent-manager-builder/preferences.ts +31 -21
  57. package/src/index.ts +8 -4
  58. package/src/standalone.tsx +1 -1
  59. package/src/types.ts +8 -3
  60. package/standalone/consent-manager.js +5 -10
  61. package/standalone/consent-manager.js.map +1 -1
  62. package/types/consent-manager/banner.d.ts +1 -1
  63. package/types/consent-manager/buttons.d.ts +6 -7
  64. package/types/consent-manager/cancel-dialog.d.ts +1 -1
  65. package/types/consent-manager/index.d.ts +3 -3
  66. package/types/consent-manager/preference-dialog.d.ts +1 -1
  67. package/types/consent-manager-builder/analytics.d.ts +1 -1
  68. package/types/consent-manager-builder/index.d.ts +7 -8
  69. package/types/consent-manager-builder/preferences.d.ts +4 -6
  70. package/types/index.d.ts +1 -1
  71. package/types/types.d.ts +14 -11
  72. package/commonjs/__tests__/consent-manager-builder/fetch-destinations.test.js +0 -155
  73. package/commonjs/consent-manager-builder/fetch-destinations.js +0 -104
  74. package/esm/__tests__/consent-manager-builder/fetch-destinations.test.js +0 -150
  75. package/esm/consent-manager-builder/fetch-destinations.js +0 -98
  76. package/src/__tests__/consent-manager-builder/fetch-destinations.test.ts +0 -89
  77. package/src/consent-manager-builder/fetch-destinations.ts +0 -50
  78. package/types/consent-manager-builder/fetch-destinations.d.ts +0 -2
  79. package/types/src/__tests__/consent-manager-builder/analytics.test.d.ts +0 -1
  80. package/types/src/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +0 -1
  81. package/types/src/__tests__/consent-manager-builder/preferences.test.d.ts +0 -1
  82. package/types/src/__tests__/index.test.d.ts +0 -1
  83. package/types/src/consent-manager/banner.d.ts +0 -21
  84. package/types/src/consent-manager/buttons.d.ts +0 -13
  85. package/types/src/consent-manager/cancel-dialog.d.ts +0 -19
  86. package/types/src/consent-manager/categories.d.ts +0 -3
  87. package/types/src/consent-manager/container.d.ts +0 -35
  88. package/types/src/consent-manager/dialog.d.ts +0 -29
  89. package/types/src/consent-manager/font-styles.d.ts +0 -2
  90. package/types/src/consent-manager/index.d.ts +0 -35
  91. package/types/src/consent-manager/preference-dialog.d.ts +0 -32
  92. package/types/src/consent-manager-builder/analytics.d.ts +0 -14
  93. package/types/src/consent-manager-builder/fetch-destinations.d.ts +0 -2
  94. package/types/src/consent-manager-builder/index.d.ts +0 -126
  95. package/types/src/consent-manager-builder/preferences.d.ts +0 -26
  96. package/types/src/index.d.ts +0 -7
  97. package/types/src/standalone.d.ts +0 -5
  98. package/types/src/types.d.ts +0 -131
  99. /package/types/__tests__/{consent-manager-builder/fetch-destinations.test.d.ts → consent-manager/dialog.test.d.ts} +0 -0
@@ -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
@@ -374,7 +374,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
374
374
  cookieDomain,
375
375
  cookieName,
376
376
  cookieExpires,
377
- cookieAttributes
377
+ cookieAttributes,
378
378
  })
379
379
  conditionallyLoadAnalytics({
380
380
  writeKey,
@@ -385,14 +385,14 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
385
385
  shouldReload,
386
386
  devMode,
387
387
  defaultDestinationBehavior,
388
- categoryPreferences: customPreferences
388
+ categoryPreferences: customPreferences,
389
389
  })
390
390
 
391
391
  return {
392
392
  ...prevState,
393
393
  destinationPreferences,
394
394
  preferences,
395
- newDestinations
395
+ newDestinations,
396
396
  }
397
397
  })
398
398
  }
@@ -415,7 +415,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
415
415
  } else if (newPreferences) {
416
416
  preferences = {
417
417
  ...existingPreferences,
418
- ...newPreferences
418
+ ...newPreferences,
419
419
  }
420
420
  } else {
421
421
  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,21 +66,25 @@ export function savePreferences({
60
66
  cookieDomain,
61
67
  cookieName,
62
68
  cookieExpires,
63
- cookieAttributes = {}
69
+ cookieAttributes = {},
64
70
  }: SavePreferences) {
65
71
  const wd = window as WindowWithHtEvents
66
72
  if (wd.htevents) {
67
- wd.htevents.identify({
68
- destinationTrackingPreferences: destinationPreferences,
69
- // use `categoryTrackingPreferences` here for consistency with `context.consent.categoryPreferences`
70
- categoryTrackingPreferences: customPreferences
71
- })
72
-
73
- wd.htevents.track('Consent Updated', {
74
- destinationTrackingPreferences: destinationPreferences,
75
- // use `categoryTrackingPreferences` here for consistency with `context.consent.categoryPreferences`
76
- categoryTrackingPreferences: customPreferences
77
- })
73
+ wd.htevents.track(
74
+ 'Consent Updated',
75
+ {
76
+ destinationPreferences,
77
+ // use `categoryPreferences` here for consistency with `context.consent.categoryPreferences`
78
+ categoryPreferences: customPreferences,
79
+ },
80
+ {
81
+ // update `context.consent` here to immediately reflect the updated preferences
82
+ consent: {
83
+ destinationPreferences,
84
+ categoryPreferences: customPreferences,
85
+ },
86
+ },
87
+ )
78
88
  } else {
79
89
  console.warn('window.htevents not found...is the SDK snippet included on the page?')
80
90
  }
@@ -84,17 +94,17 @@ export function savePreferences({
84
94
  const value = {
85
95
  version: 1,
86
96
  destination: destinationPreferences,
87
- custom: customPreferences
97
+ custom: customPreferences,
88
98
  } as PreferencesCookie
89
99
 
90
- cookies.set(cookieName || DEFAULT_COOKIE_NAME, value, {
100
+ cookies.set(cookieName || DEFAULT_COOKIE_NAME, JSON.stringify(value), {
91
101
  expires,
92
102
  domain,
93
- ...cookieAttributes
103
+ ...cookieAttributes,
94
104
  })
95
105
 
96
106
  emitter.emit('preferencesSaved', {
97
107
  destinationPreferences,
98
- customPreferences
108
+ customPreferences,
99
109
  })
100
110
  }
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ export { openDialog as openConsentManager } from './consent-manager/container'
5
5
  export {
6
6
  loadPreferences,
7
7
  savePreferences,
8
- onPreferencesSaved
8
+ onPreferencesSaved,
9
9
  } from './consent-manager-builder/preferences'
10
10
 
11
11
  export const ConsentManagerBuilder = CMB
@@ -15,12 +15,16 @@ type Nav = Navigator & {
15
15
  msDoNotTrack?: Navigator['doNotTrack']
16
16
  }
17
17
 
18
+ type WindowWithDoNotTrack = Window & {
19
+ doNotTrack?: Navigator['doNotTrack']
20
+ }
21
+
18
22
  export function doNotTrack(): boolean | null {
19
-
20
- if (typeof window !== 'undefined' && (window.navigator || navigator)) {
23
+ if (typeof window !== 'undefined' && (window.navigator || navigator)) {
21
24
  const nav = navigator as Nav
25
+ const win = window as WindowWithDoNotTrack
22
26
 
23
- let doNotTrackValue = nav.doNotTrack || window.doNotTrack || nav.msDoNotTrack
27
+ let doNotTrackValue = nav.doNotTrack || win.doNotTrack || nav.msDoNotTrack
24
28
 
25
29
  // Normalise Firefox < 32
26
30
  // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack
@@ -22,7 +22,7 @@ if (localWindow.consentManagerConfig && typeof localWindow.consentManagerConfig
22
22
  doNotTrack,
23
23
  inEU,
24
24
  preferences,
25
- inRegions
25
+ inRegions,
26
26
  })
27
27
  containerRef = props.container
28
28
  } else {
package/src/types.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { CloseBehaviorFunction } from './consent-manager/container'
2
2
  import { PreferencesManager } from './consent-manager-builder/preferences'
3
- import { CookieAttributes } from 'js-cookie'
3
+ import Cookies from 'js-cookie'
4
+
5
+ export type CookieAttributes = NonNullable<Parameters<typeof Cookies.set>[2]>
4
6
 
5
7
  export type HtEventsBrowserOptions = {
6
8
  integrations?: Record<string, boolean>
@@ -11,7 +13,6 @@ export type HtEventsBrowserOptions = {
11
13
  export type HtEventsBrowser = {
12
14
  initialized: boolean
13
15
  load: (writeKey: string, options?: HtEventsBrowserOptions) => void
14
- identify: (properties: Record<string, any>) => void
15
16
  track: (event: string, properties: Record<string, any>, options?: any, callback?: any) => void
16
17
  addSourceMiddleware: (middleware: Middleware) => void
17
18
  }
@@ -34,7 +35,7 @@ export type WindowWithHtEvents = Window &
34
35
  export type WindowWithConsentManagerConfig = Window &
35
36
  typeof globalThis & {
36
37
  consentManagerConfig?: (
37
- args: StandaloneConsentManagerParams
38
+ args: StandaloneConsentManagerParams,
38
39
  ) => ConsentManagerInput | ConsentManagerInput
39
40
  }
40
41
 
@@ -140,6 +141,10 @@ export interface ConsentManagerProps {
140
141
  initialPreferences?: CategoryPreferences
141
142
  customCategories?: CustomCategories
142
143
  defaultDestinationBehavior?: DefaultDestinationBehavior
144
+ /**
145
+ * Accepted for backward compatibility. Destinations are not fetched from the CDN,
146
+ * so this value is unused.
147
+ */
143
148
  cdnHost?: string
144
149
  preferencesDialogTemplate?: PreferenceDialogTemplate
145
150
  }