Skip to content

Commit 1e47315

Browse files
authored
Ignore consent Osano saves on its own during initialization (#1295)
* Ignore consent Osano saves on its own during initialization * changeset * Shorten comments
1 parent 9ea97ea commit 1e47315

2 files changed

Lines changed: 22 additions & 48 deletions

File tree

‎.changeset/osano-init-consent.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-osano': patch
3+
---
4+
5+
Keep the Osano banner on screen on informational (timer) banners. Osano auto-accepts, saves and fires a dialog hide during initialization on those, which the integration mistook for a visitor decision and reloaded the page under the banner. Only consent saved after Osano reports initialized is forwarded to GitBook now.

‎integrations/osano/src/script.raw.js‎

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,26 @@
3434
setupOsanoPreload();
3535

3636
var CONSENT_STORAGE_KEY = 'osano-gitbook-last-consent-decision';
37-
var visitorActed = false;
38-
var pendingDecision = null;
37+
var initialized = false;
3938

40-
function decisionFor(consent) {
41-
var hasNonEssential =
42-
!!consent &&
43-
NON_ESSENTIAL_CATEGORIES.some(function (category) {
44-
return consent[category] === 'ACCEPT';
45-
});
46-
return hasNonEssential ? 'approve' : 'reject';
47-
}
39+
function emitConsent(consent) {
40+
// Consent saved before Osano is initialized is Osano's own, not the visitor's.
41+
if (!initialized) return;
4842

49-
function forward(decision) {
5043
try {
51-
// onConsentSaved replays the visitor's existing decision on every
52-
// page load, not just when it changes. GitBook's onApprove/onReject
53-
// can trigger a reload to reinitialize scripts, so forwarding every
54-
// replay would reload -> replay -> reload forever. Only forward the
55-
// decision when it's actually different from last time, and persist
56-
// that in sessionStorage since it must survive the reload.
44+
var hasNonEssential =
45+
!!consent &&
46+
NON_ESSENTIAL_CATEGORIES.some(function (category) {
47+
return consent[category] === 'ACCEPT';
48+
});
49+
var decision = hasNonEssential ? 'approve' : 'reject';
50+
51+
// onApprove/onReject reload the page, so skip decisions GitBook already holds.
52+
if (w.GitBook.isCookiesTrackingDisabled() === !hasNonEssential) return;
5753
if (w.sessionStorage.getItem(CONSENT_STORAGE_KEY) === decision) return;
5854
w.sessionStorage.setItem(CONSENT_STORAGE_KEY, decision);
5955

60-
if (decision === 'approve') {
56+
if (hasNonEssential) {
6157
onApprove();
6258
} else {
6359
onReject();
@@ -67,37 +63,10 @@
6763
}
6864
}
6965

70-
function emitConsent(consent) {
71-
var decision = decisionFor(consent);
72-
73-
// In permissive mode Osano saves a default consent on its own while the
74-
// banner is still up. Forwarding that reloads the page under the banner,
75-
// and Osano then treats the consent as given and never shows it again.
76-
// Hold the decision until the visitor has actually done something.
77-
if (!visitorActed) {
78-
pendingDecision = decision;
79-
return;
80-
}
81-
82-
forward(decision);
83-
}
84-
85-
function onVisitorActed() {
86-
visitorActed = true;
87-
if (pendingDecision) {
88-
var decision = pendingDecision;
89-
pendingDecision = null;
90-
forward(decision);
91-
}
92-
}
93-
94-
w.Osano('onConsentSaved', emitConsent);
95-
// The dialog or drawer only closes on visitor input.
96-
w.Osano('onUiChanged', function (component, state) {
97-
if ((component === 'dialog' || component === 'drawer') && state === 'hide') {
98-
onVisitorActed();
99-
}
66+
w.Osano('onInitialized', function () {
67+
initialized = true;
10068
});
69+
w.Osano('onConsentSaved', emitConsent);
10170

10271
injectOsano();
10372
});

0 commit comments

Comments
 (0)