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>- The slot is hidden until it is filled. With no ad it takes no space.
- The ad fills the width of its container. Give the container the width you want.
data-ff-theme="dark"or"light"fixes the colours. Without it the ad reads the background it sits on and picks the matching one.
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:clickReact 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
- The ad must be at least half inside the viewport for one continuous second, in a visible tab.
- Each ad carries a signed token that is valid for ten minutes and can be counted once.
- A banner or native view moves 1 credit from the advertiser to you. An interstitial moves 3.
- Clicks are tracked and shown in stats. They do not move credits, so nobody gains from fake clicks.
- You never see your own campaigns in your own apps.
Why a slot is empty
The fairfill:empty event carries one of these reasons.
| Reason | What it means |
|---|---|
no_fill | No campaign matches this visitor right now |
frequency_cap | This visitor reached the hourly or daily cap you set |
wrong_domain | The page is not on the domain registered for the app |
app_inactive | The app is paused, or the account has no active plan |
format_not_in_plan | Interstitials need Pro or Studio |
automated | The browser identified itself as automated |
unknown_app | The app ID does not exist |