Leanbase Help Center

Your Guide to Building Products Customers Love

Home Complete guide to event tracking

Complete guide to event tracking

Event tracking is the first step toward improving your product. It enables you to understand how users interact with your app by capturing interaction and behavioral data — helping you discover what to improve and where.

At Leanbase, there are two ways to track events: autocapture and custom events.
This guide covers both — starting with how to set up autocapture, then diving into custom events, and finally refining them to collect exactly the data you need.


Setting up autocapture

Unlike many analytics tools, Leanbase doesn’t require you to pre-define or manually configure events. It can autocapture them for you — making it easy to start collecting data such as pageviews, clicks, and form submissions right away.

The fastest way to enable autocapture is to copy the snippet below into your site’s HTML inside the <head> tags.
Ideally, this should be a base or template page that loads on every view to ensure all user interactions are captured.

<script>
!function(t,e){var o,n,p,r;e.__SV||(window.leanbase=e,e._i=[],e.init=function(i,s,a){
function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){
t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script"))
.type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.leanbase.com","-assets.i.leanbase.com")+"/static/array.js",
(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);
var u=e;for(void 0!==a?u=e[a]=[]:a="leanbase",u.people=u.people||[],
u.toString=function(t){var e="leanbase";return"leanbase"!==a&&(e+="."+a),
t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},
o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys getNextSurveyStep onSessionId".split(" "),
n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.leanbase||[]);

leanbase.init('lbk_sPcFCKFdaIeyxkYtI2VqQfRR9eq4VBcQwaCBi9eBScO',{api_host:'https://us.i.leanbase.com', defaults:'2025-05-24'})
</script>

Alternative setup

Alternatively, you can install one of Leanbase’s SDKs — such as JavaScript Web, React, or React Native — for tighter integration.

Once configured, Leanbase automatically captures events like pageviews, clicks, input changes, and form submissions from HTML elements including button, form, input, select, textarea, and label.
These events flow directly into your Leanbase dashboard for analysis.

Leanbase autocapture can also collect non-event data like session duration, mouse movement, bounce rate, and performance metrics. You can learn more in the [Autocapture documentation].


Autocapture limitations

While autocapture is a great way to get started, it has a few limitations:

  • Lack of signal — Autocapture records everything, which can make it hard to know what matters most. Use filters, actions, and insights to focus on key metrics.

  • Frontend-only — Autocapture runs on the frontend, so it won’t capture backend or server-side events.

  • Limited customization — To capture specific data points or contexts, you’ll need to add custom event tracking.

  • SPA compatibility — Pageview tracking depends on page loads. For single-page apps, enable the capture_pageview configuration with history_change or use the default setting 2025-05-24.


Setting up custom events

Beyond autocapture, Leanbase supports custom events — allowing you to capture any event, anywhere in your product’s codebase.
This gives you full control over what data is collected and when.

1. Install the SDK

Choose and install the SDK for your language or framework. Leanbase provides SDKs for Node, Python, iOS, Android, and more. You can also use the Event Capture API directly.

Example for Python:

pip install leanbase

2. Configure Leanbase

Once installed, configure Leanbase with your project’s API key and host (found in your project settings):

import leanbase

leanbase.project_api_key = 'lbk_sPcFCKFdaIeyxkYtI2VqQfRR9eq4VBcQwaCBi9eBScO'
leanbase.host = 'https://us.i.leanbase.com'

3. Capture events

After setup, you can capture events anywhere in your codebase by calling the capture method with an event name, user distinct ID, and event properties:

def movie_played(movie):
    leanbase.capture(
        'distinct_id',
        'movie_played',
        {
            'movie_id': movie.id,
            'category': movie.category
        }
    )

Placing these calls strategically throughout your product ensures a consistent flow of event data into Leanbase.


Getting custom events right

Once your events are flowing, refine them to ensure you’re capturing accurate, actionable data.
Here’s what to focus on:

1. Identify users

Each event must include a user distinct ID.
Autocapture handles this automatically, but with custom events, you’ll define it yourself — often using a UUID, user ID, or email.

To connect an anonymous autocaptured ID with an identified user, use the identify method:

function loginRequest(user) {
  return authUser(user)
    .then(authorizedUser => {
      leanbase.identify(user.email)
    })
}

This lets you track users across sessions, devices, and create richer user profiles.


2. Properties

Event properties add valuable context — letting you segment users, filter events, and break down data more effectively.

leanbase.capture('event_name', {
  property1: 'value',
  property2: 'another value'
});

You can also define person properties to persist user-level data across events:

leanbase.capture('set_user_properties', {
  $set: { location: 'London' },
  $set_once: { referred_by: 'campaign_123' },
});

3. Group event tracking

Leanbase supports group analytics, allowing you to aggregate events and properties by entities such as companies, teams, or projects.
For example:

// Associate all subsequent events with company `id:5`
leanbase.group('company', 'id:5');
leanbase.capture('some_event');

This makes it easy to analyze activity at the organization or account level, not just per user.