Callbacks

Learn how to use callbacks to respond to consent management events in both JavaScript and React applications.

Overview

Callbacks allow you to execute custom code in response to various consent management events. They provide a way to integrate consent management with your application's logic, analytics, and other features.

Available Callbacks

The following callbacks are available your the c15t config.

Callback Reference

  • onError: Called when an API request fails.
  • onConsentBannerFetched: Called after successfully fetching the consent banner information.
  • onConsentSet: Called after successfully setting consent preferences.
  • onConsentVerified: Called after successfully verifying consent.

Offline Mode Callbacks

Some callbacks require a call to an endpoint meaning they will not work fully in offline mode.

  • onConsentBannerFetched: Will always return a simulated response of true, unless consent is in local storage.
  • onConsentSet: Stores consent in local storage.
  • onConsentVerified: Will always return a simulated response of true, unless consent is in local storage.

Usage Examples

import { configureConsentManager, type ConsentManagerOptions } from 'c15t';
 
const c15tConfig: ConsentManagerOptions = {
  // ...
  callbacks: {
    onConsentSet: ({ data }) => {
      if (data.preferences.measurement) {
        // Add your scripts here to enable analytics
      }
    }
  }
};

Callback Response Types

Callback responses are always of type ResponseContext<T>.

PropTypeDefault
data
ResponseType | null
-
response
Response | null
-
error
{ message: string; status: number; code?: string | undefined; cause?: unknown; details?: Record<string, unknown> | null | undefined; } | null
-
ok
boolean
-

onConsentSet Data

The onConsentSet callback receives consent preference details:

PropTypeDefault
type
string
-
preferences
Record<string, boolean>
-
domain?
string
-

onConsentVerified Data

The onConsentVerified callback receives verification results:

PropTypeDefault
type
string
-
domain?
string
-
preferences
string[]
-
valid
boolean
-

onConsentBannerFetched Data

The onConsentBannerFetched callback receives banner information:

PropTypeDefault
showConsentBanner
boolean
-
jurisdiction
{ code: string; message: string; }
-
location?
{ countryCode: string | null; regionCode: string | null; }
-

On this page

c15t.com