Actually Tracking

Script Tag

The Actually Tracking Web SDK allows you to track user behavior on your website using a simple script tag. This guide provides instructions for installing and using the Web SDK in your project.

Installation

Just insert this snippet and replace YOUR_CLIENT_ID with your client id.

index.html
<script>
  window.op=window.op||function(){var n=[];return new Proxy(function(){arguments.length&&n.push([].slice.call(arguments))},{get:function(t,r){return"q"===r?n:function(){n.push([r].concat([].slice.call(arguments)))}} ,has:function(t,r){return"q"===r}}) }();
  window.op('init', {
    clientId: 'YOUR_CLIENT_ID',
    trackScreenViews: true,
    trackOutgoingLinks: true,
    trackAttributes: true,
  });
</script>
<script src="https://actuallytracking.com/op1.js" defer async></script>

One-tag install

You can also put the configuration on the script tag itself. Screen views, outgoing links and data-track attributes are on by default; set an attribute to "false" to turn one off.

index.html
<script
  defer
  src="https://actuallytracking.com/at1.js"
  data-client-id="YOUR_CLIENT_ID"
></script>

Supported attributes: data-client-id (required), data-api-url, data-track-screen-views, data-track-outgoing-links, data-track-attributes, data-track-hash-changes, data-ignore-localhost, data-ignore-iframes, data-disable-payments and data-debug. If the page also calls window.op('init', ...), that configuration wins.

To call window.op(...) before the script has loaded, keep the first line of the snippet above (the window.op=window.op||... queue) in your <head>.

Options

Common options
  • apiUrl - The url of the actuallytracking API or your self-hosted instance
  • clientId - The client id of your application
  • clientSecret - The client secret of your application (only required for server-side events)
  • filter - A function that will be called before sending an event. If it returns false, the event will not be sent
  • disabled - If true, the library will not send any events
Web options
  • trackScreenViews - If true, the library will automatically track screen views (default: false)
  • trackOutgoingLinks - If true, the library will automatically track outgoing links (default: false)
  • trackAttributes - If true, you can trigger events by using html attributes (<button type="button" data-track="your_event" />), and track sections as they scroll into view (<section data-track-scroll="viewed_pricing">) (default: false)
  • trackHashChanges - If true, hash changes (#about) count as screen views, for hash-routed apps (default: false)
  • ignoreLocalhost - If true, nothing is sent from localhost, *.localhost, 127.0.0.1 or file:// pages (default: false)
  • ignoreIframes - If true, nothing is sent when the page is loaded inside an iframe (default: false)
  • sessionReplay - Session replay configuration object (default: disabled). See session replay docs for full options.
    • enabled - Enable session replay recording (default: false)
    • maskAllInputs - Mask all input field values (default: true)
    • maskTextSelector - CSS selector for text elements to mask (default: [data-actuallytracking-replay-mask])
    • blockSelector - CSS selector for elements to replace with a placeholder (default: [data-actuallytracking-replay-block])
    • blockClass - Class name that blocks elements from being recorded
    • ignoreSelector - CSS selector for elements excluded from interaction tracking
    • flushIntervalMs - How often (ms) recorded events are sent to the server (default: 10000)
    • maxEventsPerChunk - Maximum events per payload chunk (default: 200)
    • maxPayloadBytes - Maximum payload size in bytes (default: 1048576)
    • scriptUrl - Custom URL for the replay script (script-tag builds only)

Usage

Tracking Events

You can track events with two different methods: by calling the window.op('track') directly or by adding data-track attributes to your HTML elements.

index.html
<button type="button" onclick="window.op('track', 'my_event', { foo: 'bar' })">
  Track event
</button>
index.html
<button type="button" data-track="my_event" data-foo="bar">Track event</button>

Tracking scroll depth

Add data-track-scroll to any element to send an event the first time it scrolls into view. Requires trackAttributes.

index.html
<section data-track-scroll="viewed_pricing">...</section>

<!-- 80% visible for 2 seconds, with a custom property -->
<section
  data-track-scroll="read_testimonials"
  data-track-scroll-threshold="0.8"
  data-track-scroll-delay="2000"
  data-section="testimonials"
>...</section>
  • data-track-scroll-threshold - How much of the element must be visible, from 0 to 1 (default: 0.5)
  • data-track-scroll-delay - How long, in milliseconds, it must stay visible (default: 0)
  • Other data-* attributes are sent as event properties, as with data-track.

Each element sends its event once per page load. Combine these events in a funnel to see where visitors stop scrolling.

Visits from links tagged with utm_source, ref, source or via (for example ?ref=twitter or ?via=newsletter) are attributed to that source automatically.

Excluding your own visits

Run this in the browser console on your site to stop that browser from being tracked there:

localStorage.actuallytracking_ignore = 'true';

Remove it with delete localStorage.actuallytracking_ignore.

By default no cookies are set. Anonymous visitors get an id that changes every day, so someone who comes back tomorrow counts as a new visitor unless you identify them.

To recognise returning visitors across days, turn on Cookie mode in your project settings (Settings → Details → Measurement). The script then keeps the visitor id in a first-party at_vid cookie for a year, shared across your subdomains. Nothing changes in your snippet. If you turn cookie mode off again, visitors go back to being cookieless and the script removes the cookie on their next visit.

Cookie mode sets a cookie, so you will usually need to mention it in your cookie notice.

If a billing provider is connected (Settings → Revenue), the script ties checkouts to the visit without any server code:

  • Stripe: set the payment link's success URL to a page on your site with ?session_id={CHECKOUT_SESSION_ID}.
  • Polar: set the success URL to a page on your site with ?checkout_id={CHECKOUT_ID}.
  • Paddle: payment links that open on your own site (?_ptxn=txn_…) are picked up on their own.
  • Lemon Squeezy: when a visitor clicks a Lemon Squeezy checkout link on your site, the script adds their ids to it as custom checkout data.

The script reports a checkout as checkout_completed (or checkout_opened for Paddle), and the provider's payment is credited to the visit, source and campaign that led to it.

If you know the buyer's email on your site (a signup form, an account page, the checkout form), tell the script:

window.op('payment', { email: 'buyer@example.com' });

A Stripe payment by that email is then credited to this visit, even from a payment link with no success page. Only a SHA-256 hash of the lower-cased email is sent, never the email itself. Call it before or after checkout; a payment waits up to two minutes for the report.

To turn both off, add data-disable-payments="true" to the script tag or pass disablePayments: true to init.

Identifying Users

To identify a user, call the window.op('identify') method with a unique identifier.

main.js
window.op('identify', {
  profileId: '123', // Required
  firstName: 'Joe',
  lastName: 'Doe',
  email: 'joe@doe.com',
  properties: {
    tier: 'premium',
  },
});

Setting Global Properties

To set properties that will be sent with every event:

main.js
window.op('setGlobalProperties', {
  app_version: '1.0.2',
  environment: 'production',
});

Incrementing Properties

To increment a numeric property on a user profile.

  • value is the amount to increment the property by. If not provided, the property will be incremented by 1.
main.js
window.op('increment', {
  profileId: '1',
  property: 'visits',
  value: 1 // optional
});

Decrementing Properties

To decrement a numeric property on a user profile.

  • value is the amount to decrement the property by. If not provided, the property will be decremented by 1.
main.js
window.op('decrement', {
  profileId: '1',
  property: 'visits',
  value: 1 // optional
});

Clearing User Data

To clear the current user's data:

main.js
window.op('clear');

In cookie mode this also removes the at_vid cookie.

Advanced Usage

Filtering events

You can filter out events by adding a filter property to the init method.

Below is an example of how to disable tracking for users who have a disable_tracking item in their local storage.

main.js
window.op('init', {
  clientId: 'YOUR_CLIENT_ID',
  trackScreenViews: true,
  trackOutgoingLinks: true,
  trackAttributes: true,
  filter: () => localStorage.getItem('disable_tracking') === undefined,
});

Using the Web SDK with NPM

Step 1: Install the SDK

npm install @actually-tracking/web

Step 2: Initialize the SDK

op.js
import { ActuallyTracking } from '@actually-tracking/web';

const op = new ActuallyTracking({
  clientId: 'YOUR_CLIENT_ID',
  trackScreenViews: true,
  trackOutgoingLinks: true,
  trackAttributes: true,
});

Step 3: Use the SDK

main.js
import { op } from './op.js';

op.track('my_event', { foo: 'bar' });

Typescript

Getting ts errors when using the SDK? You can add a custom type definition file to your project.

Simple

Just paste this code in any of your .d.ts files.

op.d.ts
declare global {
  interface Window {
    op: {
      q?: string[][];
      (...args: [
        'init' | 'track' | 'identify' | 'setGlobalProperties' | 'increment' | 'decrement' | 'clear',
        ...any[]
      ]): void;
    };
  }
}

Strict typing (from sdk)

Step 1: Install the SDK
npm install @actually-tracking/web
Step 2: Create a type definition file

Create a op.d.tsfile and paste the following code:

op.d.ts
/// <reference types="@actually-tracking/web" />

On this page