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.

The dastra consent service can be accessed in this way:

<script>
dastra = dastra || []
dastra.push(['cookieReady',function(manager){
    console.log(manager.consent)
});
</script>

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:

Type
Id

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.

Location of simplified cookie name

Example of use

To illustrate this, here is a complete example that allows you to manipulate consents in the browser without using the widget :

Last updated

Was this helpful?