orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.68k stars 387 forks source link

[Feat]: Event / callback for categories #643

Open GeoJGH opened 4 months ago

GeoJGH commented 4 months ago

Description

V3.0.0

Services have the onAccept() and onReject() callbacks. There is no such things at the category level. The application has the onConsent() event.

The sequence of events occurs as follow: 1) service onAccept() 2) script execution for the enabled categories or services (ex <script type="text/plain" data-category="necessary">...</script>) 3) app event onConsent()

This is problematic if we want to do something when a category is accepted, before related scripts execution.

I am aware of two workarounds: 1) don't prevent scripts execution, but rather add, within the scripts, an onConsent() event listener and when the desired category is received, run the code. (see this post) The limitations of this approach are the non negligible extra code (vs simply adding a data-category attribute to an existing script), and the difficulties if a script execution should be impacted by the accepted/rejected status of multiple categories. 2) add a single service to the category. This limitation is more graphical than else, we now have 2 accepted/rejected sliders for the same category, as depicted here

Proposed solution

I propose to add the onAccept() and onReject() callbacks to the categories.

Alternatively, the app onConsent() event should be fired before executing the scripts associated with allowed categories or services .

Additional details

No response

orestbida commented 4 months ago

Look like a nice addition to me, extra flexibility.