Custom Events

While the Userlens SDK automatically captures clicks and page views, you can also track custom events for specific user actions that matter to your business.

When to Use Custom Events

The SDK captures every click automatically—and you can turn those into named events directly in the Userlens UI. But sometimes you want to track things that aren't clicks:

  • Form submissions (after validation passes)

  • Feature usage (user enabled dark mode)

  • Business milestones (subscription upgraded, file exported)

  • State changes (cart updated, filter applied)

For these, use pushEvent().


Basic Usage

import { useUserlens } from 'userlens-analytics-sdk/react';

function CheckoutButton() {
  const { collector } = useUserlens();

  const handleCheckout = async () => {
    // Your checkout logic...
    await processPayment();

    // Track the event
    collector?.pushEvent({
      event: 'Checkout Completed',
      properties: {
        total: 99.99,
        items: 3,
        couponUsed: true,
      },
    });
  };

  return <button onClick={handleCheckout}>Complete Purchase</button>;
}

Event Structure

The SDK automatically adds to your properties:

Property
Example
Description

$ul_browser

"Chrome"

Browser name

$ul_browser_version

"120.0.0"

Browser version

$ul_os

"macOS"

Operating system

$ul_device_type

"Desktop"

Device type

$ul_page

"https://..."

Current URL

$ul_referrer

"https://..."

Referrer URL

$ul_timezone

"America/New_York"

User timezone

...and more

You don't need to add these—they're included automatically.


Naming Conventions

Good Event Names

Avoid These Patterns


Common Patterns

Track After Async Operations

Track Feature Usage

Track Pagination/Navigation


Handling Null Collector

The collector may be null briefly during initial render. Always use optional chaining:


Custom Events vs Auto-Captured Clicks

Use Auto-Captured Clicks
Use Custom Events

Button clicks, links

Form submissions

Navigation actions

API responses

UI interactions

Business milestones

Anything visible in the DOM

Calculated values

Remember: Auto-captured clicks can be turned into named events in the Userlens UI—no code changes needed. Use pushEvent() only for things that can't be captured from clicks.


Debugging

Enable debug mode to see events in the console:


Next Steps

Last updated