Web SDK

One script, no dependencies. It finds your slots, fills them, and reports a view only when a person could have seen the ad.

Install

Add the script once. The app ID is on your app's page in the dashboard.

<script async src="https://fairfill.dev/sdk.js" data-app-id="ff_your_app_id"></script>

Banner and native slots

Put a div where an ad may appear and give the slot a name. The name shows up in your stats, so you can compare placements.

<!-- banner: one row, about 64px tall -->
<div data-ff-unit="below_board"></div>

<!-- native card: image on top, good between content -->
<div data-ff-unit="end_of_article" data-ff-format="native"></div>

Interstitials

A full screen ad for a natural pause, like the end of a level. Included in Pro and Studio. The promise resolves when the visitor closes it, or at once when there is nothing to show.

const result = await Fairfill.interstitial({
  unit: "level_end",
  closeDelay: 3, // seconds before Close is enabled
});
// { shown: true } or { shown: false, reason: "no_fill" }
startNextLevel();

Events

Each slot fires events that bubble, so one listener on the document is enough.

document.addEventListener("fairfill:filled", (e) => {
  console.log(e.detail.unit, "has an ad");
});
document.addEventListener("fairfill:empty", (e) => {
  console.log(e.detail.unit, "stayed empty:", e.detail.reason);
});
// also: fairfill:viewed, fairfill:click

React and single page apps

The SDK watches the page for new slots, so a component only has to render the div. To load a new ad in a slot that is already filled, call Fairfill.refresh(element).

export function AdSlot({ unit, format = "banner" }) {
  return <div data-ff-unit={unit} data-ff-format={format} />;
}

If you would rather load the script yourself, skip data-app-id and call Fairfill.init({ appId: "ff_your_app_id" }) once it has loaded.

Testing

On localhost every slot shows a sample ad, and nothing is counted. To see the sample on another domain, add data-test to the script tag. Automated browsers never receive ads, so end to end tests see empty slots.

How views are counted

Why a slot is empty

The fairfill:empty event carries one of these reasons.

ReasonWhat it means
no_fillNo campaign matches this visitor right now
frequency_capThis visitor reached the hourly or daily cap you set
wrong_domainThe page is not on the domain registered for the app
app_inactiveThe app is paused, or the account has no active plan
format_not_in_planInterstitials need Pro or Studio
automatedThe browser identified itself as automated
unknown_appThe app ID does not exist