---
title: Script Loader
description: Load scripts based on consent. Used to load scripts that are not necessary for the user's consent, such as analytics scripts.
lastModified: 2025-09-19

availableIn:
  - framework: 'javascript'
    url: '/docs/frameworks/javascript/script-loader'
    title: 'JavaScript'
  - framework: 'next'
    url: '/docs/frameworks/next/script-loader'
    title: 'Next.js'
  - framework: 'react'
    url: '/docs/frameworks/react/script-loader'
    title: 'React'
---
## Overview

c15t provides the ability to load and unload scripts based on consent. This is useful for loading scripts that are not necessary for the user's consent, such as analytics scripts.

One common flaw of CMPs is they'll maintain a list of scripts that are blocked and then unblocked based on consent. This is a problem because it's not always accurate and can lead to scripts being missed or incorrectly blocked.

Our approach gives you, the developer, full control over the scripts that are loaded and unloaded, as no one knows your site better than you do.

> ℹ️ **Info:**
> When using the Script Loader, the tracking blocker will be disabled automatically (If enabled). This is because the tracking blocker is deprecated and may cause conflicts with the script loader. The current approach of the tracking blocker will only be available in v1.x of c15t, and will be removed in v2.0.

## Implementation

```ts
import { ConsentManagerProvider } from '@c15t/react';
import { googleTagManager } from '@c15t/scripts/google-tag-manager';

export function App({ children }: { children: React.ReactNode }) {
  return (
    <ConsentManagerProvider options={{
      scripts: [
        googleTagManager({
          id: 'GTM-XXXXXXX',
        }), // Prebuilt script or...
        {
          id: 'example',
          src: 'https://analytics.example.com/script.js',
          category: 'analytics',
        }
      ]
    }}>
      {children}
    </ConsentManagerProvider>
  );
}
```

## Prebuilt Scripts

A lot of scripts you may need to implement may be common, such as Google Tag Manager (GTM), PostHog, Meta Pixel, etc. To save you time, we have provided a set of prebuilt scripts for you to use.

These scripts are available in the `@c15t/scripts` package.

### All Prebuilt Scripts

| Script             | Guide                                          |
| ------------------ | ---------------------------------------------- |
| Google Tag Manager | [Guide](/docs/integrations/google-tag-manager) |
| Meta Pixel         | [Guide](/docs/integrations/meta-pixel)         |
| PostHog            | [Guide](/docs/integrations/posthog)            |
| TikTok Pixel       | [Guide](/docs/integrations/tiktok-pixel)       |
| LinkedIn Insights  | [Guide](/docs/integrations/linkedin-insights)  |
| Microsoft UET      | [Guide](/docs/integrations/microsoft-uet)      |
| X Pixel            | [Guide](/docs/integrations/x-pixel)            |

## Always Load Scripts

Some tracking scripts, like Google Tag Manager or PostHog, manage their own consent state internally. For these scripts, you'll want them to load immediately and never be unloaded, regardless of the consent state in c15t.

The `alwaysLoad` property allows you to bypass consent checks and ensure these scripts are always present on the page.

### When to Use Always Load

Use `alwaysLoad: true` for:

* **Tag Management Systems** (e.g., Google Tag Manager) that handle consent internally
* **Analytics Platforms** (e.g., PostHog) with built-in consent management & cookieless behavior
* **Scripts that must initialize early** and configure themselves based on consent

> ⚠️ **Warning:**
> When using alwaysLoad, you are responsible for ensuring the script respects user consent through its own consent management API. The script will load regardless of the user's consent choices in c15t.

### Behavior

Scripts with `alwaysLoad: true` have the following characteristics:

* **Load immediately** when the consent manager initializes, bypassing consent checks
* **Never unload** when consent is revoked or changed
* **Persist through** `clearAllScripts` calls
* **Still trigger callbacks** like `onBeforeLoad`, `onLoad`, and `onConsentChange`

## Types

### Script

| Property                   | Type                                              | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | Default |  Required  |
| :------------------------- | :------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ | :--------: |
| id                         | string                                            | Unique identifier for the script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | -       | ✅ Required |
| src                        | string \| undefined                               | URL of the script to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | -       |  Optional  |
| textContent                | string \| undefined                               | Inline JavaScript code to execute                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | -       |  Optional  |
| category                   | HasCondition\<AllConsentNames>                    | Consent category or condition required to load this script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       | ✅ Required |
| callbackOnly               | boolean \| undefined                              | Whether this is a callback-only script that doesn't need to load an external resource.&#xA;When true, no script tag will be added to the DOM, only callbacks will be executed.&#xA;&#xA;This is useful for:&#xA;- Managing consent for libraries already loaded on the page&#xA;- Enabling/disabling tracking features based on consent changes&#xA;- Running custom code when consent status changes without loading external scripts&#xA;&#xA;Example use cases:&#xA;- Enabling/disabling Posthog tracking&#xA;- Configuring Google Analytics consent mode&#xA;- Managing cookie consent for embedded content | false   |  Optional  |
| persistAfterConsentRevoked | boolean \| undefined                              | Whether the script should persist after consent is revoked.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | false   |  Optional  |
| alwaysLoad                 | boolean \| undefined                              | Whether the script should always load regardless of consent state.&#xA;&#xA;This is useful for scripts like Google Tag Manager or PostHog that manage&#xA;their own consent state internally. The script will load immediately and&#xA;never be unloaded based on consent changes.&#xA;&#xA;Note: When using this option, you are responsible for ensuring the script&#xA;itself respects user consent preferences through its own consent management.                                                                                                                                                          | false   |  Optional  |
| fetchPriority              | "high" \| "low" \| "auto" \| undefined            | Priority hint for browser resource loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | -       |  Optional  |
| attributes                 | Record\<string, string> \| undefined              | Additional attributes to add to the script element                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
| async                      | boolean \| undefined                              | Whether to use async loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | -       |  Optional  |
| defer                      | boolean \| undefined                              | Whether to defer script loading                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | -       |  Optional  |
| nonce                      | string \| undefined                               | Content Security Policy nonce                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| anonymizeId                | boolean \| undefined                              | Whether to use an anonymized ID for the script element, this helps ensure the script is not blocked by ad blockers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | true    |  Optional  |
| target                     | "head" \| "body" \| undefined                     | Where to inject the script element in the DOM.&#xA;- \`'head'\`: Scripts are appended to \`\<head>\` (default)&#xA;- \`'body'\`: Scripts are appended to \`\<body>\`&#xA;&#xA;Use \`'body'\` for scripts that:&#xA;- Need to manipulate DOM elements that don't exist until body loads&#xA;- Should load after page content for performance reasons&#xA;- Are required by third-party services to be in the body&#xA;&#xA;Use \`'head'\` (default) for scripts that:&#xA;- Need to track early page events (analytics)&#xA;- Should be available before page render&#xA;- Most tracking/analytics scripts       | 'head'  |  Optional  |
| onBeforeLoad               | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed before the script is loaded                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onLoad                     | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed when the script loads successfully                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | -       |  Optional  |
| onDelete                   | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed when the script is being unloaded/removed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | -       |  Optional  |
| onError                    | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed if the script fails to load                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | -       |  Optional  |
| onConsentChange            | ((info: ScriptCallbackInfo) => void) \| undefined | Callback executed whenever the consent store is changed.&#xA;This callback only applies to scripts already loaded.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | -       |  Optional  |
