Skip to content

Event tracking

Event tracking is on by default (events.enabled = true). Use it to record custom events from anywhere in your application.

Privacy model

From Ranetrace::trackEvent(), each event ships with:

  • The event name and properties.
  • The authenticated user's ID (or an int|string $userId you pass explicitly).
  • A timestamp and the request's full URL.
  • user_agent_hash: the user agent hashed, not stored raw.
  • session_id_hash: the session ID hashed.
  • id: a random UUID the package makes when the event is tracked, from ranetrace/ranetrace-laravel 1.5.0. It says nothing about the visitor or the user. Ranetrace does not store it: it holds it for 30 minutes to tell an event that is sent twice from two events.

The IP address is not included in the event payload.

Because every event has an id of its own, tracking the same event twice records it twice, however alike the two are. A version before 1.5.0 sends no id, and Ranetrace then records two events that are alike in every field within one second as one.

Tracking a custom event

use Ranetrace\Laravel\Facades\Ranetrace;

Ranetrace::trackEvent('button_clicked', [
    'button_id' => 'header-cta',
    'page' => 'homepage',
]);

Full signature:

Ranetrace::trackEvent(
    string $eventName,
    array $properties = [],
    int|string|null $userId = null,
    bool $validate = true,
);

By default the event name is validated: it must be lowercase snake_case, start with a letter and be 3 to 50 characters long. A name that fails throws an InvalidArgumentException, even while event capture is off, so a bad name surfaces in development rather than in production. RanetraceEvents::custom() checks the same way. Pass validate: false to skip the check.

Convenience helpers

The RanetraceEvents facade exposes typed helpers (see the @method block on Ranetrace\Laravel\Facades\RanetraceEvents):

use Ranetrace\Laravel\Facades\RanetraceEvents;

// Sales
RanetraceEvents::sale(
    orderId: 'ORDER-456',
    totalAmount: 89.97,
    products: [
        ['id' => 'PROD-123', 'name' => 'Widget', 'price' => 29.99, 'quantity' => 3],
    ],
    currency: 'USD',
);

// Cart additions
RanetraceEvents::productAddedToCart(
    productId: 'PROD-123',
    productName: 'Widget',
    price: 29.99,
    quantity: 1,
);

// Authentication
RanetraceEvents::userRegistered(userId: $user->id);
RanetraceEvents::userLoggedIn(userId: $user->id);

// Page views (server-recorded)
RanetraceEvents::pageView(pageName: 'pricing');

// Generic custom event
RanetraceEvents::custom('newsletter_signup', ['source' => 'footer']);

Every helper except custom() accepts an array $additionalProperties = [] as its final argument for ad-hoc fields. custom() takes your properties as its second argument and ends in int|string|null $userId = null.

Size limits

The API refuses an event over 5,120 bytes JSON-encoded, one with a top-level text property over 500 characters, and one with a URL over 2,000 characters. From ranetrace/ranetrace-laravel 1.6.0 the package cuts such an event when it is tracked, so it arrives:

  • A top-level text property over 500 bytes is cut and ends in ... (truncated). Text nested deeper is left alone.
  • An event that is still over 5,120 bytes loses its largest top-level properties first, each replaced by a _truncated marker, until it fits. A sale with a long products list arrives with its order_id, total_amount, currency and product_count, and without products.
  • A URL over 2,000 characters is cut and ends in ... (truncated).

Each cut of a property writes a warning to the ranetrace_internal log channel that names the event and the properties. An event that does not fit even with all of its properties replaced is dropped, and that writes a warning too. A version before 1.6.0 sends the event as it is, and the API refuses one that is over a limit.

So keep event properties to identifiers and figures, and leave long text and long lists out.

Testing your setup

php artisan ranetrace:test-events

Configuration

From config/ranetrace.php:

'events' => [
    'enabled' => env('RANETRACE_EVENTS_ENABLED', true),
    'queue' => env('RANETRACE_EVENTS_QUEUE', true),
    'queue_name' => env('RANETRACE_EVENTS_QUEUE_NAME', null), // unset: the connection's default queue
    'timeout' => env('RANETRACE_EVENTS_TIMEOUT', 10),
],

To disable:

RANETRACE_EVENTS_ENABLED=false