Skip to content

Tracking user actions

track sends an event, such as a conversion, to the provider, for experiments and analytics.

val purchase: ZIO[FeatureFlags, FeatureFlagError, Unit] =
  val user    = EvaluationContext("user-42")
  val details = TrackingEventDetails(99.99).withAttribute("currency", "EUR")
  FeatureFlags.track("checkout-completed") *>     // event name only
    FeatureFlags.track("purchase", user, details) // with targeting context and details
Call Sends
track(name) the event name, with the merged context
track(name, context) the event name and a targeting context
track(name, details) the event name and TrackingEventDetails
track(name, context, details) all three

The provider receives the context levels merged as for an evaluation (see Evaluation context). Otherwise track is not like an evaluation: it runs no hooks, so context a before hook adds is not sent, and the evaluation timeout doesn't apply, so a slow provider makes track slow. TrackingEventDetails has an optional numeric value and attributes: TrackingEventDetails(value) or .withValue(value) sets the value, and .withAttribute(key, value) adds an attribute. A provider that does not support tracking ignores the call.

Errors

  • An attribute value the SDK cannot represent fails the call with FeatureFlagError.InvalidContext before anything is sent. Accepted values are the plain types withAttribute takes for a context, any AttributeValue, Options of these, and nested Lists and Maps. A null or None attribute is dropped.
  • A provider error fails the call with the matching FeatureFlagError.

With several providers

A multi-provider chain sends every track call to each child, in order, whatever the strategy. One throwing child does not starve the rest: the call fails with the first error and carries the others as suppressed.

Warning

A failed track may already have reached some providers, so retrying it can count the event twice.

Assert on tracking in a test

The instance does not keep tracked events: their merged contexts routinely hold personal data. Use the testkit's TestFeatureProvider.trackedEvents instead; it lists what the provider received, oldest first (the last 1000). See Testkit.

// In a test: what the provider received, oldest first, with the context the SDK merged
val received: ZIO[TestFeatureProvider, Nothing, List[String]] =
  ZIO.serviceWithZIO[TestFeatureProvider](_.trackedEvents).map(_.map { case (name, _, _) => name })