Skip to main content
For your web team. baselayer.js is a small browser module that tells Baselayer what happens on your pages: page views, page exits, navigations and interactions. Baselayer uses it, with what your KYA SDK reports, to show you which visits are agents and what they did. The snippet never receives a verdict about a visitor and never changes your page. Human visits are never stored. See What baselayer.js Reports.

What You Need

Your Baselayer team also adds your site’s origins to the application’s allowed origins. A page on any other origin has its records refused. See Allowed Origins. A browser key is public: it ships in your page. Keep your API key on your server.

Install

baselayer.js is one ES module. Pick how to load it: Load it with import and call init once per page:
A classic script cannot use import or a top-level await, so it loads the module with a dynamic import() and waits on the promise:
The examples below are module scripts, where a top-level await works. A module is loaded once per page, so importing it again returns the same running snippet.

Options

A malformed option throws a TypeError. Calling init again with valid options replaces the running session. List any page you want left out in excludePaths. A pattern ending in /* matches the pages below a path but not the path itself, so /your/page/* covers /your/page/details and you list /your/page too.

Content Security Policy

A page with a Content Security Policy must allow:
  • script-src https://js.baselayer.com and https://<application id>.collect.baselayer.com;
  • connect-src https://<application id>.collect.baselayer.com https://api.baselayer.com.
If the policy blocks the detection script, the snippet carries on without it and token() resolves to null. baselayer.js leaves window alone. To let other scripts on your page call it, assign it yourself: window.baselayer = baselayer.

Forward the Token to Your Server

token() resolves to a short-lived detection token. Send it to your own server in the X-Baselayer-Token header, where the KYA SDK reads it:
token() waits at most 2 seconds. It resolves to null on a timeout, when the detection script fails or is blocked, with consent: false, or before init. Treat null as “no token” and carry on: the request is still decided on what your server sees.

Record a Page View Yourself

The snippet records page views, page exits and single-page-app route changes on its own. track("page_view") queues a page view when your app shows a new page without changing the URL. It returns true when the record was queued. Any other kind returns false.

Stop the Snippet

destroy() sends the current page’s exit, sends what is queued, stops the detection script and removes the snippet’s history hooks. A single-page app calls it when it leaves the pages the snippet serves.

Pin a Release

/v1/baselayer.js is always the current release, cached for 5 minutes. To pin one release, load /v1/<version>/baselayer.js, for example https://js.baselayer.com/v1/0.1.2/baselayer.js. Baselayer never republishes a pinned release, and it is cached for a year.

Test Against Staging

Baselayer’s staging environment serves its own build of the snippet. Use a staging application ID and a staging browser key, which your Baselayer team provides, and point both origins at staging:
Use staging values throughout: each build talks only to its own environment. If your page has a Content Security Policy, allow https://<staging application id>.collect.staging.live.baselayer.com, https://js.staging.baselayer.com and https://api.staging.baselayer.com in place of the production hosts.

Check It Works

Test browser visits on staging, with your staging application ID and browser key. Open a page with the snippet, then list your visits with your staging API key. A visit appears about a minute after its first record:
Requests your KYA SDK reports show up in production too, at https://api.baselayer.com with your production API key. Baselayer stores agent visits only, so on staging, test with an automated browser if your own visit does not appear. See Reading Agent Activity for what the response holds.