Skip to content

Commit b57f8cd

Browse files
committed
Remove Sentry calls from CookieYes snippet, store user in window variable to prevent multiple loads
1 parent f68309d commit b57f8cd

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

src/app/models/usermodel.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual';
33
import throttle from 'lodash/throttle';
44

55
const settings = window.SETTINGS;
6-
const accountsUrl = `${settings.accountHref}/api/user`;
6+
const accountsUrl = `${settings.accountHref}/api/user?always_200=true`;
77

88
function cached(fn) {
99
let valid = false;
@@ -64,33 +64,25 @@ const accountsModel = {
6464
// ]
6565
// });
6666

67-
return fetch(accountsUrl, {credentials: 'include'})
68-
.then(
69-
(response) => {
70-
if (response.status === 403) {
71-
return {};
72-
}
73-
return response.json().then(
74-
(result) => {
75-
if (window.dataLayer) {
76-
window.dataLayer.push({
77-
faculty_status: result.faculty_status
78-
});
79-
}
80-
return result;
81-
},
82-
(err) => {
83-
console.warn('No JSON in Accounts response');
84-
return {err};
85-
}
86-
);
87-
},
88-
(err) => {
89-
console.warn('"Error fetching user info"');
90-
return {err};
67+
window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', { credentials: 'include' }).then(
68+
(response) => {
69+
if (response.status === 200) {
70+
return response.json().catch((error) => {
71+
throw new Error(`Failed to parse user JSON: ${error.message}`);
72+
});
73+
} else {
74+
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
9175
}
92-
)
93-
.catch((err) => {throw new Error(`Unable to fetch user data: ${err}`);});
76+
}
77+
);
78+
return window._OX_USER_PROMISE.then((user) => {
79+
if (window.dataLayer) {
80+
window.dataLayer.push({
81+
faculty_status: user.faculty_status
82+
});
83+
}
84+
return user;
85+
});
9486
})
9587
};
9688

@@ -165,6 +157,7 @@ const userModel = {
165157

166158
const throttledLoginCheck = throttle((setData) => {
167159
accountsModel.load().then((oldUser) => {
160+
window._OX_USER_PROMISE = undefined;
168161
accountsModel.load.invalidate();
169162
accountsModel.load().then((newUser) => {
170163
if (!isEqual(oldUser, newUser)) {

src/index.html

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@
2525
<link rel="prefetch" href="<%= process.env.API_ORIGIN %>/apps/cms/api/books/?format=json">
2626
<link rel="prefetch" href="<%= process.env.API_ORIGIN %>/apps/cms/api/footer/?format=json">
2727
<script>
28-
fetch('/accounts/api/user', { credentials: 'include' }).then((userResponse) => {
28+
window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', { credentials: 'include' }).then(
29+
(response) => {
30+
if (response.status === 200) {
31+
return response.json().catch((error) => {
32+
throw new Error(`Failed to parse user JSON: ${error.message}`);
33+
});
34+
} else {
35+
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
36+
}
37+
}
38+
);
39+
window._OX_USER_PROMISE.then((user) => {
2940
const updateUserConsentPreferences = (consentPreferences) => {
30-
if (userResponse.status === 200) {
41+
if (user.id) {
3142
return fetch('/accounts/api/user', {
3243
body: JSON.stringify({consent_preferences: consentPreferences}),
3344
credentials: 'include',
@@ -38,9 +49,9 @@
3849
if (response.status === 200) {
3950
window.location.reload();
4051
} else {
41-
Sentry.captureMessage(`Failed to save cookie consent (HTTP status code: ${response.status})`);
52+
throw new Error(`Failed to save cookie consent: HTTP ${response.status} status code`);
4253
}
43-
}, (error) => Sentry.captureException(error)
54+
}
4455
);
4556
}
4657
};
@@ -79,7 +90,7 @@
7990
const arr = accepted ? bannerConsentPreferences.accepted : bannerConsentPreferences.rejected;
8091
arr.push(category);
8192
});
82-
updateUserConsentPreferences(bannerConsentPreferences);
93+
return updateUserConsentPreferences(bannerConsentPreferences);
8394
} else {
8495
// If we don't have a recorded consent and they click the X button, interpret it as "reject all"
8596
document.getElementsByClassName('cky-banner-btn-close')[0]?.addEventListener(
@@ -88,9 +99,9 @@
8899
}
89100
});
90101

91-
document.addEventListener('cookieyes_consent_update', (eventData) => {
92-
updateUserConsentPreferences(eventData.detail);
93-
});
102+
document.addEventListener(
103+
'cookieyes_consent_update', (eventData) => updateUserConsentPreferences(eventData.detail)
104+
);
94105
});
95106
</script>
96107
<!-- Start cookieyes banner -->

0 commit comments

Comments
 (0)