Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-11441: Changed sentry logging to send only items that are not in cookie settings #905

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 49 additions & 28 deletions modules/hdbt_cookie_banner/assets/js/hdbt-cookie-banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,58 @@ class UnapprovedItemError extends Error {
// Attach a behavior to capture unapproved cookies with Sentry.
Drupal.behaviors.unapprovedCookies = {
attach: function attach() {
window.addEventListener(
'hds-cookie-consent-unapproved-item-found',
(e) => {
if (typeof window.Sentry === 'undefined') {
return;
}
const { storageType, keys, acceptedGroups } = e.detail
const apiUrl = drupalSettings.hdbt_cookie_banner.apiUrl;
fetch(apiUrl)
.then(response => response.json())
.then(jsonData => {

// Function to extract cookie names from each group
const extractCookiePatterns = (groups) =>
groups?.flatMap(group => group.cookies.map(cookie => cookie.name)) || [];

// Alphabetize the keys array
const sortedKeys = keys.sort();
// Collect all allowed cookie name patterns
const validItems = [
...extractCookiePatterns(jsonData.optionalGroups),
...extractCookiePatterns(jsonData.requiredGroups),
...extractCookiePatterns(jsonData.robotGroups),
];

// Sentry requires a unique name for each error in order to record
// each found unapproved item per type.
const name = `Unapproved ${storageType}`
const message = `Found: ${sortedKeys.join(', ')}`
window.addEventListener(
'hds-cookie-consent-unapproved-item-found',
(e) => {
if (typeof window.Sentry === 'undefined') {
return;
}

// Capture the error with Sentry and send a message with the
// unapproved items so that they can be searched in Sentry.
window.Sentry.captureException(new UnapprovedItemError(message, name), {
level: 'warning',
tags: {
approvedCategories: acceptedGroups.join(', '),
},
extra: {
storageType,
cookieNames: sortedKeys,
approvedCategories: acceptedGroups,
},
})
}
)
const { storageType, keys, acceptedGroups } = e.detail;
const sortedKeys = keys.sort();

// Check which keys do not match any pattern in the valid items list
const unapprovedItems = sortedKeys.filter(
key => !validItems.some(pattern => key.includes(pattern.replace('*', '')))
).sort();

// Only log if there are unapproved items that are not found in our list
if (unapprovedItems.length > 0) {
const name = `Unapproved ${storageType}`;
const message = `Found: ${unapprovedItems.join(', ')}`;

window.Sentry.captureException(new UnapprovedItemError(message, name), {
level: 'warning',
tags: {
approvedCategories: acceptedGroups.join(', '),
},
extra: {
storageType,
missingCookies: unapprovedItems,
approvedCategories: acceptedGroups,
},
});
}
}
);
})
.catch(error => console.error('Failed to fetch JSON:', error));
},
}
})(Drupal, drupalSettings);
Loading