Manage consent programmatically
This chapter will teach you how to manage the consents of our cookie widget programmatically.
Where are the consents stored?
The entire proof of user consent is stored in the browser's localStorage (the storage key is named dastra-consents) in json format. The propagation of consents depends on it, that's why it's not recommended to directly modify the data of this key
It's not recommended to directly modify the data in the localStorage. Preferably use the Javascript SDK of dastra.
Access to the consent service
The dastra consent service can be accessed in this way:
List of methods available in the consent manager
In manager.consent, you have the following methods:
open(): opens the consent widget
close(): close the consent widget
getAllConsents() : retrieve all consents
getPurposeConsent(purposeId:number): get consent for a category of cookies
setPurposeConsent(purposeId:number, consent:bool): set the consent for a category
getServiceConsent(serviceShortName:string): Retrieves consent for a particular service
setServiceConsent(serviceShortName:string, consent:bool): Sets the consent for a particular element
Get the list of user's consents (getAllConsents)
Once you access the consent manager, it is very easy to retrieve the consents of the current user:
The above method returns a list of all the user's consents
Query consents by category (getPurposeConsent/setPurposeConsent)
The categories of cookies are represented by the following ids:
Necessary
0
Preferences
1
Analytical
2
Marketing
3
Other
4
Handle consents by service
To manipulate consents by service, you will need the simplified service name available in the service management interface of your widget.
How to find the simplified name of the service? Go to the service management interface, when editing a service, the simplified name (slug) of the service appears below the cookie name.
Exemple d'utilisation
Pour illustrer la chose, voici un exemple complet qui permet de manipuler les consentements dans le navigateur sans utiliser le widget :
Dernière mise à jour