Skip to content

Ad tag API ​

A web ad tag is one script. The id query is the ad unit id from the publisher console. Put the element where the ad should appear, or call show() for the formats that create their own element.

html
<script async src="https://koin-ad.click/banner/v1/main.js?id=UNIT_ID"></script>

Replace banner with the format, and UNIT_ID with the unit id.

FormatScriptElementGlobal object
Banner/banner/v1/main.js<x-banner>banner
Native/native/v1/main.js<x-native>native
Page push/page-push/v1/main.js<x-push>push
Pop-under/pop-under/v1/main.js<x-popunder>popunder

Load one script of a given format per page. A second script with the same global name replaces window.banner, window.native, window.push or window.popunder. Several elements may share the one script, and they all use that script's unit id.

The format pages are Banner, Native, Page push and Pop-under.

theme(css) ​

theme is on every format. It takes one CSS string and merges it into the format's default stylesheet.

js
banner.theme(`
  banner {
    background-color: red;
  }
`);

Call it after the script has run. An async script can use onload.

The CSS has to be valid. Invalid CSS is ignored, and the default stylesheet stays as it is.

Selectors use the short names listed on each format page, such as banner, container or card. Koin rewrites those names to the real element or ::part() selector. A name has to be a whole word in the selector. banner matches. my-banner does not.

Merge is by declaration, not by replacing the whole rule:

  • A property you set replaces the default value.
  • A property you leave out keeps the default value.
  • A selector or @media block that is not in the default stylesheet is added.
  • The same @media condition is merged the same way as a normal rule.

banner { background-color: red; } changes only the background. width, display, justify-content and the --ad-banner-* variables on that same rule stay.

close() ​

close() removes every ad element that this script has mounted. The element then leaves the page.

js
banner.close();

Page push still plays its leave animation before the element is removed, because that timer belongs to the element. close() on the global object removes the element immediately.

Element events ​

Each ad element dispatches these events on itself. They do not bubble.

EventWhen
mountedThe element is connected
loadingA request for an ad has started
successThe ad is ready
failureThe request or the creative failed. The element is removed
closeThe visitor or a timer closed it
destroyedThe element was disconnected
js
document.querySelector('x-banner').addEventListener('success', () => {});

failure removes the element, so listen before the request finishes.

lifecycle_event ​

Add lifecycle_event to the script URL to name a function already on window. Koin calls it twice:

typeWhen
initThe script has started
mountedThe format is mounted and its stylesheet is in the page
html
<script>
  window.onKoinAd = function (event) {
    console.log(event.type);
  };
</script>
<script
  async
  src="https://koin-ad.click/banner/v1/main.js?id=UNIT_ID&lifecycle_event=onKoinAd"
></script>

Define the function before the tag runs. This callback does not receive success or failure. Those stay on the element.

Traffic, monetized. · User guide for the Koin ad network