Openli
IntroductionGetting StartedTechnical DocumentationGet help
  • Getting started
    • Introduction
    • Dictionary
    • FAQ
      • IAB TCF v2.0
  • Privacy hub
    • How to create your privacy profile
    • How can I get an Openli Privacy Badge?
    • Vendor owners
    • Why are custom properties on vendors super valuable?
    • Setting up custom properties
  • Tasks
  • Cookie Consent Management
    • Quickstart
    • Checklist: Cookies
    • Cookie widget
      • Create a cookie policy
      • Create a cookie widget
      • Install the widget
      • Manually add any cookies
      • Customise your widget
      • How to get color codes
      • Set the widget language
      • Use custom link/button instead of Cookie Shield
      • Add company name to cookie pop-up
    • Categorise your cookies
    • Adding information to cookies
    • Adding information to your cookie providers
    • Blocking Cookies with Openli
    • Cookie expiration dates and lifespan
      • Changing expiration on Google Analytics cookies
  • Policy and agreement management
    • Generate your privacy policy
    • Changing an old cookie policy to a new on autopilot
    • Embed your policies and legal agreements
      • Embedding agreements without displaying a widget
    • Install the Privacy Badge
  • General Openli guides
    • Setting up SSO (Single Sign-On)
      • SSO with Azure AD (Microsoft)
      • SSO with Google
      • SSO with Okta
      • How to log in with SSO
    • Add a website to your Openli account
    • Using projects to handle multiple languages
    • Collect consents without storing IP addresses or user-agents
  • Technical documentation
    • Widget
      • Collect cookie consent
        • Blocking cookies
        • Blocking embedded media
        • Cookie-widget options
        • Consent state API
    • API
      • Services
        • Personal data
        • Subprocessors
    • Cookies set by Openli
  • System Specific Integration Guides
    • Cloudflare
  • Legacy integrations
    • Google Consent Mode
    • Google Tag Manager
      • Block cookies with GTM
      • Install Openli with GTM
      • Prevent triggers from firing in GTM
    • HubSpot
    • Pardot
    • Shopify
      • Install cookie widget
      • Integrate consent with checkout
    • Square Online
    • Squarespace
    • Webflow
    • Wix
Powered by GitBook
On this page
  • Retrieving the cookie consent state
  • Use cases
  • Listening for consent-change events
  1. Technical documentation
  2. Widget
  3. Collect cookie consent

Consent state API

An easy way to get the current cookie consent state.

PreviousCookie-widget optionsNextAPI

Last updated 2 years ago

Retrieving the cookie consent state

When users have selected their preferences for consent, Openli will store a record of their consent choices.

In some cases, you might want to have a look at the given consent, so you can do (or not do) certain actions related to consent choices. We've made an easy way for you to get access to the cookie consent state by using a simple JavaScript command (which can be used in all scripts with access to the browser'swindow object):

window.legal.getCookieConsentState(); 
// Returns an object like:
// { analytics: true|false, marketing: true|false }

Use cases

Google Tag Manager

A common use case for this API call is with Google Tag Manager, e.g. making triggers fire only under certain conditions. For an in-depth example, check out .

Listening for consent-change events

It is also possible to register standard JavaScript DOM event-listeners to enable your own code to respond when a user changes their consent preferences.

To ensure that your code is run as you intend, you must register listeners before your call tolegal.widget(...) for the cookie widget on your page.

Event name

Event description

legalmonster.cookie.analytics.accepted

Triggered whenever a user gives consent to the analytics category. Can occur more than once.

legalmonster.cookie.analytics.rejected

Triggered whenever a user rejects consent to the analytics category. Can occur more than once.

legalmonster.cookie.marketing.accepted

Triggered whenever a user gives consent to the marketing category. Can occur more than once.

legalmonster.cookie.marketing.rejected

Triggered whenever a user rejects consent to the marketing category. Can occur more than once.

Here is an example of using one of these events to invoke some custom code:

function handleAnalyticsAccepted() {
    console.log("User accepted analytics.");
    // Take any other action you want to.
}

window.addEventListener(
    // The event name to listen for.
    "legalmonster.cookie.analytics.accepted",
    // A reference to your custom function.
    handleAnalyticsAccepted,
    // Optional: remove the listener after the first instance of the event.
    { once: true }
);
Prevent triggers from firing in Google Tag Manager