Problem/Motivation
The opt-out switch (pianoanalytics-optout-switch.js) has three issues:
-
Unchecking the switch hardcodes the consent mode back to 'essential':
If the visitor had previously granted full consent ('opt-in', e.g. through a CMP integration), that choice is silently lost: after an opt-out round trip they end up in 'essential' instead of their actual prior choice.
-
Toggling the switch only updates localStorage, the pa_consent_mode cookie and the body class. The live SDK keeps its current purposes until the next page load.
-
In _configurePrivacy(), when localStorage holds no valid mode and cnil_exempt is disabled, setAllPurposes() is never called and setConsentBodyClass() receives null, so the SDK state and the body class do not reflect any effective mode.
Steps to reproduce
- Grant full consent so pianoanalytics_consent_mode = 'opt-in'.
- Check the opt-out switch, then uncheck it.
- localStorage now holds 'essential' instead of the original 'opt-in'.
- Also: check the switch and observe that hits on the current page are still sent with the previous purposes until a reload.
Proposed resolution
- When toggling, save the current mode into a new pianoanalytics_consent_mode_prior localStorage key — only when it is not 'opt-out', so the saved value is always a real consent choice — and restore it when the switch is unchecked (removing the consent mode entirely when no prior exists).
- After a toggle, re-apply Drupal.pianoanalytics._configurePrivacy() (guarded on window.pa) so the new mode takes effect immediately, without a page reload.
- In _configurePrivacy(), fall back to paConsent.getMode() when no stored or exempt mode applies, and call setAllPurposes() unconditionally so SDK purposes and body class always reflect the effective mode.
The _prior key also gives consent integrations (e.g. a tarteaucitron bridge) a place to record the visitor's banner decision while the opt-out switch is active, so it can be restored on opt-back-in.
API changes
New localStorage key: pianoanalytics_consent_mode_prior.
No PHP API changes.
Issue fork pianoanalytics-3593679
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
funstenolf commentedI added a merge request that do work of restoring prior consent mode after unchecking the opt-out switch.
Comment #5
mably commented