@ht-sdks/consent-manager 0.0.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 (110) hide show
  1. package/LICENSE +45 -0
  2. package/README.md +851 -0
  3. package/commonjs/__tests__/consent-manager-builder/analytics.test.js +250 -0
  4. package/commonjs/__tests__/consent-manager-builder/fetch-destinations.test.js +155 -0
  5. package/commonjs/__tests__/consent-manager-builder/index.todo.js +327 -0
  6. package/commonjs/__tests__/consent-manager-builder/preferences.test.js +129 -0
  7. package/commonjs/__tests__/index.test.js +57 -0
  8. package/commonjs/consent-manager/banner.js +89 -0
  9. package/commonjs/consent-manager/buttons.js +18 -0
  10. package/commonjs/consent-manager/cancel-dialog.js +76 -0
  11. package/commonjs/consent-manager/categories.js +29 -0
  12. package/commonjs/consent-manager/container.js +169 -0
  13. package/commonjs/consent-manager/dialog.js +129 -0
  14. package/commonjs/consent-manager/font-styles.js +10 -0
  15. package/commonjs/consent-manager/index.js +244 -0
  16. package/commonjs/consent-manager/preference-dialog.js +178 -0
  17. package/commonjs/consent-manager-builder/analytics.js +76 -0
  18. package/commonjs/consent-manager-builder/fetch-destinations.js +104 -0
  19. package/commonjs/consent-manager-builder/index.js +330 -0
  20. package/commonjs/consent-manager-builder/preferences.js +82 -0
  21. package/commonjs/index.js +39 -0
  22. package/commonjs/standalone.js +86 -0
  23. package/commonjs/types.js +3 -0
  24. package/esm/__tests__/consent-manager-builder/analytics.test.js +245 -0
  25. package/esm/__tests__/consent-manager-builder/fetch-destinations.test.js +150 -0
  26. package/esm/__tests__/consent-manager-builder/index.todo.js +322 -0
  27. package/esm/__tests__/consent-manager-builder/preferences.test.js +124 -0
  28. package/esm/__tests__/index.test.js +55 -0
  29. package/esm/consent-manager/banner.js +65 -0
  30. package/esm/consent-manager/buttons.js +12 -0
  31. package/esm/consent-manager/cancel-dialog.js +52 -0
  32. package/esm/consent-manager/categories.js +26 -0
  33. package/esm/consent-manager/container.js +162 -0
  34. package/esm/consent-manager/dialog.js +105 -0
  35. package/esm/consent-manager/font-styles.js +8 -0
  36. package/esm/consent-manager/index.js +220 -0
  37. package/esm/consent-manager/preference-dialog.js +154 -0
  38. package/esm/consent-manager-builder/analytics.js +73 -0
  39. package/esm/consent-manager-builder/fetch-destinations.js +98 -0
  40. package/esm/consent-manager-builder/index.js +325 -0
  41. package/esm/consent-manager-builder/preferences.js +73 -0
  42. package/esm/index.js +28 -0
  43. package/esm/standalone.js +58 -0
  44. package/esm/types.js +1 -0
  45. package/package.json +168 -0
  46. package/src/__tests__/consent-manager-builder/analytics.test.ts +279 -0
  47. package/src/__tests__/consent-manager-builder/fetch-destinations.test.ts +89 -0
  48. package/src/__tests__/consent-manager-builder/index.todo.js +386 -0
  49. package/src/__tests__/consent-manager-builder/preferences.test.ts +156 -0
  50. package/src/__tests__/index.test.ts +68 -0
  51. package/src/consent-manager/banner.tsx +227 -0
  52. package/src/consent-manager/buttons.tsx +78 -0
  53. package/src/consent-manager/cancel-dialog.tsx +68 -0
  54. package/src/consent-manager/categories.ts +27 -0
  55. package/src/consent-manager/container.tsx +282 -0
  56. package/src/consent-manager/dialog.tsx +236 -0
  57. package/src/consent-manager/font-styles.tsx +14 -0
  58. package/src/consent-manager/index.tsx +327 -0
  59. package/src/consent-manager/preference-dialog.tsx +371 -0
  60. package/src/consent-manager-builder/analytics.ts +104 -0
  61. package/src/consent-manager-builder/fetch-destinations.ts +50 -0
  62. package/src/consent-manager-builder/index.tsx +426 -0
  63. package/src/consent-manager-builder/preferences.ts +100 -0
  64. package/src/index.ts +42 -0
  65. package/src/standalone.tsx +57 -0
  66. package/src/styled.d.ts +7 -0
  67. package/src/types.ts +151 -0
  68. package/standalone/consent-manager.js +23 -0
  69. package/standalone/consent-manager.js.map +1 -0
  70. package/types/__tests__/consent-manager-builder/analytics.test.d.ts +1 -0
  71. package/types/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +1 -0
  72. package/types/__tests__/consent-manager-builder/index.todo.d.ts +1 -0
  73. package/types/__tests__/consent-manager-builder/preferences.test.d.ts +1 -0
  74. package/types/__tests__/index.test.d.ts +1 -0
  75. package/types/consent-manager/banner.d.ts +21 -0
  76. package/types/consent-manager/buttons.d.ts +13 -0
  77. package/types/consent-manager/cancel-dialog.d.ts +19 -0
  78. package/types/consent-manager/categories.d.ts +3 -0
  79. package/types/consent-manager/container.d.ts +35 -0
  80. package/types/consent-manager/dialog.d.ts +29 -0
  81. package/types/consent-manager/font-styles.d.ts +2 -0
  82. package/types/consent-manager/index.d.ts +35 -0
  83. package/types/consent-manager/preference-dialog.d.ts +32 -0
  84. package/types/consent-manager-builder/analytics.d.ts +14 -0
  85. package/types/consent-manager-builder/fetch-destinations.d.ts +2 -0
  86. package/types/consent-manager-builder/index.d.ts +126 -0
  87. package/types/consent-manager-builder/preferences.d.ts +26 -0
  88. package/types/index.d.ts +7 -0
  89. package/types/src/__tests__/consent-manager-builder/analytics.test.d.ts +1 -0
  90. package/types/src/__tests__/consent-manager-builder/fetch-destinations.test.d.ts +1 -0
  91. package/types/src/__tests__/consent-manager-builder/preferences.test.d.ts +1 -0
  92. package/types/src/__tests__/index.test.d.ts +1 -0
  93. package/types/src/consent-manager/banner.d.ts +21 -0
  94. package/types/src/consent-manager/buttons.d.ts +13 -0
  95. package/types/src/consent-manager/cancel-dialog.d.ts +19 -0
  96. package/types/src/consent-manager/categories.d.ts +3 -0
  97. package/types/src/consent-manager/container.d.ts +35 -0
  98. package/types/src/consent-manager/dialog.d.ts +29 -0
  99. package/types/src/consent-manager/font-styles.d.ts +2 -0
  100. package/types/src/consent-manager/index.d.ts +35 -0
  101. package/types/src/consent-manager/preference-dialog.d.ts +32 -0
  102. package/types/src/consent-manager-builder/analytics.d.ts +14 -0
  103. package/types/src/consent-manager-builder/fetch-destinations.d.ts +2 -0
  104. package/types/src/consent-manager-builder/index.d.ts +126 -0
  105. package/types/src/consent-manager-builder/preferences.d.ts +26 -0
  106. package/types/src/index.d.ts +7 -0
  107. package/types/src/standalone.d.ts +5 -0
  108. package/types/src/types.d.ts +131 -0
  109. package/types/standalone.d.ts +5 -0
  110. package/types/types.d.ts +131 -0
@@ -0,0 +1,426 @@
1
+ import { Component } from 'react'
2
+ import { loadPreferences, savePreferences } from './preferences'
3
+ import conditionallyLoadAnalytics from './analytics'
4
+ import {
5
+ Destination,
6
+ CategoryPreferences,
7
+ CustomCategories,
8
+ DefaultDestinationBehavior,
9
+ HtEventsBrowserOptions
10
+ } from '../types'
11
+ import { CookieAttributes } from 'js-cookie'
12
+
13
+ function getNewDestinations(destinations: Destination[], preferences: CategoryPreferences) {
14
+ const newDestinations: Destination[] = []
15
+
16
+ // If there are no preferences then all destinations are new
17
+ if (!preferences) {
18
+ return destinations
19
+ }
20
+
21
+ for (const destination of destinations) {
22
+ if (preferences[destination.id] === undefined) {
23
+ newDestinations.push(destination)
24
+ }
25
+ }
26
+
27
+ return newDestinations
28
+ }
29
+
30
+ interface Props {
31
+ /** Your Hightouch Write key for your website */
32
+ writeKey: string
33
+
34
+ /** Options to pass to Hightouch Events Browser SDK */
35
+ options?: HtEventsBrowserOptions
36
+
37
+ cookieDomain?: string
38
+ cookieName?: string
39
+ cookieAttributes?: CookieAttributes
40
+
41
+ /**
42
+ * Number of days until the preferences cookie should expire
43
+ */
44
+ cookieExpires?: number
45
+
46
+ /**
47
+ * An initial selection of Preferences
48
+ */
49
+ initialPreferences?: CategoryPreferences
50
+
51
+ /**
52
+ * Provide a function to define whether or not consent should be required
53
+ */
54
+ shouldRequireConsent?: () => Promise<boolean> | boolean
55
+
56
+ /**
57
+ * Render props for the Consent Manager builder
58
+ */
59
+ children: (props: RenderProps) => React.ReactElement
60
+
61
+ /**
62
+ * Allows for customizing how to show different categories of consent.
63
+ */
64
+ mapCustomPreferences?: (
65
+ destinations: Destination[],
66
+ preferences: CategoryPreferences
67
+ ) => { destinationPreferences: CategoryPreferences; customPreferences: CategoryPreferences }
68
+
69
+ /**
70
+ * Allows for adding custom consent categories by mapping a custom category to Hightouch integrations
71
+ */
72
+ customCategories?: CustomCategories
73
+
74
+ /**
75
+ * Specified default behavior for when new destinations are detected on the source(s) of this consent manager.
76
+ */
77
+ defaultDestinationBehavior?: DefaultDestinationBehavior
78
+
79
+ /**
80
+ * A callback for dealing with errors in the Consent Manager
81
+ */
82
+ onError?: (err: Error) => void | Promise<void>
83
+
84
+ /**
85
+ * CDN to fetch list of integrations from
86
+ */
87
+ cdnHost?: string
88
+
89
+ /**
90
+ * Default true
91
+ * Reload the page if the trackers have already been initialized so that
92
+ * the user's new preferences can take effect.
93
+ */
94
+ shouldReload?: boolean
95
+
96
+ /**
97
+ * Default false
98
+ * Disable htevents.load for local testing.
99
+ */
100
+ devMode?: boolean
101
+
102
+ /**
103
+ * Default false
104
+ * Use default categories set by Consent Manager instead of destinations
105
+ */
106
+ useDefaultCategories?: boolean
107
+ }
108
+
109
+ interface RenderProps {
110
+ destinations: Destination[]
111
+ newDestinations: Destination[]
112
+ preferences: CategoryPreferences
113
+ destinationPreferences: CategoryPreferences
114
+ isConsentRequired: boolean
115
+ customCategories?: CustomCategories
116
+ havePreferencesChanged: boolean
117
+ workspaceAddedNewDestinations: boolean
118
+ setPreferences: (newPreferences: CategoryPreferences) => void
119
+ resetPreferences: () => void
120
+ saveConsent: (
121
+ newPreferences?: CategoryPreferences | boolean,
122
+ shouldReload?: boolean,
123
+ devMode?: boolean
124
+ ) => void
125
+ }
126
+
127
+ interface State {
128
+ isLoading: boolean
129
+ destinations: Destination[]
130
+ newDestinations: Destination[]
131
+ preferences?: CategoryPreferences
132
+ destinationPreferences?: CategoryPreferences
133
+ isConsentRequired: boolean
134
+ havePreferencesChanged: boolean
135
+ workspaceAddedNewDestinations: boolean
136
+ }
137
+
138
+ const DEFAULT_CATEGORIES = {
139
+ functional: false,
140
+ marketingAndAnalytics: false,
141
+ advertising: false,
142
+ essential: false
143
+ }
144
+
145
+ export default class ConsentManagerBuilder extends Component<Props, State> {
146
+ static displayName = 'ConsentManagerBuilder'
147
+
148
+ static defaultProps = {
149
+ options: undefined,
150
+ onError: undefined,
151
+ shouldRequireConsent: () => true,
152
+ initialPreferences: {},
153
+ cdnHost: 'cdn.hightouch-events.com',
154
+ shouldReload: true,
155
+ devMode: false,
156
+ useDefaultCategories: false
157
+ }
158
+
159
+ state = {
160
+ isLoading: true,
161
+ destinations: [],
162
+ newDestinations: [],
163
+ preferences: {},
164
+ destinationPreferences: {},
165
+ isConsentRequired: true,
166
+ havePreferencesChanged: false,
167
+ workspaceAddedNewDestinations: false,
168
+ useDefaultCategories: false
169
+ }
170
+
171
+ render() {
172
+ const { children, customCategories } = this.props
173
+ const {
174
+ isLoading,
175
+ destinations,
176
+ preferences,
177
+ newDestinations,
178
+ isConsentRequired,
179
+ havePreferencesChanged,
180
+ workspaceAddedNewDestinations,
181
+ destinationPreferences
182
+ } = this.state
183
+ if (isLoading) {
184
+ return null
185
+ }
186
+
187
+ return children({
188
+ destinations,
189
+ customCategories,
190
+ newDestinations,
191
+ preferences,
192
+ isConsentRequired,
193
+ havePreferencesChanged,
194
+ workspaceAddedNewDestinations,
195
+ destinationPreferences,
196
+ setPreferences: this.handleSetPreferences,
197
+ resetPreferences: this.handleResetPreferences,
198
+ saveConsent: this.handleSaveConsent
199
+ })
200
+ }
201
+
202
+ async componentDidMount() {
203
+ const { onError } = this.props
204
+ if (onError && typeof onError === 'function') {
205
+ try {
206
+ await this.initialise()
207
+ } catch (e) {
208
+ await onError(e)
209
+ }
210
+ } else {
211
+ await this.initialise()
212
+ }
213
+ }
214
+
215
+ initialise = async () => {
216
+ const {
217
+ writeKey,
218
+ options,
219
+ shouldRequireConsent = ConsentManagerBuilder.defaultProps.shouldRequireConsent,
220
+ initialPreferences,
221
+ mapCustomPreferences,
222
+ defaultDestinationBehavior,
223
+ cookieName,
224
+ cdnHost = ConsentManagerBuilder.defaultProps.cdnHost,
225
+ shouldReload = ConsentManagerBuilder.defaultProps.shouldReload,
226
+ devMode = ConsentManagerBuilder.defaultProps.devMode,
227
+ useDefaultCategories = ConsentManagerBuilder.defaultProps.useDefaultCategories
228
+ } = this.props
229
+
230
+ // TODO: add option to run mapCustomPreferences on load so that the destination preferences automatically get updated
231
+ let { destinationPreferences, customPreferences } = loadPreferences(cookieName)
232
+ const [isConsentRequired, destinations] = await Promise.all([
233
+ shouldRequireConsent(),
234
+ Promise.resolve([])
235
+ ])
236
+ const newDestinations = getNewDestinations(destinations, destinationPreferences || {})
237
+ const workspaceAddedNewDestinations =
238
+ destinationPreferences &&
239
+ Object.keys(destinationPreferences).length > 0 &&
240
+ newDestinations.length > 0
241
+
242
+ let preferences: CategoryPreferences | undefined
243
+ const initialPrefencesHaveValue = Object.values(initialPreferences || {}).some(
244
+ v => v === true || v === false || v === 'N/A'
245
+ )
246
+ const emptyCustomPreferecences = Object.values(customPreferences || {}).every(
247
+ v => v === null || v === undefined || v === 'N/A'
248
+ )
249
+
250
+ if (mapCustomPreferences) {
251
+ preferences = useDefaultCategories
252
+ ? DEFAULT_CATEGORIES
253
+ : customPreferences || initialPreferences || {}
254
+ if (
255
+ (initialPrefencesHaveValue && emptyCustomPreferecences) ||
256
+ (defaultDestinationBehavior === 'imply' && workspaceAddedNewDestinations)
257
+ ) {
258
+ const mapped = mapCustomPreferences(destinations, preferences)
259
+ destinationPreferences = mapped.destinationPreferences
260
+ customPreferences = mapped.customPreferences
261
+ preferences = customPreferences
262
+ }
263
+ } else {
264
+ preferences = useDefaultCategories
265
+ ? DEFAULT_CATEGORIES
266
+ : destinationPreferences || initialPreferences
267
+ }
268
+
269
+ conditionallyLoadAnalytics({
270
+ writeKey,
271
+ options,
272
+ destinations,
273
+ destinationPreferences,
274
+ isConsentRequired,
275
+ shouldReload,
276
+ devMode,
277
+ defaultDestinationBehavior,
278
+ categoryPreferences: preferences
279
+ })
280
+
281
+ this.setState({
282
+ isLoading: false,
283
+ destinations,
284
+ newDestinations,
285
+ preferences,
286
+ isConsentRequired,
287
+ destinationPreferences,
288
+ workspaceAddedNewDestinations: Boolean(workspaceAddedNewDestinations)
289
+ })
290
+ }
291
+
292
+ handleSetPreferences = (newPreferences: CategoryPreferences) => {
293
+ this.setState(prevState => {
294
+ const { destinations, preferences: existingPreferences } = prevState
295
+ const preferences = this.mergePreferences({
296
+ destinations,
297
+ newPreferences,
298
+ existingPreferences
299
+ })
300
+ return { ...prevState, preferences, havePreferencesChanged: true }
301
+ })
302
+ }
303
+
304
+ handleResetPreferences = () => {
305
+ const { initialPreferences, mapCustomPreferences, cookieName } = this.props
306
+ const { destinationPreferences, customPreferences } = loadPreferences(cookieName)
307
+
308
+ let preferences: CategoryPreferences | undefined
309
+ if (mapCustomPreferences) {
310
+ preferences = customPreferences || initialPreferences
311
+ } else {
312
+ preferences = destinationPreferences || initialPreferences
313
+ }
314
+
315
+ this.setState({ preferences })
316
+ }
317
+
318
+ handleSaveConsent = (
319
+ newPreferences: CategoryPreferences | undefined,
320
+ shouldReload: boolean,
321
+ devMode?: boolean
322
+ ) => {
323
+ const {
324
+ writeKey,
325
+ options,
326
+ cookieDomain,
327
+ cookieName,
328
+ cookieExpires,
329
+ cookieAttributes,
330
+ mapCustomPreferences,
331
+ defaultDestinationBehavior
332
+ } = this.props
333
+
334
+ this.setState(prevState => {
335
+ const { destinations, preferences: existingPreferences, isConsentRequired } = prevState
336
+
337
+ let preferences = this.mergePreferences({
338
+ destinations,
339
+ newPreferences,
340
+ existingPreferences
341
+ })
342
+
343
+ let destinationPreferences: CategoryPreferences
344
+ let customPreferences: CategoryPreferences | undefined
345
+
346
+ if (mapCustomPreferences) {
347
+ const custom = mapCustomPreferences(destinations, preferences)
348
+ destinationPreferences = custom.destinationPreferences
349
+ customPreferences = custom.customPreferences
350
+
351
+ if (customPreferences) {
352
+ // Allow the customPreferences to be updated from mapCustomPreferences
353
+ preferences = customPreferences
354
+ } else {
355
+ // Make returning the customPreferences from mapCustomPreferences optional
356
+ customPreferences = preferences
357
+ }
358
+ } else {
359
+ destinationPreferences = preferences
360
+ }
361
+
362
+ const newDestinations = getNewDestinations(destinations, destinationPreferences)
363
+
364
+ if (
365
+ prevState.havePreferencesChanged ||
366
+ newDestinations.length > 0 ||
367
+ typeof newPreferences === 'boolean'
368
+ ) {
369
+ shouldReload = true
370
+ }
371
+ savePreferences({
372
+ destinationPreferences,
373
+ customPreferences,
374
+ cookieDomain,
375
+ cookieName,
376
+ cookieExpires,
377
+ cookieAttributes
378
+ })
379
+ conditionallyLoadAnalytics({
380
+ writeKey,
381
+ options,
382
+ destinations,
383
+ destinationPreferences,
384
+ isConsentRequired,
385
+ shouldReload,
386
+ devMode,
387
+ defaultDestinationBehavior,
388
+ categoryPreferences: customPreferences
389
+ })
390
+
391
+ return {
392
+ ...prevState,
393
+ destinationPreferences,
394
+ preferences,
395
+ newDestinations
396
+ }
397
+ })
398
+ }
399
+
400
+ mergePreferences = (args: {
401
+ destinations: Destination[]
402
+ existingPreferences?: CategoryPreferences
403
+ newPreferences?: CategoryPreferences
404
+ }) => {
405
+ const { destinations, existingPreferences, newPreferences } = args
406
+
407
+ let preferences: CategoryPreferences
408
+
409
+ if (typeof newPreferences === 'boolean') {
410
+ const destinationPreferences = {}
411
+ for (const destination of destinations) {
412
+ destinationPreferences[destination.id] = newPreferences
413
+ }
414
+ preferences = destinationPreferences
415
+ } else if (newPreferences) {
416
+ preferences = {
417
+ ...existingPreferences,
418
+ ...newPreferences
419
+ }
420
+ } else {
421
+ preferences = existingPreferences!
422
+ }
423
+
424
+ return preferences
425
+ }
426
+ }
@@ -0,0 +1,100 @@
1
+ // TODO: remove duplicate cookie library from bundle
2
+ import cookies, { CookieAttributes } from 'js-cookie'
3
+ import topDomain from '@segment/top-domain'
4
+ import { WindowWithHtEvents, Preferences, CategoryPreferences } from '../types'
5
+ import { EventEmitter } from 'events'
6
+
7
+ export const DEFAULT_COOKIE_NAME = 'ht-cm-preferences'
8
+ const COOKIE_DEFAULT_EXPIRES = 365
9
+
10
+ type PreferencesCookie = {
11
+ version: number
12
+ destination: CategoryPreferences
13
+ custom: CategoryPreferences
14
+ }
15
+
16
+ export interface PreferencesManager {
17
+ loadPreferences(cookieName?: string): Preferences
18
+ onPreferencesSaved(listener: (prefs: Preferences) => void): void
19
+ savePreferences(prefs: SavePreferences): void
20
+ }
21
+
22
+ // TODO: harden against invalid cookies
23
+ // TODO: harden against different versions of cookies
24
+ export function loadPreferences(cookieName?: string): Preferences {
25
+ const preferences = cookies.getJSON(cookieName || DEFAULT_COOKIE_NAME) as PreferencesCookie
26
+
27
+ if (!preferences) {
28
+ return {}
29
+ }
30
+
31
+ return {
32
+ destinationPreferences: preferences.destination,
33
+ customPreferences: preferences.custom
34
+ }
35
+ }
36
+
37
+ type SavePreferences = Preferences & {
38
+ cookieDomain?: string
39
+ cookieName?: string
40
+ cookieExpires?: number
41
+ cookieAttributes?: CookieAttributes
42
+ }
43
+
44
+ const emitter = new EventEmitter()
45
+
46
+ /**
47
+ * Subscribes to consent preferences changing over time and returns
48
+ * a cleanup function that can be invoked to remove the instantiated listener.
49
+ *
50
+ * @param listener a function to be invoked when ConsentPreferences are saved
51
+ */
52
+ export function onPreferencesSaved(listener: (prefs: Preferences) => void) {
53
+ emitter.on('preferencesSaved', listener)
54
+ return () => emitter.off('preferencesSaved', listener)
55
+ }
56
+
57
+ export function savePreferences({
58
+ destinationPreferences,
59
+ customPreferences,
60
+ cookieDomain,
61
+ cookieName,
62
+ cookieExpires,
63
+ cookieAttributes = {}
64
+ }: SavePreferences) {
65
+ const wd = window as WindowWithHtEvents
66
+ 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
+ })
78
+ } else {
79
+ console.warn('window.htevents not found...is the SDK snippet included on the page?')
80
+ }
81
+
82
+ const domain = cookieDomain || topDomain(window.location.href)
83
+ const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES
84
+ const value = {
85
+ version: 1,
86
+ destination: destinationPreferences,
87
+ custom: customPreferences
88
+ } as PreferencesCookie
89
+
90
+ cookies.set(cookieName || DEFAULT_COOKIE_NAME, value, {
91
+ expires,
92
+ domain,
93
+ ...cookieAttributes
94
+ })
95
+
96
+ emitter.emit('preferencesSaved', {
97
+ destinationPreferences,
98
+ customPreferences
99
+ })
100
+ }
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ import CMB from './consent-manager-builder'
2
+ import CM from './consent-manager'
3
+
4
+ export { openDialog as openConsentManager } from './consent-manager/container'
5
+ export {
6
+ loadPreferences,
7
+ savePreferences,
8
+ onPreferencesSaved
9
+ } from './consent-manager-builder/preferences'
10
+
11
+ export const ConsentManagerBuilder = CMB
12
+ export const ConsentManager = CM
13
+
14
+ type Nav = Navigator & {
15
+ msDoNotTrack?: Navigator['doNotTrack']
16
+ }
17
+
18
+ export function doNotTrack(): boolean | null {
19
+
20
+ if (typeof window !== 'undefined' && (window.navigator || navigator)) {
21
+ const nav = navigator as Nav
22
+
23
+ let doNotTrackValue = nav.doNotTrack || window.doNotTrack || nav.msDoNotTrack
24
+
25
+ // Normalise Firefox < 32
26
+ // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack
27
+ if (doNotTrackValue === 'yes') {
28
+ doNotTrackValue = '1'
29
+ } else if (doNotTrackValue === 'no') {
30
+ doNotTrackValue = '0'
31
+ }
32
+
33
+ if (doNotTrackValue === '1') {
34
+ return true
35
+ }
36
+ if (doNotTrackValue === '0') {
37
+ return false
38
+ }
39
+ }
40
+
41
+ return null
42
+ }
@@ -0,0 +1,57 @@
1
+ import React from 'react'
2
+ import ReactDOM from 'react-dom'
3
+ import inEU from '@segment/in-eu'
4
+ import inRegions from '@segment/in-regions'
5
+ import { ConsentManager, openConsentManager, doNotTrack } from '.'
6
+ import { ConsentManagerProps, WindowWithConsentManagerConfig, ConsentManagerInput } from './types'
7
+ import * as preferences from './consent-manager-builder/preferences'
8
+
9
+ export const version = process.env.VERSION
10
+ export { openConsentManager, doNotTrack, inEU, preferences }
11
+
12
+ let props: Partial<ConsentManagerInput> = {}
13
+ let containerRef: string | undefined
14
+
15
+ const localWindow = window as WindowWithConsentManagerConfig
16
+
17
+ if (localWindow.consentManagerConfig && typeof localWindow.consentManagerConfig === 'function') {
18
+ props = localWindow.consentManagerConfig({
19
+ React,
20
+ version,
21
+ openConsentManager,
22
+ doNotTrack,
23
+ inEU,
24
+ preferences,
25
+ inRegions
26
+ })
27
+ containerRef = props.container
28
+ } else {
29
+ throw new Error(`window.consentManagerConfig should be a function`)
30
+ }
31
+
32
+ if (!containerRef) {
33
+ throw new Error('ConsentManager: container is required')
34
+ }
35
+
36
+ if (!props.writeKey) {
37
+ throw new Error('ConsentManager: writeKey is required')
38
+ }
39
+
40
+ if (typeof props.implyConsentOnInteraction === 'string') {
41
+ props.implyConsentOnInteraction = props.implyConsentOnInteraction === 'true'
42
+ }
43
+
44
+ if (props.closeBehavior !== undefined && typeof props.closeBehavior === 'string') {
45
+ const options = ['accept', 'deny', 'dismiss']
46
+
47
+ if (!options.includes(props.closeBehavior)) {
48
+ throw new Error(`ConsentManager: closeBehavior should be one of ${options}`)
49
+ }
50
+ }
51
+
52
+ const container = document.querySelector(containerRef)
53
+ if (!container) {
54
+ throw new Error('ConsentManager: container not found')
55
+ }
56
+
57
+ ReactDOM.render(<ConsentManager {...(props as ConsentManagerProps)} />, container)
@@ -0,0 +1,7 @@
1
+ import { CSSProp } from 'styled-components'
2
+
3
+ declare module 'react' {
4
+ interface HTMLAttributes<T> extends DOMAttributes<T> {
5
+ css?: CSSProp
6
+ }
7
+ }