Accessing Consent in JavaScript (SDK)

Complete reference for the Ours Privacy CMP JavaScript SDK, including methods to read consent state, update consent, and control banner visibility.

Accessing Consent in JavaScript (SDK)

You can interact with the CMP on your site using the global window.ours_consent object. This object provides a limited set of methods to read and update user consent, as well as control the visibility of the consent banner or modal.

Note: For most users, you do not need to use these methods directly. The consent UI and banner handle all standard consent flows for you. These APIs are intended for advanced or custom integration scenarios only.


SDK Methods

The following methods are available on window.ours_consent:

1. getConsent()

Get the full consent object. Returns the current consent state, including which categories are accepted and rejected.

The acceptedCategories array always reflects the categories that are currently active — even before the visitor has interacted with the banner:

  • Opt-out mode: All categories configured as enabled appear in acceptedCategories by default, since they are active until the visitor explicitly opts out.
  • Opt-in mode: Only read-only categories (e.g. necessary) appear in acceptedCategories by default. Non-essential categories remain empty until the visitor explicitly accepts them.

The rejectedCategories array reflects categories that are currently inactive. In the initial state, this can include categories the visitor has not explicitly accepted yet. Use type === '' to detect that no explicit consent action has been taken.

After the visitor makes a choice, acceptedCategories and rejectedCategories continue to reflect the active and inactive sets.

const consent = window.ours_consent.getConsent();
// consent = {
//   type: 'all' | 'custom' | 'necessary' | '',
//   acceptedCategories: string[],
//   rejectedCategories: string[],
// }

The type field indicates how consent was given:

  • '' — No explicit consent action yet (initial state)
  • 'all' — Visitor accepted all categories that can be accepted
  • 'necessary' — Visitor accepted only necessary categories
  • 'custom' — Visitor made a custom selection

If your policy includes a category that is set to Disabled and Read-only, calling acceptAll() may still return 'custom' because that category remains off and cannot be accepted.


2. acceptCategory(category)

Programmatically accept one or more categories. This method accepts a string or an array of strings representing the categories to accept.

Important: This method replaces the entire consent state. It accepts the categories you pass in and rejects all non-necessary categories (categories not in the list). Each call replaces the previous state, so you cannot call this method multiple times in a row to incrementally add categories.

// Accept a single category
window.ours_consent.acceptCategory('analytics');

// Accept multiple categories
window.ours_consent.acceptCategory(['analytics', 'marketing']);

3. rejectCategory(category)

Programmatically reject one or more categories. This method accepts a string or an array of strings representing the categories to reject.

This method adds the specified categories to the rejected categories list without replacing the entire consent state.

// Reject a single category
window.ours_consent.rejectCategory('analytics');

// Reject multiple categories
window.ours_consent.rejectCategory(['analytics', 'marketing']);

4. acceptAll()

Accept all cookie categories at once. This is a convenience method that accepts all available categories in a single call.

window.ours_consent.acceptAll();
// All categories are now accepted

If your configuration includes categories that are permanently disabled, those categories remain rejected after acceptAll().


5. necessaryOnly()

Accept only necessary (readonly) cookie categories, rejecting all others. This is a convenience method that sets consent to only the necessary categories.

window.ours_consent.necessaryOnly();
// Only necessary categories are accepted, all others are rejected

6. getAcceptedCategories()

Get a list of all currently active (accepted) categories. This follows the same rules as getConsent().acceptedCategories — in opt-out mode it includes all default-enabled categories, and in opt-in mode it includes only read-only categories (e.g. necessary) until the visitor makes an explicit choice.

const accepted = window.ours_consent.getAcceptedCategories();
// Opt-out (before action): ['necessary', 'analytics', 'advertising']
// Opt-in (before action):  ['necessary']
// After accept all:        ['necessary', 'analytics', 'advertising']

7. show()

Show the consent banner or modal programmatically.

window.ours_consent.show();

8. showPreferences()

Show the preferences modal programmatically. Similar to show(), but opens the preferences interface instead of the main consent banner or modal.

window.ours_consent.showPreferences();

9. hide()

Hide the consent banner or modal programmatically.

window.ours_consent.hide();

10. on(event, callback)

Subscribe to consent-related events. This allows you to run custom code when consent changes or is first set.

  • Supported events:
    • change: Fired when the user modifies their preferences and only if consent has already been provided.
    • firstConsent: Fired only the very first time that the user expresses their choice of consent (accept/reject).
    • consent: Fired the very first time the user expresses their choice of consent — just like firstConsent — but also on every subsequent page load.
    • close: Fired when the consent modal is dismissed via the close button.

Example:

window.ours_consent.on('change', (consent) => {
  console.log('Consent changed:', consent);
});

window.ours_consent.on('firstConsent', (consent) => {
  console.log('First consent set:', consent);
});

window.ours_consent.on('close', () => {
  console.log('Consent modal was closed');
});

11. setLanguage(languageCode)

Override the consent banner's display language at runtime. This is useful when you have pages in a specific language and want the banner to match, regardless of the visitor's browser language.

  • languageCode — a BCP 47 language code (e.g. 'es', 'fr', 'de').
  • Returns a Promise<boolean> — resolves to true if the language was applied, false if the language is not available.

Prerequisite: The language must already have translations configured in the Ours Privacy admin. If the requested language has not been set up, the method returns false and the banner stays in its current language.

// Force the banner into Spanish on a Spanish-language page
window.ours_consent.setLanguage('es');

Page-specific override example:

<script>
  // On your /espanol/ page, override banner language after the CMP loads
  window.ours_consent.setLanguage('es');
</script>

Tip: Always check that window.ours_consent is loaded before calling these methods. These APIs are intended for advanced integrations and most users will not need to use them directly.


Next Steps

How is this guide?

On this page