Developer docs

Installing the datakant tracking snippet

How to install the default third-party snippet, switch to the first-party snippet when the server-side tracking add-on is active, and switch back after canceling.

v3.0Updated May 2026

Overview

By default, datakant ships with a small JavaScript snippet that sends every tracked event to the shared datakant ingest endpoint at https://datakant.ai. This is the third-party snippet, and it works out of the box on any website — no DNS records to configure, no custom subdomains, no server-side setup. Drop it in and you are live.

The server-side tracking add-on adds a second option: a first-party snippet that points at a tracking subdomain on your own domain (for example track.your-domain.com). Events flow through your domain instead of datakant.ai, which sidesteps third-party ad blockers and keeps the request origin first-party. The trade-off is one CNAME record in your DNS plus this docs page in your bookmarks.

Pick one variant, not both

Only one of the two snippets should be live on a given page at a time. Running both in parallel doubles every event and corrupts your numbers.

Install the default third-party snippet

Paste the snippet below inside the <head> of every page you want to track. You can add it through your CMS's head-injection setting, a tag manager (Google Tag Manager, Segment, Tealium), or directly in your site's HTML / template.

HTML
<!-- datakant Analytics -->
<script>
  window.dataLayer = window.dataLayer || [];
  window.DATAKANT_TRACKING_ID = 'YOUR_TRACKING_ID';
</script>
<script src="https://datakant.ai/datakant-tracker.js" async></script>
<!-- End datakant Analytics -->
Replace YOUR_TRACKING_ID

Swap {placeholder} for the property tracking ID shown in your datakant dashboard under YOUR_TRACKING_ID Tracking code. It starts with dk_.

Verify events arrive

After the snippet is live, open the Realtime view in your datakant dashboard. A live session should appear within a few seconds of loading a page on your site. If nothing shows up, check the browser's DevTools Network tab and filter by ingest — you should see POST requests to /api/events/ingest returning 200.

When you enable the server-side / first-party tracking add-on

Subscribing to the server-side tracking add-on does not change anything on your site by itself. It unlocks a second snippet variant — the first-party snippet — that you copy from Dashboard › Tracking code after the DNS setup is complete.

The switchover is a three-step process:

  1. Set up your tracking subdomain

    In the dashboard, follow the server-side setup wizard. You will pick a subdomain (for example track.your-domain.com) and add a single CNAME record at your DNS provider that points at the datakant ingest endpoint. The wizard verifies the record once propagation completes — usually within minutes.

  2. Copy the first-party snippet

    Once the subdomain is verified, the Tracking code tab will show the first-party variant of the snippet. The only difference from the third-party snippet is the src attribute, which now points at your subdomain rather than datakant.ai.

  3. Replace the snippet on your site

    Remove the third-party snippet from your site and paste the first-party snippet in its place. Deploy. Once the new snippet is live on every page, ingest will flow through your own subdomain.

HTML — first-party
<!-- datakant Analytics (first-party) -->
<script>
  window.dataLayer = window.dataLayer || [];
  window.DATAKANT_TRACKING_ID = 'YOUR_TRACKING_ID';
</script>
<script src="https://track.your-domain.com/datakant-tracker.js" async></script>
<!-- End datakant Analytics -->
Don't install the first-party snippet before DNS is verified

Until your tracking subdomain resolves and the wizard confirms it, the first-party endpoint will not accept ingest. Keep the third-party snippet live until the dashboard marks the subdomain as verified, then switch. Do not run both snippets at the same time — that double-counts every event.

When you cancel the server-side / first-party tracking add-on

When you cancel the server-side tracking add-on, the first-party endpoint keeps accepting events through the end of your current billing period and then stops. To avoid losing data when that happens, switch your site back to the default third-party snippet before the period ends.

The switchback is the mirror image of the switchover:

  1. Copy the third-party snippet

    Grab the third-party snippet from Dashboard › Tracking code, or copy the example in the Install the third-party snippet section above. The tracking ID stays the same — only the src URL changes back to https://datakant.ai.

  2. Remove the first-party snippet from your site

    Delete the <script> block that points at your tracking subdomain. Paste the third-party snippet in its place and deploy.

  3. Verify the new snippet is live

    Open Realtime in the dashboard and confirm events keep arriving. In DevTools, ingest requests should now post to https://datakant.ai/api/events/ingest again.

Don't run both snippets at once

Same rule as before: only one snippet should be live on a given page at a time. If you leave the first-party snippet in place alongside the third-party snippet during the transition, every event is sent twice and your numbers double until you remove one.

What about the CNAME record?

You can leave the CNAME record in place. It will resolve to nothing useful once the add-on lapses, but it doesn't hurt to keep it — if you re-subscribe later, the subdomain is ready to go without a fresh DNS round-trip.