@ht-sdks/consent-manager 0.1.0 → 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 (98) hide show
  1. package/commonjs/__tests__/consent-manager/dialog.test.js +33 -0
  2. package/commonjs/__tests__/consent-manager-builder/analytics.test.js +53 -46
  3. package/commonjs/__tests__/consent-manager-builder/index.todo.js +48 -87
  4. package/commonjs/__tests__/consent-manager-builder/preferences.test.js +34 -27
  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 +3 -5
  16. package/commonjs/consent-manager-builder/index.js +30 -30
  17. package/commonjs/consent-manager-builder/preferences.js +22 -16
  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 +41 -34
  22. package/esm/__tests__/consent-manager-builder/index.todo.js +34 -73
  23. package/esm/__tests__/consent-manager-builder/preferences.test.js +30 -23
  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 +2 -4
  34. package/esm/consent-manager-builder/index.js +26 -26
  35. package/esm/consent-manager-builder/preferences.js +17 -11
  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 +40 -33
  42. package/src/__tests__/consent-manager-builder/index.todo.js +52 -95
  43. package/src/__tests__/consent-manager-builder/preferences.test.ts +31 -21
  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 +5 -5
  54. package/src/consent-manager-builder/index.tsx +28 -28
  55. package/src/consent-manager-builder/preferences.ts +20 -14
  56. package/src/index.ts +8 -4
  57. package/src/standalone.tsx +1 -1
  58. package/src/types.ts +8 -2
  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 +1 -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 +14 -10
  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
@@ -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,7 +66,7 @@ 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) {
@@ -69,15 +75,15 @@ export function savePreferences({
69
75
  {
70
76
  destinationPreferences,
71
77
  // use `categoryPreferences` here for consistency with `context.consent.categoryPreferences`
72
- categoryPreferences: customPreferences
78
+ categoryPreferences: customPreferences,
73
79
  },
74
80
  {
75
81
  // update `context.consent` here to immediately reflect the updated preferences
76
82
  consent: {
77
83
  destinationPreferences,
78
- categoryPreferences: customPreferences
79
- }
80
- }
84
+ categoryPreferences: customPreferences,
85
+ },
86
+ },
81
87
  )
82
88
  } else {
83
89
  console.warn('window.htevents not found...is the SDK snippet included on the page?')
@@ -88,17 +94,17 @@ export function savePreferences({
88
94
  const value = {
89
95
  version: 1,
90
96
  destination: destinationPreferences,
91
- custom: customPreferences
97
+ custom: customPreferences,
92
98
  } as PreferencesCookie
93
99
 
94
- cookies.set(cookieName || DEFAULT_COOKIE_NAME, value, {
100
+ cookies.set(cookieName || DEFAULT_COOKIE_NAME, JSON.stringify(value), {
95
101
  expires,
96
102
  domain,
97
- ...cookieAttributes
103
+ ...cookieAttributes,
98
104
  })
99
105
 
100
106
  emitter.emit('preferencesSaved', {
101
107
  destinationPreferences,
102
- customPreferences
108
+ customPreferences,
103
109
  })
104
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>
@@ -33,7 +35,7 @@ export type WindowWithHtEvents = Window &
33
35
  export type WindowWithConsentManagerConfig = Window &
34
36
  typeof globalThis & {
35
37
  consentManagerConfig?: (
36
- args: StandaloneConsentManagerParams
38
+ args: StandaloneConsentManagerParams,
37
39
  ) => ConsentManagerInput | ConsentManagerInput
38
40
  }
39
41
 
@@ -139,6 +141,10 @@ export interface ConsentManagerProps {
139
141
  initialPreferences?: CategoryPreferences
140
142
  customCategories?: CustomCategories
141
143
  defaultDestinationBehavior?: DefaultDestinationBehavior
144
+ /**
145
+ * Accepted for backward compatibility. Destinations are not fetched from the CDN,
146
+ * so this value is unused.
147
+ */
142
148
  cdnHost?: string
143
149
  preferencesDialogTemplate?: PreferenceDialogTemplate
144
150
  }