# Consent Metadata (/docs/consent-management/reference/consent-metadata)

Consent metadata is a JSON object that airgap.js keeps with a visitor's consent record. Use it for values that your own code needs next to consent, such as where a change came from.

## Set metadata [#set-metadata]

Call `airgap.setConsentMetadata(auth, metadata)`. The call needs [consent authorization](../configuration/consent-ui/creating-your-own-ui.mdx#consent-authorization), the same as `airgap.setConsent()`.

```javascript
saveButton.addEventListener('click', async (event) => {
  await airgap.setConsentMetadata(event, {
    ...airgap.getConsent().metadata,
    source: 'preference-center',
  });
});
```

The call replaces the whole metadata object. It does not merge keys. Spread the current metadata, as shown above, to keep the keys that you do not change.

| Argument    | Type                      | Default          | Description                                                                                  |
| ----------- | ------------------------- | ---------------- | -------------------------------------------------------------------------------------------- |
| `auth`      | `AirgapAuth`              | Required         | A user interaction event or a `load` event. See [`AirgapAuth`](./airgap/api.mdx#airgapauth). |
| `metadata`  | `object`, `false`, `null` | Required         | An object replaces the metadata. `false` clears it. `null` leaves it as it is.               |
| `autoSync`  | `boolean`                 | `true`           | When `true`, airgap.js runs a [sync](./sync.mdx) after it stores the metadata.               |
| `timestamp` | ISO 8601 `string`         | The current time | The [metadata timestamp](#the-metadata-timestamp) for this write.                            |

The call returns a promise. It resolves to `false` when the authorization is not valid, and to `true` in all other cases. `setConsentMetadata` does not fire a `consent-change` event.

You can also set metadata in the same call that sets consent:

```javascript
airgap.setConsent(
  event,
  { Advertising: false },
  { metadata: { ...airgap.getConsent().metadata, source: 'banner' } },
);
```

## Read metadata [#read-metadata]

`airgap.getConsent()` returns the metadata and its timestamp.

```javascript
const { metadata, metadataTimestamp } = airgap.getConsent();
```

`metadata` is `undefined` until your code or a sync sets it.

## The metadata timestamp [#the-metadata-timestamp]

The consent record keeps two timestamps. `timestamp` is the time of the last consent change. `metadataTimestamp` is the time of the last metadata change.

- Each write sets `metadataTimestamp` to the current time, unless you pass your own value.
- airgap.js applies a write only when its timestamp is later than the stored `metadataTimestamp`. It ignores an older write.
- The newer metadata object wins as a whole. airgap.js does not compare single keys.

## Reserved keys [#reserved-keys]

The top-level key `tcmp` is reserved for Transcend. airgap.js modules use it to sync their own data. Today it holds the IAB Transparency and Consent Framework (TCF) string at `metadata.tcmp.tcf.tcString`.

Do not write your own values under `tcmp`. Because each write replaces the whole object, a write that leaves out `tcmp` removes it. Spread the current metadata to keep it.

## Storage [#storage]

airgap.js stores metadata inside the consent record in `localStorage`. When cookie-based sync is on, the consent cookie holds the same record. See [storage keys](./privacy-and-security.mdx#airgapjs).

- **Keep metadata small.** A cookie holds about 4 KiB. If the consent record does not fit, airgap.js logs an error and does not save the record.
- **Set metadata after the visitor confirms consent, or in the same `setConsent` call.** airgap.js does not save an unconfirmed consent record, so metadata that you set earlier can be lost on the next page load.
- **Metadata is readable in the browser.** Do not store secrets in it.

## Where metadata syncs [#where-metadata-syncs]

| Sync method                                       | What happens to metadata                                                                                                                                                                                                   |
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Cookie-based sync](./sync.mdx#cookie-based-sync) | The consent cookie carries the metadata and its timestamp. Each subdomain of your `data-site` domain reads the cookie. A page takes the metadata from the cookie only when the cookie also holds the newer consent record. |
| [XDI sync](./sync.mdx#xdi-sync)                   | airgap.js sends the metadata to the XDI sync host. A page does not apply metadata that it gets back from the sync host.                                                                                                    |
| [Remote sync](./sync.mdx#remote-sync)             | airgap.js sends only the reserved `tcmp` key to the backend. Your own keys stay in the browser.                                                                                                                            |

When remote sync returns a record from the backend, airgap.js handles its metadata as follows:

- If the browser has no metadata, airgap.js stores the metadata from the backend.
- If the browser has metadata, airgap.js keeps it and takes only `tcmp` from the backend.

To store your own metadata values on your backend, send them yourself. See [Storing consent on your backend](../configuration/persist-consent.mdx#storing-consent-on-your-backend).

## Metadata and identifiers [#metadata-and-identifiers]

Metadata is not an identifier. The consent record in the browser holds no user identifiers, and airgap.js does not read identifiers from metadata.

To link consent to a user, call `airgap.sync({ auth: consentToken })` with a token that your backend signs. The token carries the identifiers of the user, and remote sync sends it with the consent record. See [Authenticate a user's consent](../../preference-management/storing-consent-preferences.mdx#authenticate-a-users-consent).

Client-side consent sync, over cookies or XDI, carries the consent record only. It does not carry the token or any identifier. airgap.js keeps the token for the current page only, so call `airgap.sync({ auth })` on each page where the user is logged in.
