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:
import or a top-level await, so it loads the module with a dynamic import() and waits on the promise:
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.comandhttps://<application id>.collect.baselayer.com;connect-src https://<application id>.collect.baselayer.com https://api.baselayer.com.
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: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: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.