Track custom events

{!sdk/tracking-intro.md!} here.

You can find a Go documentation reference of the SDK at https://godoc.org/github.com/sqreen/go-agent/sdk.

Tracking events

You use the TrackEvent() SDK method to record custom events.

To record an event, first name it:

sqreen := sdk.FromContext(ctx)
sqreen.TrackEvent("my.event")

The SDK supports optional parameters, such as properties:

props := sdk.EventPropertyMap{"key": "value"}
sqreen := sdk.FromContext(ctx)
sqreen.TrackEvent("my.event").WithProperties(props)

Later, when creating automation playbooks using this event, you can use these parameters to group events and apply conditions and detections.

{!sdk/tracking-default-properties.md!}

Method definition

This is the TrackEvent() method:

func (ctx *HTTPRequestRecord) TrackEvent(event string) *HTTPRequestEvent

TrackEvent() creates a new event, named event. You can set the event options using the returned value's methods. Event options allow you to provide additional parameters:

func (e *HTTPRequestEvent) WithProperties(p EventPropertyMap) *HTTPRequestEvent
func (e *HTTPRequestEvent) WithTimestamp(t time.Time) *HTTPRequestEvent
func (e *HTTPRequestEvent) WithUserIdentifiers(id EventUserIdentifiersMap) *HTTPRequestEvent
  • WithProperties(): pass an object with arbitrary parameters to record custom event dimensions.

  • WithUserIdentifiers(): identify the user account which performed the event. This should be the same object provided to ForUser() method when used.

  • WithTimestamp(): manually set the event’s timestamp. By default, Sqreen uses the current server time.

User tracking

To associate the event tracked with a user account, you can pass it to every TrackEvent() using WithUserIdentifiers() or rely on the ForUser() method to globally set it to the following method calls.

When TrackEvent() receives user identifiers, it overrides the identify value for the context of this event.

uid := sdk.EventUserIdentifiersMap{"uid": "my-uid"}
sdk.FromContext(ctx).ForUser(uid).TrackEvent("my.event")

{!sdk/tracking-next-steps.md!}