Skip to content

Commit 741f31a

Browse files
authored
Cookie consent & Google consent v2 updates (#522)
* Dispatch a `CookieConsent` event from the cookie consent module whenever the consent is updated. * Listen for cookie consent updates and update GA consent accordingly. * Keep consent properties in a single place and reference where needed. * Compiled JavaScript * Version bump to v14.3.9
1 parent 7635426 commit 741f31a

File tree

6 files changed

+80
-24
lines changed

6 files changed

+80
-24
lines changed

dist/honeycomb.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function _arrayLikeToArray(arr, len) {
7979
}
8080
var accountId;
8181
var crossDomainAccountId;
82+
var consentProperties = ['ad_storage', 'ad_user_data', 'ad_personalization', 'analytics_storage'];
8283
var init = function init() {
8384
// If the account ID is not set, then don't carry on.
8485
if (!accountId || accountId === 'G-XXX') {
@@ -146,13 +147,13 @@ var initAccount = function initAccount(accountId) {
146147
};
147148

148149
// Set default values of consent to denied.
149-
window.gtag('consent', 'default', {
150-
ad_storage: 'denied',
151-
ad_user_data: 'denied',
152-
ad_personalization: 'denied',
153-
analytics_storage: 'denied',
150+
var defaultConsent = {
154151
wait_for_update: 500
152+
};
153+
consentProperties.forEach(function (prop) {
154+
defaultConsent[prop] = 'denied';
155155
});
156+
window.gtag('consent', 'default', defaultConsent);
156157
window.gtag('js', new Date());
157158

158159
// Add account IDs.
@@ -168,12 +169,11 @@ var initAccount = function initAccount(accountId) {
168169
// Update consent for storing cookies if targeting consent given.
169170
var hasTargetingConsent = _honeycomb["default"].hasConsent('targeting');
170171
if (hasTargetingConsent) {
171-
window.gtag('consent', 'update', {
172-
ad_storage: 'granted',
173-
ad_user_data: 'granted',
174-
ad_personalization: 'granted',
175-
analytics_storage: 'granted'
172+
var updatedConsent = {};
173+
consentProperties.forEach(function (prop) {
174+
updatedConsent[prop] = 'granted';
176175
});
176+
window.gtag('consent', 'update', updatedConsent);
177177
}
178178
};
179179

@@ -269,6 +269,19 @@ var setOptimizeId = function setOptimizeId() {
269269
var setSites = function setSites() {
270270
(0, _honeycombNotification.logDeprecatedFunctionToConsole)('setSites', 'Google Analytics', '14.2.0');
271271
};
272+
273+
// Listen for cookie consent updates and update consent based on the
274+
// "targeting" group.
275+
window.addEventListener('CookieConsent', function (e) {
276+
var consent = Object.prototype.hasOwnProperty.call(e.detail.groups, 'targeting') && e.detail.groups.targeting == 1 ? 'granted' : 'denied';
277+
if (typeof window.gtag === 'function') {
278+
var updatedConsent = {};
279+
consentProperties.forEach(function (prop) {
280+
updatedConsent[prop] = consent;
281+
});
282+
window.gtag('consent', 'update', updatedConsent);
283+
}
284+
});
272285
var _default = exports["default"] = {
273286
init: init,
274287
accountId: accountId,
@@ -1567,6 +1580,8 @@ var hasConsent = function hasConsent() {
15671580
* Set the consent cookie with the groups consent statuses, and for it to
15681581
* expire in 31 days if cookies accepted, and 6 months if rejected.
15691582
*
1583+
* Also dispatch a `CookieConsent` event which includes the consent group data.
1584+
*
15701585
* @param {Object|Null} groups The groups object with the group as the property,
15711586
* and the consent status as the value (0|1)
15721587
* @param {Boolean} status The status to set consent to if groups is null.
@@ -1586,6 +1601,12 @@ var setHasConsent = function setHasConsent() {
15861601
'max-age': status === 1 ? 2678400 : 16070400,
15871602
domain: getConsentCookieDomain()
15881603
});
1604+
window.dispatchEvent(new CustomEvent('CookieConsent', {
1605+
bubbles: true,
1606+
detail: {
1607+
groups: groups
1608+
}
1609+
}));
15891610
};
15901611

15911612
/**

dist/honeycomb.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "honeycomb-web-toolkit",
3-
"version": "14.3.8",
3+
"version": "14.3.9",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/red-gate/honeycomb-web-toolkit"

src/analytics/js/honeycomb.analytics.google.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { logDeprecatedFunctionToConsole } from '../../notification/js/honeycomb.
55
let accountId;
66
let crossDomainAccountId;
77

8+
const consentProperties = [
9+
'ad_storage',
10+
'ad_user_data',
11+
'ad_personalization',
12+
'analytics_storage',
13+
];
14+
815
const init = () => {
916

1017
// If the account ID is not set, then don't carry on.
@@ -89,13 +96,13 @@ const initAccount = (accountId, crossDomainAccountId = null) => {
8996
};
9097

9198
// Set default values of consent to denied.
92-
window.gtag('consent', 'default', {
93-
ad_storage: 'denied',
94-
ad_user_data: 'denied',
95-
ad_personalization: 'denied',
96-
analytics_storage: 'denied',
99+
const defaultConsent = {
97100
wait_for_update: 500,
101+
};
102+
consentProperties.forEach((prop) => {
103+
defaultConsent[prop] = 'denied';
98104
});
105+
window.gtag('consent', 'default', defaultConsent);
99106

100107
window.gtag('js', new Date());
101108

@@ -112,12 +119,11 @@ const initAccount = (accountId, crossDomainAccountId = null) => {
112119
// Update consent for storing cookies if targeting consent given.
113120
const hasTargetingConsent = cookieConsent.hasConsent('targeting');
114121
if (hasTargetingConsent) {
115-
window.gtag('consent', 'update', {
116-
ad_storage: 'granted',
117-
ad_user_data: 'granted',
118-
ad_personalization: 'granted',
119-
analytics_storage: 'granted',
122+
const updatedConsent = {};
123+
consentProperties.forEach((prop) => {
124+
updatedConsent[prop] = 'granted';
120125
});
126+
window.gtag('consent', 'update', updatedConsent);
121127
}
122128
};
123129

@@ -211,6 +217,24 @@ const setSites = (...args) => {
211217
logDeprecatedFunctionToConsole('setSites', 'Google Analytics', '14.2.0');
212218
};
213219

220+
// Listen for cookie consent updates and update consent based on the
221+
// "targeting" group.
222+
window.addEventListener('CookieConsent', (e) => {
223+
const consent =
224+
Object.prototype.hasOwnProperty.call(e.detail.groups, 'targeting') &&
225+
e.detail.groups.targeting == 1
226+
? 'granted'
227+
: 'denied';
228+
229+
if (typeof window.gtag === 'function') {
230+
const updatedConsent = {};
231+
consentProperties.forEach((prop) => {
232+
updatedConsent[prop] = consent;
233+
});
234+
window.gtag('consent', 'update', updatedConsent);
235+
}
236+
});
237+
214238
export default {
215239
init,
216240
accountId,

src/cookie-consent/js/honeycomb.cookie-consent.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ const hasConsent = ( group = null ) => {
230230
* Set the consent cookie with the groups consent statuses, and for it to
231231
* expire in 31 days if cookies accepted, and 6 months if rejected.
232232
*
233+
* Also dispatch a `CookieConsent` event which includes the consent group data.
234+
*
233235
* @param {Object|Null} groups The groups object with the group as the property,
234236
* and the consent status as the value (0|1)
235237
* @param {Boolean} status The status to set consent to if groups is null.
@@ -249,6 +251,15 @@ const setHasConsent = ( groups = null, status = true ) => {
249251
'max-age': (status === 1) ? 2678400 : 16070400,
250252
domain: getConsentCookieDomain(),
251253
});
254+
255+
window.dispatchEvent(
256+
new CustomEvent('CookieConsent', {
257+
bubbles: true,
258+
detail: {
259+
groups,
260+
},
261+
})
262+
);
252263
};
253264

254265
/**

0 commit comments

Comments
 (0)