Manage consent programmatically
This chapter will teach you how to manage the consents of our cookie widget programmatically.
Where are the consents stored?
By default, consents are stored in a browser cookie named consent-eu, as serialised JSON, for 180 days. In the banner configuration, you can:
change the name of this cookie (Cookie consent name);
change its lifetime (Consent expiry time) and its domain (Consent cookie domain);
store consents in localStorage instead (Enable localStorage store for consent cookies). The key is then
dastra-consents, or the name chosen for the cookie.
Access to the consent service
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>List of methods available in the consent manager
In manager.consent, you have the following methods:
getAllConsents(): retrieve all consents
hasConsented(): returns
trueif the user has already recorded an explicit consentgetPurposeConsent(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
save(): stores the consents and dispatches the events
dispatchEvents(): applies the current consents (events, Google Consent Mode, scripts) without storing them
To open or close the widget, use manager.open() and manager.close(). Outside the cookieReady callback, use dastra.push(['openCookieConsent']) and dastra.push(['closeCookieConsent']).
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:
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.

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:
Reacting to consent updates (the dastra:consents:updated event)
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.
Last updated
Was this helpful?