Skip to content

PHP event tracking

Event tracking is on by default in the PHP SDK (events.enabled, environment variable RANETRACE_EVENTS_ENABLED). Use it to record things that matter to your business from anywhere in your application.

Using Laravel? See Event Tracking instead.

Tracking an event

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

The full signature:

$ranetrace->trackEvent(
    string $name,
    array $properties = [],
    int|string|null $userId = null,
    bool $validate = true,
);

Properties are free-form. They are sanitized for serialization and secret-scrubbed before they leave your host, so a value under a key like api_token is redacted rather than sent. Every string value is also checked on its own, whatever key it sits under: the password in a URL and a token whose prefix says it is a credential are redacted, as PHP error tracking lists.

Naming rules

An event name must be 3 to 50 characters, start with a letter, and contain only lowercase letters, digits and underscores. In other words, snake_case.

An invalid name is a mistake you can still fix, so it is the one thing in the capture path that fails loudly: trackEvent() throws an InvalidArgumentException before any capture work happens. Everything after that point is caught and dropped instead, because monitoring must never be the reason a checkout breaks.

If you have to preserve a name from an external system that breaks the convention, pass validate: false or use customUnsafe(). Reach for it rarely: unconventional names make the events dashboard harder to read for everyone after you.

Convenience methods

events() returns the event tracker, which has typed methods for the events most applications record:

$events = $ranetrace->events();

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

// Cart additions
$events->productAddedToCart(
    productId: 'PROD-123',
    productName: 'Widget',
    price: 29.99,
    quantity: 1,
    category: 'tools',
);

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

// Page views, recorded server side
$events->pageView(pageName: 'pricing');

// Any other event, name validated
$events->custom('newsletter_signup', ['source' => 'footer']);

// Same, but skipping name validation
$events->customUnsafe('Legacy.Signup');

Every method except custom() and customUnsafe() takes an array $additionalProperties = [] as its last argument for ad-hoc fields; those two take your properties as their second argument and end in int|string|null $userId = null. Additional properties are merged over the properties the method builds, so you can override any of them. The one exception is productAddedToCart()'s category, which is applied after the merge: the named argument is the more specific statement of intent, so it wins.

What each method fills in for you:

Method Event name Properties it sets
sale() sale order_id, total_amount, currency, products, product_count
productAddedToCart() product_added_to_cart product_id, product_name, price, quantity, total_value, category
userRegistered() user_registered none, your properties are sent as given
userLoggedIn() user_logged_in none, your properties are sent as given
pageView() page_view page_name
custom() your name none
customUnsafe() your name, unvalidated none

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-php 1.5.0 the SDK cuts such an event when it is captured, 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 SDK's internal log 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.5.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.

The privacy model

Every event carries the name, the properties, a timestamp, and the URL of the current request. The URL has its sensitive query parameters redacted, and under CLI there is no URL, so the field is empty.

From ranetrace/ranetrace-php 1.4.0 every event also carries an id, a random version 4 UUID the SDK makes when the event is captured. 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. So tracking the same event twice records it twice, however alike the two are. A version before 1.4.0 sends no id, and Ranetrace then records two events that are alike in every field within one second as one.

Two identifiers travel with an event, and both are one-way hashes:

  • user_agent_hash, an HMAC-SHA256 of the raw user agent. Null when the request carried none, as under CLI, so "no user agent" stays distinguishable from a hashed one.
  • session_id_hash, an HMAC-SHA256 of the client IP, the first 100 characters of the user agent, and today's date. Because the date is part of the input, this fingerprint rotates every day, and it cannot be joined across sites.

The raw IP address is never part of the payload. Neither is an email: the only user field an event carries is the id.

That id comes from the $userId argument when you pass one, and otherwise from the user_resolver you configured (see PHP error tracking). When there is neither, the event is recorded without a user. When the user_resolver throws, the event is still tracked, without a user, and the SDK's internal log holds one Ranetrace user resolver failed warning with the exception message.

The fingerprint salt

The hashes are salted per install. By default the salt is your API key, which every install already has and which never travels in a payload, so fingerprints are non-reversible out of the box.

Set an explicit salt when you want to rotate fingerprints independently of the key, for example after a key rotation that should not break event grouping:

RANETRACE_FINGERPRINT_SALT=a-long-random-string

Changing the salt changes every hash, so past and future events for the same visitor stop lining up. That is the point when you rotate deliberately, and worth knowing before you change it by accident.

Configuration

Environment variable Config key Default What it controls
RANETRACE_EVENTS_ENABLED events.enabled true Whether events are captured at all.
RANETRACE_EVENTS_TIMEOUT events.timeout 10 Seconds to wait for the API when a batch of events is sent.
RANETRACE_FINGERPRINT_SALT fingerprint_salt your API key Salt for the user agent and session hashes.

To turn event tracking off:

RANETRACE_EVENTS_ENABLED=false

Captured events reach the API on the next flush. See PHP installation.