Adlens

Docs

Set up in one script tag

Install, first-party setup, custom events, what the script sends, how revenue is estimated, and the stats API.

Plain tag (two minutes) #

Add before </head> on every page, with the key from your site settings:

<script defer src="https://adlens.adlens-trial.workers.dev/s.js" data-site="YOUR_SITE_KEY"></script>

That is all a pageview needs. Visits, sources, countries, devices, scroll depth, time on page, popunder opens, player taps and the adblock share are collected by the same script.

Ad blockers that block analytics hosts can stop this form. For numbers that include those visitors (and an adblock share that means something), use the first-party form below.

First-party (counts visitors who block ads) #

Serve the script and the beacon from your own domain under a neutral path. Blockers see a request to your own site and leave it alone. Pick a path that is not used by anything else, here /a/.

The tag becomes:

<script defer src="/a/s.js" data-site="YOUR_SITE_KEY" data-api="/a/e"></script>

and your server forwards two paths:

Your pathForward to
/a/s.jshttps://adlens.adlens-trial.workers.dev/s.js
/a/ehttps://adlens.adlens-trial.workers.dev/e (POST, body unchanged)

Forward the visitor's User-Agent and keep the request's Origin. Country is read from the forwarding request, so the proxy sends the visitor's two-letter country in an x-adlens-country header; without it the proxy's own location is recorded. A Cloudflare Worker has it in request.cf.country (sample below).

Next.js (next.config.js)

async rewrites() {
  return [
    { source: "/a/s.js", destination: "https://adlens.adlens-trial.workers.dev/s.js" },
    { source: "/a/e", destination: "https://adlens.adlens-trial.workers.dev/e" },
  ];
}

Next forwards the agent and origin headers. Behind a host that is not Cloudflare (Heroku, Vercel), the country recorded is the host's region, not the visitor's; use the Worker form below when you need countries right, or put the site behind Cloudflare.

Cloudflare Worker (in front of your site)

if (url.pathname === "/a/s.js" || url.pathname === "/a/e") {
  const target = "https://adlens.adlens-trial.workers.dev" + url.pathname.slice(2);
  const headers = new Headers(request.headers);
  headers.set("x-adlens-country", request.cf?.country || "");
  return fetch(target, { method: request.method, headers, body: request.body });
}

nginx

location = /a/s.js { proxy_pass https://adlens.adlens-trial.workers.dev/s.js; proxy_ssl_server_name on; }
location = /a/e    { proxy_pass https://adlens.adlens-trial.workers.dev/e;    proxy_ssl_server_name on; }

Custom events #

adlens("signup", { plan: "pro" });

Calls made before the script loads are queued; add this line above the tag if you call it early:

<script>window.adlens=window.adlens||function(){(adlens.q=adlens.q||[]).push(arguments)}</script>

Single-page apps #

Nothing to add: the script records a new pageview on every history.pushState, replaceState and back or forward navigation to a new path.

Check the install #

Open your site, then the dashboard's Realtime card: the visit shows within a minute. Visits from localhost and from automated browsers are left out unless the tag has data-allow-localhost or data-allow-automation.

What is stored #

Per pageview: the path, the source site of a visit's first page, UTM tags, country, region and city, device type, OS and browser with versions, time on page, scroll depth, popup opens, iframe taps, web vitals and whether the adblock test was blocked. No cookies and no IP address are stored. To tell visits apart, the server combines the IP address and browser with a secret that changes every day, keeps only the hash, and deletes each day's secret after two days, so a hash cannot be tied back to an address, or to the same person on another day. Raw events are kept 400 days in the exact store (3 months in Analytics Engine) and daily totals after that.

Crawler report (optional, from your own Worker) #

Crawlers never run the script, so send the server's hits from the Worker in front of your site. With an API key (Settings) in the Worker secret ADLENS_KEY and your site id in ADLENS_SITE:

const buf = [];
let last = Date.now();
export default {
  async fetch(request, env, ctx) {
    const res = await fetch(request); // your site as it is today
    buf.push({ path: new URL(request.url).pathname, ua: request.headers.get("user-agent") || "", status: res.status });
    if (buf.length >= 100 || Date.now() - last > 10_000) {
      const hits = buf.splice(0, 500);
      last = Date.now();
      ctx.waitUntil(fetch("https://adlens.adlens-trial.workers.dev/api/v1/hits", {
        method: "POST",
        headers: { authorization: `Bearer ${env.ADLENS_KEY}`, "content-type": "application/json" },
        body: JSON.stringify({ site: env.ADLENS_SITE, hits }),
      }));
    }
    return res;
  },
};

Each call counts once against the key's hourly limit (600), so at 100 hits a call that covers 60,000 requests an hour.

Tracker options #

Attributes on the script tag, or on your page's markup, switch these on or off:

WhereWhat it does
data-adlens-event="signup" on any elementA click sends that event, with properties from its data-adlens-prop-<key>="value" attributes. No JavaScript to write.
data-hash="on" on the tagCounts #/route changes as pageviews and keeps the hash in the path, for sites that route by hash.
data-404 on the tag in your 404 templateSends a 404 event with the path, so broken links show in Events.
data-auto="off" on the tagTurns off the automatic outbound, download and form_submit events, which are on by default.
data-errors="off" on the tagTurns off js_error events, which report up to 3 JavaScript errors per pageview.
<a href="/pricing" data-adlens-event="cta_click" data-adlens-prop-place="hero">See pricing</a>

Without JavaScript #

For visitors with JavaScript turned off, add this image inside <noscript> next to the tag. It counts a pageview with no time on page, scroll or ad fields; the referrer comes from the image request.

<noscript><img src="https://adlens.adlens-trial.workers.dev/e.gif?s=SITE_KEY&u=/path" alt=""></noscript>

Events from your server #

Send a pageview or an event from your own code with an API key, for example a purchase confirmed on the server. Pass the visitor's IP address and browser so country, device and unique visitors are right; without them your server's own are used. It answers 202, and each call counts toward the key's hourly limit.

curl -X POST "https://adlens.adlens-trial.workers.dev/api/v1/events" -H "Authorization: Bearer adl_YOUR_KEY" -H "content-type: application/json" \
  -d '{"site":"SITE_ID","name":"purchase","url":"/checkout/done","props":{"plan":"pro"},"ip":"VISITOR_IP","user_agent":"VISITOR_USER_AGENT"}'

Revenue estimates #

Revenue screens show what each page, country and device earns. Until you connect an ad network, the numbers are estimates built from ad network bid rates per 1,000 impressions, by country, operating system and ad format.

  • In site settings, pick the formats you run (pop, in-page, native) and a rate level (low, mid, high).
  • A popunder is counted each time the script sees one open. In-page and native ads estimate $0, since the script cannot see them render.
  • EPMV is estimated revenue per 1,000 pageviews.
  • Every API answer with revenue says "revenue_source": "estimate", and the dashboard labels it.

Ad revenue is an estimate from bid rates. Importing real earnings from an ad network is not offered yet.

Stats API #

Make a key in Settings, then send it as a Bearer token. Each key can make 600 requests an hour. Limits, filters and errors are in the Stats API guide.

curl -H "Authorization: Bearer adl_YOUR_KEY" "https://adlens.adlens-trial.workers.dev/api/v1/stats/SITE_ID/overview?period=30d"
ReportAnswers
overviewVisitors, pageviews, duration, scroll, revenue, EPMV, share of visitors with ad blockers, tier-1 share, pops and taps per visit, the previous period, a daily series
pagesRows per path
countriesRows per country with its tier
devicesRows per device; ?by=os or ?by=browser regroup
sourcesRows per referrer host; ?by=utm_source, utm_medium or utm_campaign regroup
eventsCustom events with counts and property breakdowns
adsPops and taps per visit, revenue and RPM per format
realtimeVisitors in the last 5 minutes, pageviews in the last 30
searchSearch Console clicks next to revenue, once connected

Query parameters

  • period: today, 7d, 30d, 90d or custom with from and to as YYYY-MM-DD (UTC).
  • Filters, combined: page, country (two letters, lower case), device (mobile, tablet, desktop), os, browser, source.

Answers

Money is US dollars with two decimals, shares run from 0 to 1, durations are seconds. Row reports are sorted by pageviews, return at most 100 rows and give total_rows. Errors come back as {"error": "code", "message": "text"} with a 4xx or 5xx status. A public dashboard serves the same reports at /api/public/SLUG/REPORT.

Questions: hello@adlens.example