# Banners

**Purpose:** `SDK.banner` places a small ad inside an element (a "container") that your page owns, and removes it again. Banners belong on screens that stay open for a while (menus, game-over and shop screens), never over the playfield. See the [advertisement rules](https://docs.zokogames.com/requirements/advertisements).

A container is any element with an `id`. It must be in the DOM, visible and at least partly on screen when you make the request.

## At a glance

Every member of `SDK.banner`, with its signature and the SDK version that added it. The sections below describe each call's errors and how it behaves in local mode, for guests and offline.

| Signature | Resolves to | Can fail with | Since |
| --- | --- | --- | --- |
| `requestBanner(options: { id: string; width: number; height: number }): Promise<void>` | Nothing, once the portal accepted the banner. | `invalidArgument`, `missingId`, `invalidSize`, `videoAdPlaying`, `notCreated`, `notVisible`, `bannerCooldown`, `maxRefreshReached`, `bannersDisabled`, `other`, `timeout`, `sdkNotInitialized`, `sdkDisabled`. `invalidSize` also comes from the portal when the container sticks out over the top or left edge or its id is over 128 characters. Every code except `invalidArgument`, `missingId`, `sdkNotInitialized` and `sdkDisabled` carries `containerId`. | 1.0.0 |
| `requestResponsiveBanner(id: string): Promise<void>` | Nothing, once the portal accepted the banner. | `missingId`, `videoAdPlaying`, `notCreated`, `notVisible`, `noAvailableSizes`, `bannerCooldown`, `maxRefreshReached`, `bannersDisabled`, `invalidSize` (from the portal, see the limits), `other`, `timeout`, `sdkNotInitialized`, `sdkDisabled`. | 1.0.0 |
| `clearBanner(id: string): Promise<void>` | Nothing, also when no banner is shown in that container. | `missingId`, `timeout`, `sdkNotInitialized`, `sdkDisabled`. | 1.0.0 |
| `clearAllBanners(): Promise<void>` | Nothing. | `timeout`, `sdkNotInitialized`, `sdkDisabled`. | 1.0.0 |

The types `BannerOptions` (`id`, `width`, `height`) and `BannerRequest` are in the declaration file described in the SDK introduction.

## Lifecycle

```text
screen with a banner slot opens
  -> requestBanner / requestResponsiveBanner(id)     checks run in this order:
       id given -> size allowed (static only) -> no video ad playing -> container found and visible
       -> size fits (responsive only) -> refresh limits -> portal answers
  -> (banner stays while the container stays)
  -> clearBanner(id) / clearAllBanners()             when the player leaves the screen
```

A failed request leaves nothing on screen. Do not request a banner while a [video ad](https://docs.zokogames.com/sdk/ads) is on screen; wait for `adFinished` or `adError`.

## requestBanner

```ts
requestBanner(options: { id: string; width: number; height: number }): Promise<void>
```

Places a banner of an exact size, centred inside the container, and resolves with nothing once the portal has accepted it. `options` must be an object (`invalidArgument` otherwise). Allowed sizes:

| Static sizes (`width x height`) |
| --- |
| 728x90, 300x250, 320x50, 468x60, 320x100 |

```html
<div id="menu-banner" style="width:300px;height:250px"></div>
<script>
  SDK.banner.requestBanner({ id: "menu-banner", width: 300, height: 250 })
    .catch((e) => console.warn("banner:", e.code, e.containerId));
</script>
```

## requestResponsiveBanner

```ts
requestResponsiveBanner(id: string): Promise<void>
```

Measures the container and picks the largest size from this list that fits inside it:

| Responsive sizes |
| --- |
| 970x250, 970x90, 728x90, 468x60, 336x280, 300x600, 300x250, 250x250, 320x50, 160x600, 120x600 |

The banner is placed at the container's own position and size, so give the container explicit dimensions or a layout that resolves to a sensible box. If nothing fits you get `noAvailableSizes`.

```js
await SDK.banner.requestResponsiveBanner("sidebar");
```

It resolves with nothing. A non-string or empty `id` rejects with `missingId`.

## Clearing

```ts
clearBanner(id: string): Promise<void>
clearAllBanners(): Promise<void>
```

Remove banners when the player leaves the screen they sit on. `clearBanner` takes the container id (empty or non-string rejects with `missingId`); `clearAllBanners` takes nothing. Both resolve with nothing, and clearing an id that has no banner is harmless. Clearing does **not** reset the refresh timer or the refresh count: the limits below continue across clear and request cycles.

<!-- docs-sample: uses startLevel -->
```js
function leaveMenu() {
  SDK.banner.clearAllBanners();
  startLevel();
}
```

## Limits

- The same container can be requested again no sooner than **30 seconds** after its previous successful request, even if you cleared it in between. Earlier calls reject with `bannerCooldown`. Checked by the SDK before it asks the portal.
- A session may refresh banners at most **60 times** (a refresh is a request for a container that was already requested before). After that, `maxRefreshReached`.
- At most **3** banners can be live on the game page at once; this is the technical limit. The review rule is stricter: at most **two** banners on one screen (see the launch checklist). A request for a fourth container rejects with `maxRefreshReached` until you clear one; requesting a container that already shows a banner does not count as a new one. Checked by the portal.
- While a video ad is on screen banner requests reject with `videoAdPlaying`. Request the banner again after `adFinished`.
- The container `id` can be at most 128 characters, and the banner's top left corner must lie inside the game frame: a container that sticks out over the top or left edge of the page is answered by the portal with `invalidSize`, even when its size is allowed. A banner that is larger than the space left in the frame is cut to fit. Keep banner containers fully on screen.
- A request for a container that already shows a banner (not cleared) moves or resizes it. The SDK still counts it for the 30 second floor and for the 60 refreshes, so it is refused with `bannerCooldown` inside the first 30 seconds. Clear the banner and wait, or keep the container size fixed.
- The Preview checklist flags a banner requested while gameplay is active (call `gameplayStop()` first), a refresh inside 30 seconds, more than 60 refreshes and more than two banners on screen at once.

**Knowing whether banners are available.** There is no call that reports it up front. The first `requestBanner` or `requestResponsiveBanner` rejects with `bannersDisabled` when banners are not served (ads off for the game, an ad-free member, offline, a store app build). Keep the container collapsed (no reserved height) until the request resolves, and remove or keep it collapsed on `bannersDisabled` so the layout does not leave a hole.

## Error codes

Banner errors carry the `containerId` you passed, except `missingId` (there is none) and the general codes such as `invalidArgument`, `sdkNotInitialized` and `sdkDisabled`.

| Code | Meaning |
| --- | --- |
| `bannersDisabled` | Banners are not served: ads are off for this game (always the case in basic launch), banners are switched off, the player is an ad-free member, the player is offline, or the game runs inside a store app build. |
| `unfilled` | Listed for "no banner available"; the current portal does not send it. Handle it like any other error. |
| `missingId` | `id` is empty or not a string. |
| `notCreated` | No element with that `id` exists. |
| `notVisible` | The element has no size, is hidden (`display:none`, `visibility:hidden`, `opacity:0`) or is outside the viewport. |
| `noAvailableSizes` | Responsive request: the container is smaller than every supported size. |
| `invalidSize` | Static request: the size is not in the static list. Since SDK 1.6.3 the message lists the allowed sizes (`300x250 is not a supported banner size (allowed: ...)`). |
| `videoAdPlaying` | A video ad is playing. |
| `bannerCooldown` | Refresh interval below 30 seconds. |
| `maxRefreshReached` | One code for two different limits: the session refresh cap (60 refreshes) or three banners already live. The error `message` says which ("refresh limit for this session" for the cap), but that text is for logs and can change in any release: do not match on it. To branch, count the banners you have live yourself, and treat the rest as the session cap. |
| `other` | Anything else, including a transport failure. |

Errors from the portal that carry no container id get the id you passed. Because failures are plain objects, read `e.code` and `e.containerId` (see [errors](https://docs.zokogames.com/sdk/introduction#errors)).

Banners are never required for the game to work. On any error, keep the container empty or collapse it, and do not retry in a tight loop.

## In local mode, as a guest, offline, ad-blocked or ad-free

| Situation | What happens |
| --- | --- |
| Local mode | The banner is a dashed grey box labelled with its size and container id, appended to the container. The SDK-side checks (`missingId`, `notVisible`, `invalidSize`, `videoAdPlaying`, `bannerCooldown`, `maxRefreshReached`, `noAvailableSizes`) run too, but the box itself never fails to fill, `bannersDisabled` never happens and the limit of three live banners is not applied, so test error handling by temporarily hiding the container (`notVisible`) or passing a wrong size (`invalidSize`). |
| Basic launch | `bannersDisabled`: banners switch on only at full launch, together with ads. Build the handling now; the Preview tool runs your build with ads switched on, so real banners can be tested there. |
| Guest | Same as a signed-in player. |
| Offline | `bannersDisabled`. |
| Ad blocker | Banner requests are not refused for a blocker; the blocker may keep the ad itself from loading. Keep the container collapsible. |
| Ad-free member | `bannersDisabled`. |

**Common mistakes.** What the Preview checklist and the portal report most often:

| Mistake | Result | Fix |
| --- | --- | --- |
| The container is created, resized or shown after the request | `notCreated` or `notVisible` | Request after the container is in the DOM, visible and measured (for example after the screen's first layout). |
| The container has `display:none`, zero height or `opacity:0` | `notVisible` | Give it a real size and show it before the request. |
| A banner is placed over the playfield or while the game is running | The Preview checklist item for banners fails ("banner during gameplay") and review rejects it | Banners belong on menu, shop and result screens. Call `gameplayStop()` first. |
| The same banner is requested again on a timer of 10 or 20 seconds | `bannerCooldown`, and after 60 refreshes `maxRefreshReached` | Refresh at most every 30 seconds, and only while the screen stays open. |
| Three or more containers are requested | A third request works, a fourth is `maxRefreshReached`; review allows two | Keep at most two banners on a screen and clear the others. |
| A size that is not on the static list (for example `300x300`) | `invalidSize` | Use a listed size, or `requestResponsiveBanner`. |
| Width and height are passed as strings or are missing | `invalidSize` | Pass numbers: `{ id, width: 300, height: 250 }`. |
| The layout keeps a gap for the banner when the request fails | An empty hole in the menu | Collapse or hide the container on `bannersDisabled` and every other error. |
| The request is made while a video ad plays | `videoAdPlaying` | Request after `adFinished` or `adError`. |
| Banners are never cleared when the screen closes | The banner stays over the next screen, and refreshes still count | Call `clearBanner(id)` or `clearAllBanners()` when the player leaves. |

## Putting it together

<!-- docs-sample: uses startLevel -->
```js
async function showMenuBanner() {
  try {
    await SDK.banner.requestResponsiveBanner("menu-banner");
  } catch (e) {
    if (e.code === "bannersDisabled" || e.code === "noAvailableSizes") {
      const box = document.getElementById("menu-banner");
      if (box) box.hidden = true;
    } else {
      console.warn("banner:", e.code, e.containerId);
    }
  }
}

function leaveMenu() {
  SDK.banner.clearAllBanners().catch(() => {});
  startLevel();
}
```

## Next steps

- [Advertisement rules](https://docs.zokogames.com/requirements/advertisements)
- [Ad monetisation guide](https://docs.zokogames.com/guides/ad-monetisation)
