> ## Documentation Index
> Fetch the complete documentation index at: https://docs.contentignite.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JS API

> Our tag has a number of public methods for setting data and triggering actions

<Warning>
  **Cloudflare Rocket Loader Warning**

  For any on page scripts using the Content Ignite JS API, it is important that you exclude the script from Rocket Loader (if used), which defers JS and in the case of the examples below, can prevent them from running altogether. Our JS API is a simple and fast way to queue the passing of information to our Publisher Tag, and as a result of the code not running immediately, has nil impact on page performance. [Read more](/getting-started/going-live#notes-on-caching-and-firewalls)
</Warning>

## Passing data to ID/Signal providers

```ts title="Definition" theme={null}
.passSignalData(type: 'email'|'phone', value: string)
```

When passing us PI for use by id/signal providers, ensure you pass it on every page load that the PI is available.

For email, the obvious one is on login and every page load while logged in, but a secondary important scenario is newsletter sign up! In this case, you are looking to pass the email address at every opportunity.

<Note>
  **Privacy**

  It is worth noting that any PI passed through using this method never reaches the Content Ignite servers. We do not store this data in any way, we simply act as the "pipes" to pass it to the ID vendors you've configured in Fusion, all handled client side.
</Note>

<span className="branded">
  ```js title="Example" theme={null}
  window.__ciads = window.__ciads || { cmd: [] };
  window.__ciads.cmd.push(() => {
    window.__ciads.passSignalData("email", "jamie@contentignite.com");
  });
  ```
</span>

## Setting targeting

```ts title="Definition" theme={null}
.setTargeting(key: string, values: string | string[])
```

This method allows you to pass custom page targeting directly to our tag, this could be data returned by your CMS for example.

<Warning>
  It is against our terms of service, and Googles, to pass in Personal
  Information via this feature, things such as email addresses, age, gender etc
  are forbidden.
</Warning>

```js title="Example" theme={null}
window.__ciads = window.__ciads || { cmd: [] };
window.__ciads.cmd.push(() => {
  window.__ciads.setTargeting("tags", ["news", "politics"]);
});
```

### Mergeing vs Overwriting vs deleting key-values

When you call `.setTargeting()` with a string value, it will merge that value with any existing values for that key. If you call it with an array value, it will overwrite any existing values for that key. To delete targeting values for a given key, call `.setTargeting()` with the value `NULL`.

### Config/Slot level targeting

```ts title="Definition" theme={null}
.setTargeting(key: string, values: string | string[], slotId: string)
```

The optional third parameter of `.setTargeting()` allows you to pass targeting for a specific slot, identified by either the config ID, unit type or parent container ID. If not passed, the targeting will be applied at the page level.

```js title="Example" theme={null}
window.__ciads = window.__ciads || { cmd: [] };
window.__ciads.cmd.push(() => {
  window.__ciads.setTargeting("tags", "news", "12345"); // targeting for config with id of 12345
  window.__ciads.setTargeting("tags", "news", "in-image"); // targeting for all in-image units
  window.__ciads.setTargeting("tags", "news", "div-gpt-ad-1611935295857-0"); // targeting for the unit with the parent container ID of 'div-gpt-ad-1611935295857-0'
});
```

### Targeting Macros

When passing targeting values, you can also use macros to pass dynamic values. The macros available are:

* `{CONFIG_ID}` The config ID as it is in Fusion; e.g. 1234
* `{INSTANCE_COUNT}` The instance count for repeating units; e.g. 1, 2, 3 etc
* `{UNIT_TYPE}` Unit type from Fusion; e.g. in-image, adhesion, iab etc

Simply include the macro in the value string and it will be replaced with the relevant value for each ad unit when the tag processes the targeting.

```js title="Example" theme={null}
window.__ciads = window.__ciads || { cmd: [] };
window.__ciads.cmd.push(() => {
  window.__ciads.setTargeting("pos", "ad-{INSTANCE_COUNT}"); // will pass pos: ad-1 for the first instance of a unit, pos: ad-2 for the second instance etc
});
```

### IAB Taxonomy (reserved keys)

We support two different IAB taxonomy types, which you can pass in via `.setTargeting()` by use of their reserved keys, these are:

* `IAB_AUDIENCE_1_1` - [IAB Specification](https://iabtechlab.com/standards/audience-taxonomy/), [List of Values](https://github.com/InteractiveAdvertisingBureau/Taxonomies/blob/develop/Audience%20Taxonomies/Audience%20Taxonomy%201.1.tsv)
* `IAB_CONTENT_2_2` - [IAB Specification](https://iabtechlab.com/standards/content-taxonomy/), [List of Values](https://github.com/InteractiveAdvertisingBureau/Taxonomies/blob/develop/Content%20Taxonomies/Content%20Taxonomy%202.2.tsv)

For example, to pass the content taxonomy of "Automotive", you'd call:

```js title="Example" theme={null}
window.__ciads = window.__ciads || { cmd: [] };
window.__ciads.cmd.push(() => {
  window.__ciads.setTargeting("IAB_CONTENT_2_2", [1]);
});
```

<Note>
  Note that if you enable contextual inside of Fusion for a given publisher,
  then the `IAB_CONTENT_2_2` taxonomy is already passed for you!
</Note>

## SPA reloads

```ts title="Definition" theme={null}
.reload()
```

Strictly for use in SPA's (Single Page Applications). This will cause a new page impression to register and all existing ads to be destroyed and re-built.

```js title="Example" theme={null}
window.__ciads = window.__ciads || { cmd: [] };
window.__ciads.cmd.push(() => {
  window.__ciads.reload();
});
```

## Tag Events

For custom development, there are several JavaScript events that are triggered throughout an ads life cycle. You can hook into these as needed to perform additional follow-up actions.

These are standard JavaScript events, available on the window the tag was deployed into.

For example, for a direct tag-on-page deployment, you can listen to an event with the following:

```js title="Example" theme={null}
window.addEventListener("ci_al", function (e) {
  console.log(e.detail);
});
```

Depending on the event, a `detail` object is returned with the following optional field:

* `detail.config_id` - STRING - The id of the ad unit the event was triggered from
* `detail.msg` - STRING - Further details of the event

The following events are available:

| **Event Name**          | **Description**                                                                                                                                                                                                                                |
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Audience Push `ci_ap`   | Fired when an audience is matched and will be pushed into Ad Manager. Fired once per ad unit.                                                                                                                                                  |
| Ad Impression `ci_ai`   | Fired when an ad has had a viewable impression (50% of the ad’s pixels are visible in the browser window for a continuous 1 second.)                                                                                                           |
| Page Impression `ci_pi` | Fired once for each page once the tag has loaded                                                                                                                                                                                               |
| Ad Unit Load `ci_aul`   | Fired once an ad unit has been loaded. Once user consent has been received (if applicable) and an ad has been served. For in-view units, this will be once the in-view event was detected.                                                     |
| Ad Load `ci_al`         | Fired once an ad unit has been loaded. Once user consent has been received (if applicable) and an ad has been served. For in-view units, this will be once the in-view event was detected. For refresh units, this will fire once per refresh. |
| Ad Miss `ci_am`         | Fired if an ad unit did not receive an ad (either ad\_load or ad\_miss will be triggered for each ad and any subsequent refreshes)                                                                                                             |
| Consent Denied `ci_cd`  | Fired when user consent is required but was not given                                                                                                                                                                                          |
| Tag Error `ci_te`       | Fired on an unexpected tag error                                                                                                                                                                                                               |
