# Scripts for airgap.js (/docs/consent-management/reference/airgap/prefix-modules)

Admin Dashboard pages: /consent-manager/*

A **script** is custom JavaScript that Transcend builds into your airgap.js bundle. It runs at the top of the bundle, before airgap.js starts, on every page that loads the bundle.

<Callout type="info">
  Transcend installs scripts for you. You cannot add or edit a script in the Admin Dashboard. To request one, contact your Transcend representative.
</Callout>

## When to use a script [#when-to-use-a-script]

| You need to                                           | Example                                                        |
| ----------------------------------------------------- | -------------------------------------------------------------- |
| Run the same consent logic on every site              | One rule for all brands, with no extra script tag on each site |
| Run code before airgap.js starts                      | Set a load option or register a request override for all sites |
| Send consent to your own systems                      | Post each consent change to your own API                       |
| Apply your own rule when two consent records disagree | Compare the browser record with the account record after login |

If only one site needs the logic, a script tag placed before the airgap.js script tag does the same job. See [Airgap Pre-init Configuration](./api.mdx#airgap-pre-init-configuration).

## How a script reaches your sites [#how-a-script-reaches-your-sites]

1. Tell your Transcend representative what the script must do. You can send your own JavaScript or TypeScript as a starting point.
2. Transcend releases the script as a numbered version. Each version has a changelog entry.
3. Transcend attaches that version to your bundle. If the script takes configuration, Transcend sets its values at this step.
4. Publish the bundle. In the Admin Dashboard, select **Review Changes & Publish...**, pick the bundle type, and then select **Deploy Changes**. For bundles that use scripts, the review step has a **Scripts** tab. It lists the catalog, name, version, and **Config** of each script.
5. Every site that loads that bundle now runs the script. You do not change any script tag.

To test first, deploy to the Test bundle. See [Testing with Test Bundle](../../testing-and-troubleshooting/debugging-and-testing.mdx#testing-with-test-bundle).

A later change to the script follows the same steps. Transcend releases a new version, attaches it to your bundle, and you publish again.

### Names, versions, and configuration [#names-versions-and-configuration]

| Part    | What it means                                                                                                     |
| ------- | ----------------------------------------------------------------------------------------------------------------- |
| Catalog | A script is either general, which any organization can use, or specific to your organization.                     |
| Name    | Each script has a name. A bundle can carry more than one script.                                                  |
| Version | A bundle is pinned to one version of each script. A new release does not change your bundle until it is attached. |
| Config  | A script can define configuration. Transcend checks the values against the script schema when it attaches it.     |

## What a script can do [#what-a-script-can-do]

A script runs in the page, in the same file as airgap.js. Transcend wraps each script in its own function so its variables do not leak to the page.

| When                     | What the script can do                                                                                                                                      |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Before airgap.js starts  | Set [load options](./load-options.mdx), register request overrides and cookie overrides, and queue a callback for when airgap.js is ready.                  |
| After airgap.js is ready | Call the [airgap.js API](./api.mdx#airgapjs-api-reference): `airgap.getConsent()`, `airgap.setConsent()`, `airgap.sync()`, and `airgap.addEventListener()`. |

A script can listen for [`consent-change`](./api.mdx#consent-change-event), [`consent-resolution`](./api.mdx#consent-resolution-event), and [`sync`](./api.mdx#sync-event) like any other code on the page.

`airgap.setConsent()` needs proof that the change is allowed. See [Consent authorization](../../configuration/consent-ui/creating-your-own-ui.mdx#consent-authorization). A script can use a user interaction event or the `load` event of the airgap.js script tag.

## Example patterns Transcend can build [#example-patterns-transcend-can-build]

You do not author the released script yourself. These sketches show the kinds of logic Transcend can embed for you.

| Pattern                           | Behavior                                                                                                            |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| Consent webhook                   | On each `consent-change` event, POST the consent record to your API                                                 |
| Login-time consent reconciliation | On the airgap.js `load` event, fetch the account record, merge it with the browser record, then call `setConsent()` |

Classify any domains your script calls in your data flows so airgap.js allows those requests. See [Unknown Request Policy](./api.mdx#unknown-request-policy).

## Limits [#limits]

- **An error can stop airgap.js.** The script and airgap.js are one bundle. An error that Transcend does not catch at the top level stops the rest of the bundle.
- **The code is public.** Anyone can read the script in the bundle. Do not put secrets in a script or in its configuration.
- **The script adds to the bundle size.** Every visitor downloads it with airgap.js.
- **Each change needs a release.** Transcend releases the new version, and you publish the bundle.
