For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

In manager.consent, you have the following methods:

  • open(): opens the consent widget

  • close(): closes the consent widget

  • getAllConsents(): retrieve all consents

  • hasConsented(): returns true if the user has already recorded an explicit consent

  • getPurposeConsent(purposeLabel:string): get consent for a cookie category

  • setPurposeConsent(purposeLabel:string, 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 service

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)

Cookie categories are identified by the following string labels:

Category
Label

Necessary

Necessary

Preferences

Preference

Analytics

Analytical

Marketing

Marketing

Other

Other

Unclassified

Unclassified

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

The following example shows how to apply a full opt-out programmatically β€” for instance when the user clicks a "Reject all" button you have built yourself, or when honouring a browser privacy signal.

You can equally opt-in selectively β€” for example to accept analytics only:

Every time a user changes their consent choices β€” through the widget or programmatically via save() β€” Dastra dispatches a dastra:consents:updated event on window. You can subscribe to it to trigger your own logic: loading third-party scripts, updating the UI, firing an analytics event, etc.

This event fires only when save() is called β€” it does not fire after dispatchEvent() (session-only application without persistence).

Last updated

Was this helpful?