Skip to content

Commit 9c186c4

Browse files
committed
Removed Osano, added CookieYes
1 parent 554d96a commit 9c186c4

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

src/index.html

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,78 @@
2424
<link rel="preconnect" href="https://connect.facebook.net" crossorigin="anonymous">
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">
27-
<link rel="stylesheet" href="https://ram.openstax.org/osano/osano.css">
28-
<script src="https://cmp.osano.com/AzZqbXTbzhHsU3cv1/68d8e8ae-4024-4489-a000-72766ad284a6/osano.js"></script>
27+
<script>
28+
fetch('/accounts/api/user', { credentials: 'include' }).then((userResponse) => {
29+
const updateUserConsentPreferences = (consentPreferences) => {
30+
if (userResponse.status === 200) {
31+
return fetch('/accounts/api/user', {
32+
body: JSON.stringify({consent_preferences: consentPreferences}),
33+
credentials: 'include',
34+
headers: { 'Content-Type': 'application/json' },
35+
method: 'PUT',
36+
}).then(
37+
(response) => {
38+
if (response.status === 200) {
39+
window.location.reload();
40+
} else {
41+
Sentry.captureMessage(`Failed to save cookie consent (HTTP status code: ${response.status})`);
42+
}
43+
}, (error) => Sentry.captureException(error)
44+
);
45+
}
46+
};
47+
48+
document.addEventListener('cookieyes_banner_load', async(eventData) => {
49+
const user = userResponse.status === 200 ? await userResponse.json() : undefined;
50+
const userConsentPreferences = user?.consent_preferences;
51+
52+
if (userConsentPreferences) {
53+
// Accounts remembers some state
54+
// We always defer to Accounts in this case
55+
56+
// Only call the CookieYes API if it doesn't match what's in Accounts
57+
let doUpdate = !eventData.detail.isUserActionCompleted;
58+
59+
userConsentPreferences.accepted.forEach((accepted) => {
60+
// There is no "necessary" switch
61+
if (accepted !== 'necessary') {
62+
document.getElementById(`ckySwitch${accepted}`).checked = true;
63+
if (!eventData.detail.categories[accepted]) { doUpdate = true; }
64+
}
65+
});
66+
userConsentPreferences.rejected.forEach((rejected) => {
67+
document.getElementById(`ckySwitch${rejected}`).checked = false;
68+
if (eventData.detail.categories[rejected]) { doUpdate = true; }
69+
});
70+
if (doUpdate) { performBannerAction('accept_partial'); }
71+
} else if (eventData.detail.isUserActionCompleted) {
72+
// Accounts doesn't have state but somehow CookieYes does
73+
// This can happen because of 3rd party cookies enabled, non-embedded launch, etc
74+
// Ideally we'd clear the CookieYes consent and let users click the banner again,
75+
// but there's no API to do that
76+
// So instead we make a request to save the user consent to Accounts
77+
const bannerConsentPreferences = { accepted: [], rejected: [] };
78+
Object.entries(eventData.detail.categories).forEach(([category, accepted]) => {
79+
const arr = accepted ? bannerConsentPreferences.accepted : bannerConsentPreferences.rejected;
80+
arr.push(category);
81+
});
82+
updateUserConsentPreferences(bannerConsentPreferences);
83+
} else {
84+
// If we don't have a recorded consent and they click the X button, interpret it as "reject all"
85+
document.getElementsByClassName('cky-banner-btn-close')[0]?.addEventListener(
86+
'click', () => performBannerAction('reject')
87+
);
88+
}
89+
});
90+
91+
document.addEventListener('cookieyes_consent_update', (eventData) => {
92+
updateUserConsentPreferences(eventData.detail);
93+
});
94+
});
95+
</script>
96+
<!-- Start cookieyes banner -->
97+
<script id="cookieyes" type="text/javascript" src="https://cdn-cookieyes.com/client_data/7c03144a7ef8b7f646f1ce01/script.js"></script>
98+
<!-- End cookieyes banner -->
2999
<script src="//script.crazyegg.com/pages/scripts/0084/9271.js" async="async"></script>
30100
</head>
31101
<body class="page-loading">

0 commit comments

Comments
 (0)