|
1 |
| -/** |
2 |
| - * GoogleConsentMode handles with Google Consent Mode v2. |
3 |
| - */ |
4 |
| -import isEqual from 'lodash/isEqual'; |
| 1 | +import { GCM_SHARED_COOKIE_NAME, setCookie } from './cookieUtils'; |
| 2 | +import { utils } from '@farfetch/blackout-core/analytics'; |
5 | 3 | import omit from 'lodash/omit';
|
6 | 4 |
|
7 | 5 | export const googleConsentTypes = {
|
8 | 6 | GRANTED: 'granted',
|
9 | 7 | DENIED: 'denied',
|
10 | 8 | };
|
| 9 | + |
| 10 | +/** |
| 11 | + * GoogleConsentMode handles with Google Consent Mode v2. |
| 12 | + */ |
11 | 13 | export class GoogleConsentMode {
|
| 14 | + dataLayer; // Stores different data layer names |
| 15 | + configWithConsentOnly; // exclude not consent properties from config |
| 16 | + consentDataLayerCommands = []; |
| 17 | + waitForUpdate; |
| 18 | + regions; |
| 19 | + hasConfig; |
| 20 | + |
12 | 21 | /**
|
13 | 22 | * Creates a new GoogleConsentMode instance.
|
14 | 23 | *
|
15 | 24 | * @param {string} dataLayer - DataLayer name.
|
16 |
| - * @param {string} initConsent - The init consent data. |
| 25 | + * @param {object} initConsent - The init consent data. |
17 | 26 | * @param {object} config - The configuration properties of Google Consent Mode.
|
18 | 27 | */
|
19 | 28 | constructor(dataLayer, initConsent, config) {
|
20 | 29 | this.dataLayer = dataLayer;
|
21 |
| - this.config = config; |
| 30 | + |
| 31 | + this.waitForUpdate = config?.waitForUpdate; |
| 32 | + this.regions = config?.regions; |
22 | 33 |
|
23 | 34 | // select only the Google Consent Elements
|
24 |
| - this.configExcludingModeRegionsAndWaitForUpdate = omit(this.config || {}, [ |
| 35 | + this.configWithConsentOnly = omit(config || {}, [ |
25 | 36 | 'waitForUpdate',
|
26 | 37 | 'regions',
|
27 | 38 | 'mode',
|
28 | 39 | ]);
|
29 | 40 |
|
30 |
| - this.loadDefaults(initConsent); |
| 41 | + this.hasConfig = Object.keys(this.configWithConsentOnly).length > 0; |
| 42 | + |
| 43 | + this.initialize(initConsent); |
31 | 44 | }
|
32 | 45 |
|
33 | 46 | /**
|
34 |
| - * Initialize Google Consent Mode instance. |
35 |
| - * |
36 |
| - * @param {string} initConsent - The init consent data. |
| 47 | + * Tries to load shared consent from cookies if available |
| 48 | + * and writes it to the dataLayer. |
| 49 | + * This method is only supposed to be called if no google |
| 50 | + * consent config was passed. |
37 | 51 | */
|
38 |
| - loadDefaults(initConsent) { |
39 |
| - if (this.config) { |
40 |
| - const initialValue = {}; |
| 52 | + loadSharedConsentFromCookies() { |
| 53 | + const consentModeCookieValue = utils.getCookie(GCM_SHARED_COOKIE_NAME); |
41 | 54 |
|
42 |
| - if (this.config.waitForUpdate) { |
43 |
| - initialValue['wait_for_update'] = this.config.waitForUpdate; |
44 |
| - } |
| 55 | + if (consentModeCookieValue) { |
| 56 | + try { |
| 57 | + const values = JSON.parse(consentModeCookieValue); |
| 58 | + |
| 59 | + if (Array.isArray(values)) { |
| 60 | + values.forEach(value => { |
| 61 | + const [consentCommand, command, consent] = value; |
45 | 62 |
|
46 |
| - // Obtain default google consent registry |
47 |
| - const consentRegistry = Object.keys( |
48 |
| - this.configExcludingModeRegionsAndWaitForUpdate, |
49 |
| - ).reduce((result, consentKey) => { |
50 |
| - return { |
51 |
| - ...result, |
52 |
| - [consentKey]: |
53 |
| - this.configExcludingModeRegionsAndWaitForUpdate[consentKey] |
54 |
| - ?.default || googleConsentTypes.DENIED, |
55 |
| - }; |
56 |
| - }, initialValue); |
57 |
| - |
58 |
| - // Write default consent to data layer |
59 |
| - this.write('consent', 'default', consentRegistry); |
60 |
| - |
61 |
| - // write regions to data layer if they exists |
62 |
| - const regions = this.config.regions; |
63 |
| - if (regions) { |
64 |
| - regions.forEach(region => { |
65 |
| - this.write('consent', 'default', region); |
66 |
| - }); |
| 63 | + this.write(consentCommand, command, consent); |
| 64 | + }); |
| 65 | + } |
| 66 | + } catch { |
| 67 | + // Do nothing... |
67 | 68 | }
|
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Loads default values from the configuration and |
| 74 | + * writes them in a cookie for sharing. |
| 75 | + * |
| 76 | + * @param {object} initConsent - The consent data available, which can be null if the user has not yet given consent. |
| 77 | + */ |
| 78 | + loadDefaultsFromConfig(initConsent) { |
| 79 | + const initialValue = {}; |
68 | 80 |
|
69 |
| - // after write default consents, then write first update with initial consent data |
70 |
| - this.updateConsent(initConsent); |
| 81 | + if (this.waitForUpdate) { |
| 82 | + initialValue['wait_for_update'] = this.waitForUpdate; |
71 | 83 | }
|
| 84 | + |
| 85 | + // Obtain default google consent registry |
| 86 | + const consentRegistry = Object.keys(this.configWithConsentOnly).reduce( |
| 87 | + (result, consentKey) => ({ |
| 88 | + ...result, |
| 89 | + [consentKey]: |
| 90 | + this.configWithConsentOnly[consentKey]?.default || |
| 91 | + googleConsentTypes.DENIED, |
| 92 | + }), |
| 93 | + initialValue, |
| 94 | + ); |
| 95 | + |
| 96 | + // Write default consent to data layer |
| 97 | + this.write('consent', 'default', consentRegistry); |
| 98 | + |
| 99 | + // write regions to data layer if they exist |
| 100 | + const regions = this.regions; |
| 101 | + |
| 102 | + if (regions) { |
| 103 | + regions.forEach(region => { |
| 104 | + this.write('consent', 'default', region); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + this.updateConsent(initConsent); |
| 109 | + |
| 110 | + this.saveConsent(); |
72 | 111 | }
|
73 | 112 |
|
74 | 113 | /**
|
75 |
| - * Update consent. |
| 114 | + * Try to set consent types with dataLayer. If a valid |
| 115 | + * config was passed, default values for the consent |
| 116 | + * types are used. Else, try to load the commands |
| 117 | + * set from the cookie if it is available. |
76 | 118 | *
|
77 |
| - * @param {object} consentData - The consent data to be set. |
| 119 | + * @param initConsent - The consent data available, which can be null if the user has not yet given consent. |
78 | 120 | */
|
79 |
| - updateConsent(consentData) { |
80 |
| - if (this.config) { |
81 |
| - // Dealing with null or undefined consent values |
82 |
| - const safeConsent = consentData || {}; |
| 121 | + initialize(initConsent) { |
| 122 | + if (this.hasConfig) { |
| 123 | + this.loadDefaultsFromConfig(initConsent); |
| 124 | + } else { |
| 125 | + this.loadSharedConsentFromCookies(); |
| 126 | + } |
| 127 | + } |
83 | 128 |
|
| 129 | + /** |
| 130 | + * Writes consent updates to the dataLayer |
| 131 | + * by applying the configuration (if any) to |
| 132 | + * the passed consent data. |
| 133 | + * |
| 134 | + * @param {object} consentData - Consent data obtained from the user or null if not available. |
| 135 | + */ |
| 136 | + updateConsent(consentData) { |
| 137 | + if (this.hasConfig && consentData) { |
84 | 138 | // Fill consent value into consent element, using analytics consent categories
|
85 |
| - const consentRegistry = Object.keys( |
86 |
| - this.configExcludingModeRegionsAndWaitForUpdate, |
87 |
| - ).reduce((result, consentKey) => { |
88 |
| - let consentValue = googleConsentTypes.DENIED; |
89 |
| - const consent = |
90 |
| - this.configExcludingModeRegionsAndWaitForUpdate[consentKey]; |
91 |
| - |
92 |
| - if (consent) { |
93 |
| - // has consent config key |
94 |
| - |
95 |
| - if (consent.getConsentValue) { |
96 |
| - // give priority to custom function |
97 |
| - consentValue = consent.getConsentValue(safeConsent); |
98 |
| - } else if ( |
99 |
| - consent?.categories !== undefined && |
100 |
| - consent.categories.every(consent => safeConsent[consent]) |
101 |
| - ) { |
102 |
| - // The second option to assign value is by categories list |
103 |
| - consentValue = googleConsentTypes.GRANTED; |
| 139 | + const consentRegistry = Object.keys(this.configWithConsentOnly).reduce( |
| 140 | + (result, consentKey) => { |
| 141 | + let consentValue = googleConsentTypes.DENIED; |
| 142 | + const consent = this.configWithConsentOnly[consentKey]; |
| 143 | + |
| 144 | + if (consent) { |
| 145 | + // has consent config key |
| 146 | + if (consent.getConsentValue) { |
| 147 | + // give priority to custom function |
| 148 | + consentValue = consent.getConsentValue(consentData); |
| 149 | + } else if ( |
| 150 | + consent?.categories !== undefined && |
| 151 | + consent.categories.every(consent => consentData[consent]) |
| 152 | + ) { |
| 153 | + // The second option to assign value is by categories list |
| 154 | + consentValue = googleConsentTypes.GRANTED; |
| 155 | + } |
104 | 156 | }
|
105 |
| - } |
106 | 157 |
|
107 |
| - return { |
108 |
| - ...result, |
109 |
| - [consentKey]: consentValue, |
110 |
| - }; |
111 |
| - }, {}); |
| 158 | + return { |
| 159 | + ...result, |
| 160 | + [consentKey]: consentValue, |
| 161 | + }; |
| 162 | + }, |
| 163 | + {}, |
| 164 | + ); |
112 | 165 |
|
113 | 166 | // Write consent to data layer
|
114 | 167 | this.write('consent', 'update', consentRegistry);
|
| 168 | + |
| 169 | + this.saveConsent(); |
115 | 170 | }
|
116 | 171 | }
|
117 | 172 |
|
118 | 173 | /**
|
119 |
| - * Write consent on data layer. |
| 174 | + * Saves calculated google consent mode to a cookie |
| 175 | + * for sharing consent between apps in same |
| 176 | + * domain. |
| 177 | + */ |
| 178 | + saveConsent() { |
| 179 | + if (this.consentDataLayerCommands.length > 0) { |
| 180 | + setCookie( |
| 181 | + GCM_SHARED_COOKIE_NAME, |
| 182 | + JSON.stringify(this.consentDataLayerCommands), |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Handles consent by updating the data layer with consent information. |
120 | 189 | *
|
121 |
| - * @param {string} consentCommand - The consent command "consent". |
| 190 | + * @param {string} consent - The consent command "consent". |
| 191 | + * @param consentCommand |
122 | 192 | * @param {string} command - The command "default" or "update".
|
123 | 193 | * @param {object} consentParams - The consent arguments.
|
124 | 194 | */
|
125 |
| - // eslint-disable-next-line no-unused-vars |
126 | 195 | write(consentCommand, command, consentParams) {
|
127 | 196 | // Without using the arguments reference, google debug mode would not seem to register the consent
|
128 | 197 | // that was written to the datalayer, so the parameters added to the function signature are only to
|
129 | 198 | // avoid mistakes when calling the function.
|
130 | 199 |
|
131 |
| - if ( |
132 |
| - this.config && |
133 |
| - typeof window !== 'undefined' && |
134 |
| - consentParams && |
135 |
| - !isEqual(this.lastConsent, consentParams) |
136 |
| - ) { |
137 |
| - // @ts-ignore |
| 200 | + if (typeof window !== 'undefined' && consentParams) { |
138 | 201 | window[this.dataLayer] = window[this.dataLayer] || [];
|
139 | 202 |
|
| 203 | + // eslint-disable-next-line prefer-rest-params |
140 | 204 | window[this.dataLayer].push(arguments);
|
141 |
| - this.lastConsent = consentParams; |
| 205 | + |
| 206 | + this.consentDataLayerCommands.push([ |
| 207 | + consentCommand, |
| 208 | + command, |
| 209 | + consentParams, |
| 210 | + ]); |
142 | 211 | }
|
143 | 212 | }
|
144 | 213 | }
|
|
0 commit comments