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.
<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.
| Format | Script | Element | Global 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.
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
@mediablock that is not in the default stylesheet is added. - The same
@mediacondition 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.
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.
| Event | When |
|---|---|
mounted | The element is connected |
loading | A request for an ad has started |
success | The ad is ready |
failure | The request or the creative failed. The element is removed |
close | The visitor or a timer closed it |
destroyed | The element was disconnected |
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:
type | When |
|---|---|
init | The script has started |
mounted | The format is mounted and its stylesheet is in the page |
<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.