|
34 | 34 | setupOsanoPreload(); |
35 | 35 |
|
36 | 36 | var CONSENT_STORAGE_KEY = 'osano-gitbook-last-consent-decision'; |
37 | | - var visitorActed = false; |
38 | | - var pendingDecision = null; |
| 37 | + var initialized = false; |
39 | 38 |
|
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; |
48 | 42 |
|
49 | | - function forward(decision) { |
50 | 43 | 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; |
57 | 53 | if (w.sessionStorage.getItem(CONSENT_STORAGE_KEY) === decision) return; |
58 | 54 | w.sessionStorage.setItem(CONSENT_STORAGE_KEY, decision); |
59 | 55 |
|
60 | | - if (decision === 'approve') { |
| 56 | + if (hasNonEssential) { |
61 | 57 | onApprove(); |
62 | 58 | } else { |
63 | 59 | onReject(); |
|
67 | 63 | } |
68 | 64 | } |
69 | 65 |
|
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; |
100 | 68 | }); |
| 69 | + w.Osano('onConsentSaved', emitConsent); |
101 | 70 |
|
102 | 71 | injectOsano(); |
103 | 72 | }); |
|
0 commit comments