Script Loader

Gate third-party scripts behind consent - load Google Analytics, Meta Pixel, and other tracking scripts only when users grant permission.

The script loader manages third-party scripts based on consent state. Scripts are defined in the provider's scripts option and are automatically loaded when their required consent category is granted, and unloaded when consent is revoked.

c15t has a collection of premade scripts available on the @c15t/scripts package. It's recomended to check if a pre-built integration exists before manually creating a script, see the integrations overview.

Info

We recommend using the pre-built integrations when possible.

Basic Usage

Pass an array of Script objects to the provider:

Script Types

Standard Scripts

Load an external JavaScript file via a <script> tag. Use src to specify the URL.

Inline Scripts

Execute inline JavaScript code. Use textContent instead of src:

Callback-Only Scripts

Don't inject any <script> tag - just execute callbacks based on consent changes. Useful for controlling libraries that are already loaded:

The category field accepts a HasCondition - either a simple string or a logical expression:

Script Callbacks

Every script supports four lifecycle callbacks:

CallbackWhenUse Case
onBeforeLoadBefore the script tag is injectedSet up global variables
onLoadScript loaded successfullyInitialize the library
onErrorScript failed to loadLog error, load fallback
onConsentChangeConsent state changed (script already loaded)Toggle tracking on/off

Advanced Options

Always Load

Scripts that manage their own consent internally (like GTM in consent mode):

Persist After Revocation

Keep the script loaded even after consent is revoked (the page won't reload for this script):

Script Placement

Control where in the DOM the script is injected:

Ad Blocker Evasion

Script element IDs are anonymized by default to avoid ad blocker pattern matching:

Dynamic Script Management

Add, remove, or check scripts at runtime via useConsentManager():

API Reference

Loading…