@commercetools-frontend/cookie-consent 1.0.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.
- package/README.md +103 -0
- package/core/dist/commercetools-frontend-cookie-consent-core.cjs.d.ts +1 -0
- package/core/dist/commercetools-frontend-cookie-consent-core.cjs.dev.js +138 -0
- package/core/dist/commercetools-frontend-cookie-consent-core.cjs.js +7 -0
- package/core/dist/commercetools-frontend-cookie-consent-core.cjs.prod.js +138 -0
- package/core/package.json +3 -0
- package/dist/declarations/src/core.d.ts +18 -0
- package/dist/declarations/src/react.d.ts +6 -0
- package/package.json +38 -0
- package/react/dist/commercetools-frontend-cookie-consent-react.cjs.d.ts +1 -0
- package/react/dist/commercetools-frontend-cookie-consent-react.cjs.dev.js +38 -0
- package/react/dist/commercetools-frontend-cookie-consent-react.cjs.js +7 -0
- package/react/dist/commercetools-frontend-cookie-consent-react.cjs.prod.js +38 -0
- package/react/package.json +3 -0
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @commercetools-frontend/cookie-consent
|
|
2
|
+
|
|
3
|
+
> This is a package used internally for Merchant Center applications. We do not provide any guarantees or support for the functionality.
|
|
4
|
+
|
|
5
|
+
This package provides an easy to use integration with OneTrust cookie consent.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
$ npm install --save @commercetools-frontend/cookie-consent
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Background
|
|
14
|
+
|
|
15
|
+
Cookie consent is given from a user towards a company as the legal entity for all of its products not for every single product. Consent via OneTrust is can be given on a marketing website for instance commercetools.com. The OneTrust cookie banner supports opting out of certain categories of cookies or revoking consent entirely.
|
|
16
|
+
|
|
17
|
+
The resulting cookie stored in a user's browser is called `OptanonConsent`. The contents of those cookie can be of the following:
|
|
18
|
+
|
|
19
|
+
```txt
|
|
20
|
+
isGpcEnabled=0&datestamp=Wed+Feb+01+2023+12%3A35%3A32+GMT%2B0100+(Central+European+Standard+Time)&version=202209.1.0&isIABGlobal=false&hosts=&consentId=7f3cf16d-b3e1-4781-8db1-61482d0a9dff&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A0%2CC0003%3A1%2CC0004%3A1%2CC0005%3A1&geolocation=AT%3B9&AwaitingReconsent=false
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The cookie will be removed after 365 days yielding repeated consent to be requested by the user on our marketing website. The cookie itself is URL encoded and can be parsed using `URLSearchParams`. The field of interest is `groups`. These are the groups for which consent was granted or not:
|
|
24
|
+
|
|
25
|
+
1. EssentialCookies: 'C0001'
|
|
26
|
+
2. PerformanceCookies: 'C0002'
|
|
27
|
+
3. FunctionalCookies: 'C0003'
|
|
28
|
+
4. TargetingCookies: 'C0004'
|
|
29
|
+
5. SocialMediaCookies: 'C0005'
|
|
30
|
+
|
|
31
|
+
This package parses the cookie if present and returns consent for groups given or revoked.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Retrieving consent values
|
|
36
|
+
|
|
37
|
+
This package does not make assumptions what framework you use. To integrate without any assumptions about the framework use the `/core` entry point:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { getParsedConsentCookieGroups } from '@commercetools-frontend/cookie-consent/core';
|
|
41
|
+
|
|
42
|
+
const consentGroups = getParsedConsentCookieGroups();
|
|
43
|
+
// { essentialCookies: true, performanceCookies: false }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
If there is ever a need to use the raw consent cookie's value itself you can retrieve it too:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { getRawConsentCookie } from '@commercetools-frontend/cookie-consent/core';
|
|
50
|
+
|
|
51
|
+
const rawConsentCookie = getRawConsentCookie();
|
|
52
|
+
// The value of the `OptanonConsent` cookie
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Knowing the consent groups you can use a constant to easily map them onto something easier to understand:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { getParsedConsentCookieGroups } from '@commercetools-frontend/cookie-consent/core';
|
|
59
|
+
|
|
60
|
+
const consentGroups = getParsedConsentCookieGroups();
|
|
61
|
+
const hasGivenPerformanceCookieConsent = Boolean(
|
|
62
|
+
consentGroups.performanceCookies
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
You can also use the `useCookieConsent` from the `/react` entry point of the package.
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import { useCookieConsent } from '@commercetools-frontend/cookie-consent/react';
|
|
70
|
+
|
|
71
|
+
const { givenConsent } = useCookieConsent('performanceCookies');
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The resulting `givenConsent` value is a boolean which can be passed to any software needing consent for instance FullStory or Intercom. A combination of `@commercetools-frontend/cookie-consent` and `@commercetools-frontend/fullstory` could look like this:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
const { givenConsent } = useCookieConsent('performanceCookies');
|
|
78
|
+
|
|
79
|
+
useFullStoryTrackingEffect({ disable: !givenConsent });
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Setting consent values
|
|
83
|
+
|
|
84
|
+
To integrate without any assumptions about the framework again, use the `/core` entry point:
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { setConsentCookie } from '@commercetools-frontend/cookie-consent/core';
|
|
88
|
+
|
|
89
|
+
setConsentCookie({ performanceCookieConsent: true });
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Also here you can use the `useCookieConsent` from the `/react` entry point of the package.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
import { useCookieConsent } from '@commercetools-frontend/cookie-consent/react';
|
|
96
|
+
|
|
97
|
+
const { setConsent } = useCookieConsent('performanceCookies');
|
|
98
|
+
|
|
99
|
+
// For instance in an `onClick` hander you can
|
|
100
|
+
<button onClick={() => setConsent({ performanceCookieConsent: true })}>
|
|
101
|
+
Update cookie consent
|
|
102
|
+
</button>;
|
|
103
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/core";
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
6
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
7
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
8
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
9
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
10
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
11
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
12
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
13
|
+
var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
14
|
+
var _findInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/find');
|
|
15
|
+
var _Object$fromEntries = require('@babel/runtime-corejs3/core-js-stable/object/from-entries');
|
|
16
|
+
var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
|
|
17
|
+
var _Object$values = require('@babel/runtime-corejs3/core-js-stable/object/values');
|
|
18
|
+
var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries');
|
|
19
|
+
var _URLSearchParams = require('@babel/runtime-corejs3/core-js-stable/url-search-params');
|
|
20
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
21
|
+
var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
|
|
22
|
+
|
|
23
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
24
|
+
|
|
25
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
26
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
27
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
28
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
29
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
30
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
31
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
32
|
+
var _findInstanceProperty__default = /*#__PURE__*/_interopDefault(_findInstanceProperty);
|
|
33
|
+
var _Object$fromEntries__default = /*#__PURE__*/_interopDefault(_Object$fromEntries);
|
|
34
|
+
var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
|
|
35
|
+
var _Object$values__default = /*#__PURE__*/_interopDefault(_Object$values);
|
|
36
|
+
var _Object$entries__default = /*#__PURE__*/_interopDefault(_Object$entries);
|
|
37
|
+
var _URLSearchParams__default = /*#__PURE__*/_interopDefault(_URLSearchParams);
|
|
38
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
39
|
+
var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInstanceProperty);
|
|
40
|
+
|
|
41
|
+
var _context2;
|
|
42
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
43
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context10, _context11; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context10 = ownKeys(Object(source), !0)).call(_context10, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context11 = ownKeys(Object(source))).call(_context11, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
|
|
44
|
+
var COOKIE_CONSENT_GROUPS = {
|
|
45
|
+
essentialCookies: 'C0001',
|
|
46
|
+
performanceCookies: 'C0002',
|
|
47
|
+
functionalCookies: 'C0003',
|
|
48
|
+
targetingCookies: 'C0004',
|
|
49
|
+
socialMediaCookies: 'C0005'
|
|
50
|
+
};
|
|
51
|
+
function getRawConsentCookie() {
|
|
52
|
+
var _context;
|
|
53
|
+
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'OptanonConsent';
|
|
54
|
+
var escapedCookies = decodeURIComponent(document.cookie);
|
|
55
|
+
var consentCookie = _findInstanceProperty__default["default"](_context = escapedCookies.split('; ')).call(_context, function (rawCookie) {
|
|
56
|
+
var _rawCookie$split = rawCookie.split('='),
|
|
57
|
+
_rawCookie$split2 = _slicedToArray(_rawCookie$split, 1),
|
|
58
|
+
cookieName = _rawCookie$split2[0];
|
|
59
|
+
return cookieName === name;
|
|
60
|
+
});
|
|
61
|
+
return consentCookie;
|
|
62
|
+
}
|
|
63
|
+
var defaultConsentGroups = _Object$fromEntries__default["default"](_mapInstanceProperty__default["default"](_context2 = _Object$values__default["default"](COOKIE_CONSENT_GROUPS)).call(_context2, function (consentGroup) {
|
|
64
|
+
return [consentGroup, false];
|
|
65
|
+
}));
|
|
66
|
+
function getParsedConsentCookieGroups() {
|
|
67
|
+
var _context3;
|
|
68
|
+
var cookieValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getRawConsentCookie();
|
|
69
|
+
var parsedConsentCookieGroups = {};
|
|
70
|
+
var encodedConsentGroupToConsentGroup = _Object$fromEntries__default["default"](_mapInstanceProperty__default["default"](_context3 = _Object$entries__default["default"](COOKIE_CONSENT_GROUPS)).call(_context3, function (_ref) {
|
|
71
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
72
|
+
encodedConsentGroupName = _ref2[0],
|
|
73
|
+
decodedConsentGroupName = _ref2[1];
|
|
74
|
+
return [decodedConsentGroupName, encodedConsentGroupName];
|
|
75
|
+
}));
|
|
76
|
+
try {
|
|
77
|
+
var _context4;
|
|
78
|
+
var parsedConsentCookieValue = new _URLSearchParams__default["default"](cookieValue);
|
|
79
|
+
var rawConsentCookieGroups = parsedConsentCookieValue.get('groups');
|
|
80
|
+
var parsedConsentCookieGroupEntries = rawConsentCookieGroups === null || rawConsentCookieGroups === void 0 ? void 0 : _mapInstanceProperty__default["default"](_context4 = rawConsentCookieGroups.split(',')).call(_context4, function (consentGroup) {
|
|
81
|
+
var _consentGroup$split = consentGroup.split(':'),
|
|
82
|
+
_consentGroup$split2 = _slicedToArray(_consentGroup$split, 2),
|
|
83
|
+
encodedConsentGroupName = _consentGroup$split2[0],
|
|
84
|
+
consentGroupValue = _consentGroup$split2[1];
|
|
85
|
+
return [encodedConsentGroupToConsentGroup[encodedConsentGroupName], consentGroupValue === '1'];
|
|
86
|
+
});
|
|
87
|
+
parsedConsentCookieGroups = parsedConsentCookieGroupEntries ? _Object$fromEntries__default["default"](parsedConsentCookieGroupEntries) : null;
|
|
88
|
+
} catch (error) {}
|
|
89
|
+
return _objectSpread(_objectSpread({}, defaultConsentGroups), parsedConsentCookieGroups);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Copied from GitHub to make this library not have a UUID library dependency.
|
|
94
|
+
* This implementation is good enough for the purposes here.
|
|
95
|
+
*/
|
|
96
|
+
function generateUuid(prefix) {
|
|
97
|
+
return prefix ? ((Number(prefix) ^ Math.random() * 16) >> Number(prefix) / 4).toString(16) : "".concat(1e7, "-", 1e3, "-", 4e3, "-", 8e3, "-", 1e11).replace(/[018]/g, generateUuid);
|
|
98
|
+
}
|
|
99
|
+
function generateConsentCookie(consentGroups) {
|
|
100
|
+
var _context5, _context6;
|
|
101
|
+
var encodedConsentGroups = _filterInstanceProperty__default["default"](_context5 = _mapInstanceProperty__default["default"](_context6 = _Object$entries__default["default"](consentGroups)).call(_context6, function (_ref3) {
|
|
102
|
+
var _context7;
|
|
103
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
104
|
+
consentGroupName = _ref4[0],
|
|
105
|
+
consentGroupValue = _ref4[1];
|
|
106
|
+
var encodedConsentGroup = COOKIE_CONSENT_GROUPS[consentGroupName];
|
|
107
|
+
if (!encodedConsentGroup) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return _concatInstanceProperty__default["default"](_context7 = "".concat(encodedConsentGroup, ":")).call(_context7, consentGroupValue && '1' || '0');
|
|
111
|
+
})).call(_context5, Boolean).join(',');
|
|
112
|
+
var consentValues = {
|
|
113
|
+
isGpcEnabled: '0',
|
|
114
|
+
datestamp: new Date().toString(),
|
|
115
|
+
version: '202209.1.0',
|
|
116
|
+
isIABGlobal: 'false',
|
|
117
|
+
hosts: '',
|
|
118
|
+
consentId: generateUuid(),
|
|
119
|
+
interactionCount: '1',
|
|
120
|
+
landingPath: 'NotLandingPage',
|
|
121
|
+
groups: encodedConsentGroups,
|
|
122
|
+
AwaitingReconsent: 'false'
|
|
123
|
+
};
|
|
124
|
+
var consentCookieValue = new _URLSearchParams__default["default"](consentValues);
|
|
125
|
+
return consentCookieValue.toString();
|
|
126
|
+
}
|
|
127
|
+
function setConsentCookie(consentGroups) {
|
|
128
|
+
var _context8, _context9;
|
|
129
|
+
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.commercetools.com';
|
|
130
|
+
var expiresAt = new Date();
|
|
131
|
+
expiresAt.setFullYear(expiresAt.getFullYear() + 11);
|
|
132
|
+
document.cookie = _concatInstanceProperty__default["default"](_context8 = _concatInstanceProperty__default["default"](_context9 = "OptanonConsent=".concat(generateConsentCookie(consentGroups), "; domain=")).call(_context9, domain, "; expires=")).call(_context8, expiresAt.toUTCString(), "; SameSite=Lax; path=/; ");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
exports.COOKIE_CONSENT_GROUPS = COOKIE_CONSENT_GROUPS;
|
|
136
|
+
exports.getParsedConsentCookieGroups = getParsedConsentCookieGroups;
|
|
137
|
+
exports.getRawConsentCookie = getRawConsentCookie;
|
|
138
|
+
exports.setConsentCookie = setConsentCookie;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
6
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
7
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
8
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
9
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
10
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
11
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
12
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
13
|
+
var _slicedToArray = require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
14
|
+
var _findInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/find');
|
|
15
|
+
var _Object$fromEntries = require('@babel/runtime-corejs3/core-js-stable/object/from-entries');
|
|
16
|
+
var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
|
|
17
|
+
var _Object$values = require('@babel/runtime-corejs3/core-js-stable/object/values');
|
|
18
|
+
var _Object$entries = require('@babel/runtime-corejs3/core-js-stable/object/entries');
|
|
19
|
+
var _URLSearchParams = require('@babel/runtime-corejs3/core-js-stable/url-search-params');
|
|
20
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
21
|
+
var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
|
|
22
|
+
|
|
23
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
24
|
+
|
|
25
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
26
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
27
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
28
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
29
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
30
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
31
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
32
|
+
var _findInstanceProperty__default = /*#__PURE__*/_interopDefault(_findInstanceProperty);
|
|
33
|
+
var _Object$fromEntries__default = /*#__PURE__*/_interopDefault(_Object$fromEntries);
|
|
34
|
+
var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
|
|
35
|
+
var _Object$values__default = /*#__PURE__*/_interopDefault(_Object$values);
|
|
36
|
+
var _Object$entries__default = /*#__PURE__*/_interopDefault(_Object$entries);
|
|
37
|
+
var _URLSearchParams__default = /*#__PURE__*/_interopDefault(_URLSearchParams);
|
|
38
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
39
|
+
var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInstanceProperty);
|
|
40
|
+
|
|
41
|
+
var _context2;
|
|
42
|
+
function ownKeys(object, enumerableOnly) { var keys = _Object$keys__default["default"](object); if (_Object$getOwnPropertySymbols__default["default"]) { var symbols = _Object$getOwnPropertySymbols__default["default"](object); enumerableOnly && (symbols = _filterInstanceProperty__default["default"](symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor__default["default"](object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
43
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context10, _context11; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty__default["default"](_context10 = ownKeys(Object(source), !0)).call(_context10, function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](target, _Object$getOwnPropertyDescriptors__default["default"](source)) : _forEachInstanceProperty__default["default"](_context11 = ownKeys(Object(source))).call(_context11, function (key) { _Object$defineProperty__default["default"](target, key, _Object$getOwnPropertyDescriptor__default["default"](source, key)); }); } return target; }
|
|
44
|
+
var COOKIE_CONSENT_GROUPS = {
|
|
45
|
+
essentialCookies: 'C0001',
|
|
46
|
+
performanceCookies: 'C0002',
|
|
47
|
+
functionalCookies: 'C0003',
|
|
48
|
+
targetingCookies: 'C0004',
|
|
49
|
+
socialMediaCookies: 'C0005'
|
|
50
|
+
};
|
|
51
|
+
function getRawConsentCookie() {
|
|
52
|
+
var _context;
|
|
53
|
+
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'OptanonConsent';
|
|
54
|
+
var escapedCookies = decodeURIComponent(document.cookie);
|
|
55
|
+
var consentCookie = _findInstanceProperty__default["default"](_context = escapedCookies.split('; ')).call(_context, function (rawCookie) {
|
|
56
|
+
var _rawCookie$split = rawCookie.split('='),
|
|
57
|
+
_rawCookie$split2 = _slicedToArray(_rawCookie$split, 1),
|
|
58
|
+
cookieName = _rawCookie$split2[0];
|
|
59
|
+
return cookieName === name;
|
|
60
|
+
});
|
|
61
|
+
return consentCookie;
|
|
62
|
+
}
|
|
63
|
+
var defaultConsentGroups = _Object$fromEntries__default["default"](_mapInstanceProperty__default["default"](_context2 = _Object$values__default["default"](COOKIE_CONSENT_GROUPS)).call(_context2, function (consentGroup) {
|
|
64
|
+
return [consentGroup, false];
|
|
65
|
+
}));
|
|
66
|
+
function getParsedConsentCookieGroups() {
|
|
67
|
+
var _context3;
|
|
68
|
+
var cookieValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getRawConsentCookie();
|
|
69
|
+
var parsedConsentCookieGroups = {};
|
|
70
|
+
var encodedConsentGroupToConsentGroup = _Object$fromEntries__default["default"](_mapInstanceProperty__default["default"](_context3 = _Object$entries__default["default"](COOKIE_CONSENT_GROUPS)).call(_context3, function (_ref) {
|
|
71
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
72
|
+
encodedConsentGroupName = _ref2[0],
|
|
73
|
+
decodedConsentGroupName = _ref2[1];
|
|
74
|
+
return [decodedConsentGroupName, encodedConsentGroupName];
|
|
75
|
+
}));
|
|
76
|
+
try {
|
|
77
|
+
var _context4;
|
|
78
|
+
var parsedConsentCookieValue = new _URLSearchParams__default["default"](cookieValue);
|
|
79
|
+
var rawConsentCookieGroups = parsedConsentCookieValue.get('groups');
|
|
80
|
+
var parsedConsentCookieGroupEntries = rawConsentCookieGroups === null || rawConsentCookieGroups === void 0 ? void 0 : _mapInstanceProperty__default["default"](_context4 = rawConsentCookieGroups.split(',')).call(_context4, function (consentGroup) {
|
|
81
|
+
var _consentGroup$split = consentGroup.split(':'),
|
|
82
|
+
_consentGroup$split2 = _slicedToArray(_consentGroup$split, 2),
|
|
83
|
+
encodedConsentGroupName = _consentGroup$split2[0],
|
|
84
|
+
consentGroupValue = _consentGroup$split2[1];
|
|
85
|
+
return [encodedConsentGroupToConsentGroup[encodedConsentGroupName], consentGroupValue === '1'];
|
|
86
|
+
});
|
|
87
|
+
parsedConsentCookieGroups = parsedConsentCookieGroupEntries ? _Object$fromEntries__default["default"](parsedConsentCookieGroupEntries) : null;
|
|
88
|
+
} catch (error) {}
|
|
89
|
+
return _objectSpread(_objectSpread({}, defaultConsentGroups), parsedConsentCookieGroups);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Copied from GitHub to make this library not have a UUID library dependency.
|
|
94
|
+
* This implementation is good enough for the purposes here.
|
|
95
|
+
*/
|
|
96
|
+
function generateUuid(prefix) {
|
|
97
|
+
return prefix ? ((Number(prefix) ^ Math.random() * 16) >> Number(prefix) / 4).toString(16) : "".concat(1e7, "-", 1e3, "-", 4e3, "-", 8e3, "-", 1e11).replace(/[018]/g, generateUuid);
|
|
98
|
+
}
|
|
99
|
+
function generateConsentCookie(consentGroups) {
|
|
100
|
+
var _context5, _context6;
|
|
101
|
+
var encodedConsentGroups = _filterInstanceProperty__default["default"](_context5 = _mapInstanceProperty__default["default"](_context6 = _Object$entries__default["default"](consentGroups)).call(_context6, function (_ref3) {
|
|
102
|
+
var _context7;
|
|
103
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
104
|
+
consentGroupName = _ref4[0],
|
|
105
|
+
consentGroupValue = _ref4[1];
|
|
106
|
+
var encodedConsentGroup = COOKIE_CONSENT_GROUPS[consentGroupName];
|
|
107
|
+
if (!encodedConsentGroup) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return _concatInstanceProperty__default["default"](_context7 = "".concat(encodedConsentGroup, ":")).call(_context7, consentGroupValue && '1' || '0');
|
|
111
|
+
})).call(_context5, Boolean).join(',');
|
|
112
|
+
var consentValues = {
|
|
113
|
+
isGpcEnabled: '0',
|
|
114
|
+
datestamp: new Date().toString(),
|
|
115
|
+
version: '202209.1.0',
|
|
116
|
+
isIABGlobal: 'false',
|
|
117
|
+
hosts: '',
|
|
118
|
+
consentId: generateUuid(),
|
|
119
|
+
interactionCount: '1',
|
|
120
|
+
landingPath: 'NotLandingPage',
|
|
121
|
+
groups: encodedConsentGroups,
|
|
122
|
+
AwaitingReconsent: 'false'
|
|
123
|
+
};
|
|
124
|
+
var consentCookieValue = new _URLSearchParams__default["default"](consentValues);
|
|
125
|
+
return consentCookieValue.toString();
|
|
126
|
+
}
|
|
127
|
+
function setConsentCookie(consentGroups) {
|
|
128
|
+
var _context8, _context9;
|
|
129
|
+
var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.commercetools.com';
|
|
130
|
+
var expiresAt = new Date();
|
|
131
|
+
expiresAt.setFullYear(expiresAt.getFullYear() + 11);
|
|
132
|
+
document.cookie = _concatInstanceProperty__default["default"](_context8 = _concatInstanceProperty__default["default"](_context9 = "OptanonConsent=".concat(generateConsentCookie(consentGroups), "; domain=")).call(_context9, domain, "; expires=")).call(_context8, expiresAt.toUTCString(), "; SameSite=Lax; path=/; ");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
exports.COOKIE_CONSENT_GROUPS = COOKIE_CONSENT_GROUPS;
|
|
136
|
+
exports.getParsedConsentCookieGroups = getParsedConsentCookieGroups;
|
|
137
|
+
exports.getRawConsentCookie = getRawConsentCookie;
|
|
138
|
+
exports.setConsentCookie = setConsentCookie;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare const COOKIE_CONSENT_GROUPS: {
|
|
2
|
+
readonly essentialCookies: "C0001";
|
|
3
|
+
readonly performanceCookies: "C0002";
|
|
4
|
+
readonly functionalCookies: "C0003";
|
|
5
|
+
readonly targetingCookies: "C0004";
|
|
6
|
+
readonly socialMediaCookies: "C0005";
|
|
7
|
+
};
|
|
8
|
+
type TConsentGroupNames = keyof typeof COOKIE_CONSENT_GROUPS;
|
|
9
|
+
declare function getRawConsentCookie(name?: string): string | undefined;
|
|
10
|
+
declare function getParsedConsentCookieGroups(cookieValue?: string | undefined): {
|
|
11
|
+
essentialCookies: boolean;
|
|
12
|
+
performanceCookies: boolean;
|
|
13
|
+
functionalCookies: boolean;
|
|
14
|
+
targetingCookies: boolean;
|
|
15
|
+
socialMediaCookies: boolean;
|
|
16
|
+
};
|
|
17
|
+
declare function setConsentCookie(consentGroups: Record<TConsentGroupNames, boolean>, domain?: string): void;
|
|
18
|
+
export { getRawConsentCookie, getParsedConsentCookieGroups, setConsentCookie, COOKIE_CONSENT_GROUPS, };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { setConsentCookie, COOKIE_CONSENT_GROUPS } from './core';
|
|
2
|
+
declare function useCookieConsent(consentGroup: keyof typeof COOKIE_CONSENT_GROUPS, cookieName?: string): {
|
|
3
|
+
givenConsent: boolean;
|
|
4
|
+
setConsent: typeof setConsentCookie;
|
|
5
|
+
};
|
|
6
|
+
export { useCookieConsent };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commercetools-frontend/cookie-consent",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/commercetools-frontend-cookie-consent.cjs.js",
|
|
9
|
+
"module": "dist/commercetools-frontend-cookie-consent.esm.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"typecheck": "tsc --noEmit"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"core",
|
|
16
|
+
"react",
|
|
17
|
+
"package.json",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"preconstruct": {
|
|
21
|
+
"entrypoints": [
|
|
22
|
+
"./core.ts",
|
|
23
|
+
"./react.ts"
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@babel/runtime": "^7.20.7",
|
|
28
|
+
"@babel/runtime-corejs3": "^7.20.7",
|
|
29
|
+
"@fullstory/browser": "1.6.2"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "18.11.18",
|
|
33
|
+
"typescript": "4.9.4"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": "17.x"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../../dist/declarations/src/react";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var core_dist_commercetoolsFrontendCookieConsentCore = require('../../core/dist/commercetools-frontend-cookie-consent-core.cjs.dev.js');
|
|
7
|
+
require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
8
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
9
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
10
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
11
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
12
|
+
require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
13
|
+
require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
14
|
+
require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
15
|
+
require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
16
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/find');
|
|
17
|
+
require('@babel/runtime-corejs3/core-js-stable/object/from-entries');
|
|
18
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/map');
|
|
19
|
+
require('@babel/runtime-corejs3/core-js-stable/object/values');
|
|
20
|
+
require('@babel/runtime-corejs3/core-js-stable/object/entries');
|
|
21
|
+
require('@babel/runtime-corejs3/core-js-stable/url-search-params');
|
|
22
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
23
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/concat');
|
|
24
|
+
|
|
25
|
+
function useCookieConsent(consentGroup) {
|
|
26
|
+
var cookieName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'OptanonConsent';
|
|
27
|
+
var consentCookieGroups = react.useMemo(function () {
|
|
28
|
+
var rawConsentCookie = core_dist_commercetoolsFrontendCookieConsentCore.getRawConsentCookie(cookieName);
|
|
29
|
+
var consentCookieGroups = core_dist_commercetoolsFrontendCookieConsentCore.getParsedConsentCookieGroups(rawConsentCookie);
|
|
30
|
+
return consentCookieGroups;
|
|
31
|
+
}, [cookieName]);
|
|
32
|
+
return {
|
|
33
|
+
givenConsent: Boolean(consentCookieGroups[consentGroup]),
|
|
34
|
+
setConsent: core_dist_commercetoolsFrontendCookieConsentCore.setConsentCookie
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
exports.useCookieConsent = useCookieConsent;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var core_dist_commercetoolsFrontendCookieConsentCore = require('../../core/dist/commercetools-frontend-cookie-consent-core.cjs.prod.js');
|
|
7
|
+
require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
8
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
9
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
10
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
11
|
+
require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
12
|
+
require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
13
|
+
require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
14
|
+
require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
15
|
+
require('@babel/runtime-corejs3/helpers/slicedToArray');
|
|
16
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/find');
|
|
17
|
+
require('@babel/runtime-corejs3/core-js-stable/object/from-entries');
|
|
18
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/map');
|
|
19
|
+
require('@babel/runtime-corejs3/core-js-stable/object/values');
|
|
20
|
+
require('@babel/runtime-corejs3/core-js-stable/object/entries');
|
|
21
|
+
require('@babel/runtime-corejs3/core-js-stable/url-search-params');
|
|
22
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
23
|
+
require('@babel/runtime-corejs3/core-js-stable/instance/concat');
|
|
24
|
+
|
|
25
|
+
function useCookieConsent(consentGroup) {
|
|
26
|
+
var cookieName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'OptanonConsent';
|
|
27
|
+
var consentCookieGroups = react.useMemo(function () {
|
|
28
|
+
var rawConsentCookie = core_dist_commercetoolsFrontendCookieConsentCore.getRawConsentCookie(cookieName);
|
|
29
|
+
var consentCookieGroups = core_dist_commercetoolsFrontendCookieConsentCore.getParsedConsentCookieGroups(rawConsentCookie);
|
|
30
|
+
return consentCookieGroups;
|
|
31
|
+
}, [cookieName]);
|
|
32
|
+
return {
|
|
33
|
+
givenConsent: Boolean(consentCookieGroups[consentGroup]),
|
|
34
|
+
setConsent: core_dist_commercetoolsFrontendCookieConsentCore.setConsentCookie
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
exports.useCookieConsent = useCookieConsent;
|