Track custom events

This guide will help you track custom events using the Sqreen SDK and automate security scenarios. Visit the security automation section to learn more.

The Sqreen SDK stores events locally in a queue until the next heartbeat. Every minute, it flushes tracked events to our servers. When your app exits, it flushes any remaining tracked events.

To complete this guide, you should have installed our library in your application. Follow the installation steps described here.

Tracking events

The SDK uses the track method to record your custom events.

Recording an event by naming it:

<?php
sqreen\track(event.name);
?>

Our SDK supports optional parameters, such as properties:

<?php
sqreen\track(event.name, ['properties' => ['foo' => 'bar']]);
?>

When creating automation playbooks using this event, you can use the properties to group events and apply conditions and detections.

Default properties

Out of the box, the Sqreen library collects some properties based on the HTTP request:

  • Client IP.
  • User agent.
  • Path requested.
  • Request HTTP verb.
  • HTTP parameters.

By default, the Sqreen SDK scrubs sensitive data from these properties. See PII scrubbing

Track method definition

<?php
sqreen\track(event_name, [options]);
?>

  • event_name is a string. This is the name of the event you're tracking.
  • options enables you to provide extra parameters. This is an object with the following fields:
    • properties: an object with arbitrary parameters to record custom event dimensions. This parameter is optional. You can provide up to 16 properties per event.
    • user_identifiers: user account which performed the event. This should be the same object provided to sqreen\identify, sqreen\auth_track or sqreen\signup_track method when used. This parameter is optional.
    • timestamp: a Date object. Use this if you want to manually set the event's timestamp. By default, Sqreen uses the current server time. This parameter is optional.
    • collect_body: a boolean. If true, Sqreen collects the value in req.body and links it to the event. The track method does not collect the requests HTTP body by default.
    • request: a request object. Sqreen should automatically populate this field with the current request. If Sqreen cannot find it, you need to add it manually.

User tracking

If you want to associate the event tracked with a user account, you can decide to either pass it to every track call or rely on the identify method to set it in the context of the current HTTP request.

Providing track with user identifiers overrides the identify value for the context of this event.

Block users

To block users you must implement the identify method.

Track events from the past

When getting started with Sqreen, it can be useful to import past events. This lets you start with an existing dataset and automate scenarios right away.

When tracking an event, using the optional timestamp parameter overrides the current server time.

<?php

$event_date = new DateTime('2018-03-15T14:42:00+01:00');
sqreen\track(event.name, [
    'properties' => ['foo' => 'bar'],
    'timestamp' => $event_date
]);
?>

Monitor events

Congratulations! You've set up the Sqreen SDK and tracked your first custom events.

Now, go to your dashboard and visit the Event Explorer to check the events are properly recorded by Sqreen.

Next, depending on your traffic and the frequency of the tracked events, you may want to wait a few hours or days to collect enough events to craft a playbook.

event explorer

Create a security automation playbook

Once you are ready to automate a scenario, go to your dashboard and visit the Playbooks section to start building an automation playbook.

Error handling

Things can sometimes go wrong. This section features the most frequent issues when using our SDK.

Events recording

If the Sqreen agent does not manage to flush events collected in the past minute to our servers, it keeps retrying. After some time, it drops the events to prevent Sqreen memory overhead growing and impacting your application's performance.