JavaScript SDK

Reference for the client-side Consent Trustee API and events.

Global Object

Once the Consent Trustee script loads, it exposes a global window.ConsentTrusteeobject that you can use to interact with the consent banner programmatically.

// Check if ConsentTrustee is loaded
if (window.ConsentTrustee) {
  console.log('ConsentTrustee is ready');
}

Methods

ConsentTrustee.getConsent()

Returns the current consent state for all categories.

Returns

{
  necessary: true,    // Always true
  preferences: false,
  statistics: true,
  marketing: false
}

Example

const consent = window.ConsentTrustee.getConsent();
if (consent.statistics) {
  // User has consented to statistics
  loadGoogleAnalytics();
}
ConsentTrustee.showBanner()

Programmatically show the consent banner.

Example

// Add a "Manage Cookies" button
document.getElementById('manage-cookies').onclick = () => {
  window.ConsentTrustee.showBanner();
};
ConsentTrustee.showPreferences()

Open the preferences modal directly, allowing users to customize their consent.

Example

// Footer link to manage cookie preferences
<a href="#" onclick="window.ConsentTrustee.showPreferences(); return false;">
  Cookie Settings
</a>
ConsentTrustee.setConsent(categories)

Set consent for specific categories programmatically. Pass true for accept-all or false for reject-all-optional by toggling the values yourself.

Example

// Custom selection
window.ConsentTrustee.setConsent({
  preferences: true,
  statistics: true,
  marketing: false
});

// Accept all
window.ConsentTrustee.setConsent({
  preferences: true,
  statistics: true,
  marketing: true
});

// Reject all (necessary is always implicitly true)
window.ConsentTrustee.setConsent({
  preferences: false,
  statistics: false,
  marketing: false
});
ConsentTrustee.withdraw()

Withdraw all previously given consent. Clears the consent cookie, dispatches thect:consentWithdrawn event, and sends a withdraw event to the API.

Example

document.getElementById('withdraw-consent').onclick = () => {
  window.ConsentTrustee.withdraw();
};
ConsentTrustee.setLanguage(lang)

Change the banner language dynamically. If the banner is visible, it re-renders with the new language.

Parameters

lang (string) - Language code: en, de, fr, es, it, nl, pl, pt, uk

Example

// Integrate with your language switcher
function onLanguageChange(newLang) {
  window.ConsentTrustee.setLanguage(newLang);
}
ConsentTrustee.getLanguage()

Get the current active language of the consent banner.

Returns

"en" | "de" | "fr" | "es" | "it" | "nl" | "pl" | "pt" | "uk"

Dynamic Language Support

Consent Trustee automatically detects language changes on multi-language websites and updates the banner accordingly.

Language Detection Priority

  1. data-ct-lang attribute on the script tag (highest priority)
  2. Programmatic setLanguage() API call
  3. <html lang="..."> attribute (auto-detected via MutationObserver)
  4. Browser language (navigator.language)
  5. Default: English

Automatic Detection

When your website changes the <html lang> attribute, the banner automatically updates:

// Your existing language switcher
function switchToGerman() {
  document.documentElement.lang = 'de';
  // ConsentTrustee banner updates automatically!
}

Force Language via Script Attribute

<script
  src="https://cdn.consenttrustee.com/consenttrustee.js"
  data-domain="your-domain-slug"
  data-ct-lang="de"
  async>
</script>

Events

Consent Trustee dispatches custom events that you can listen to for integrating with your application.

Both events are dispatched on window. Theevent.detail field is the consent state object directly.

ct:consentChanged

Fired whenever the user changes their consent preferences (accept, reject, or custom selection).

window.addEventListener('ct:consentChanged', (event) => {
  const consent = event.detail;
  console.log('Categories:', consent);

  // React to consent changes
  if (consent.statistics) {
    enableAnalytics();
  } else {
    disableAnalytics();
  }
});

Event Detail

{
  necessary: true,
  preferences: boolean,
  statistics: boolean,
  marketing: boolean
}
ct:consentWithdrawn

Fired when ConsentTrustee.withdraw() is called.event.detail is the consent state that was in effect just before withdrawal.

window.addEventListener('ct:consentWithdrawn', (event) => {
  const previousConsent = event.detail;
  // Tear down anything you started under the previous consent
  disableAnalytics();
});

Script Blocking

Consent Trustee automatically manages script execution based on user consent. Here's how it works:

How Script Blocking Works

  1. Scripts with type="text/plain" are ignored by the browser
  2. Consent Trustee reads the data-ct-category attribute
  3. When consent is given, the script type is changed to text/javascript
  4. For external scripts, data-ct-src is moved to src
  5. The script then executes normally

Inline Scripts

<script type="text/plain" data-ct-category="statistics">
  // This code runs only after statistics consent
  gtag('config', 'GA_MEASUREMENT_ID');
</script>

External Scripts

<script
  type="text/plain"
  data-ct-category="marketing"
  data-ct-src="https://connect.facebook.net/en_US/fbevents.js">
</script>

IFrames

<iframe
  data-ct-category="marketing"
  data-ct-src="https://www.youtube.com/embed/VIDEO_ID"
  width="560"
  height="315">
</iframe>

React to consent from JavaScript

For code paths where the script-blocking attributes don't fit (e.g. dynamically imported modules), combine an initial getConsent() read with act:consentChanged listener:

function applyConsent(consent) {
  if (consent?.statistics) {
    initAnalytics();
  }
  if (consent?.marketing) {
    initMarketingPixels();
  }
}

// Initial read for returning visitors who already decided
applyConsent(window.ConsentTrustee?.getConsent());

// React to subsequent changes
window.addEventListener('ct:consentChanged', (event) => {
  applyConsent(event.detail);
});

Script Attributes

Configure Consent Trustee behavior using data attributes on the script tag.

AttributeRequiredDescription
data-domainYesYour domain slug from the dashboard
data-ct-langNoForce a specific language (en, de, fr, etc.)
data-apiNoOverride the API base URL (advanced — only needed for self-hosted setups)
<script
  src="https://cdn.consenttrustee.com/consenttrustee.js"
  data-domain="your-domain-slug"
  data-ct-lang="de"
  async>
</script>

Best Practices

Do:

  • Load the script as early as possible in <head>
  • Use the async attribute for non-blocking load
  • Block all non-essential scripts until consent
  • Provide a way to manage preferences in footer
  • Test in incognito mode regularly

Don't:

  • Load tracking scripts before Consent Trustee
  • Auto-accept consent without user interaction
  • Hide or make the reject button hard to find
  • Store consent preferences on your own server only
  • Ignore the necessary category for essential cookies