Sending custom events

Overview

This topic explains how to send custom events using the track feature in each SDK. The track feature is available in server-side, client-side, and edge SDKs.

Tracking events

The track feature lets you record actions your customers take in your application as events. Events are specific to one LaunchDarkly environment and use an event key. You can send these events to LaunchDarkly metrics for use in experiments and guarded rollouts. To learn more, read Metrics.

Event keys and metric keys are different

Sending custom events to LaunchDarkly requires a unique event key. You can set the event key to anything you want. Adding this event key to your codebase lets your SDK track actions customers take in your app as events.

LaunchDarkly also automatically generates a metric key when you create a metric. You only use the metric key to identify the metric in API calls. To learn more, read Creating and managing metrics.

This is an example of a custom conversion binary metric with an event key of conversions-to-paid-accounts:

A custom conversion binary metric.

A custom conversion binary metric.

Evaluating flags, either with variation() or with allFlags(), produces analytics events which you can observe on your LaunchDarkly Live events page. The initial context you specify in the client constructor, as well as any context you specify with identify(), produces an analytics event which is how LaunchDarkly receives your context data. To learn more, read Live events.

To learn more about the different kinds of events SDKs send to LaunchDarkly, read Analytics events. To learn how to view a list of all of the metric events your SDKs are sending to LaunchDarkly, read Viewing incoming events.

Details about each SDK’s configuration are available in the SDK-specific sections below.

Client-side SDKs

This feature is available in the following client-side SDKs:

.NET (client-side)

The Track method lets you record custom events end users take in your application using an event key.

Here’s how:

C#
1client.Track("event-key-123abc");

You can also attach custom data to your event by passing an extra parameter to Track, using the LdValue class to represent any value that can be encoded in JSON. To learn how, read LdClient.Track.

Android

The track method lets you record custom events end users take in your application using an event key. You can also attach custom JSON data to your event by passing an extra JsonElement parameter to track.

Here’s how:

1client.track("event-key-123abc", data);

To learn more, read track.

You can also attach custom data to your event by calling TrackData or TrackMetric.

C++ (client-side)

The Track method lets you record custom events end users take in your application using an event key.

Here’s how:

1client.Track("event-key-123abc");

To learn more, read Track.

Electron

The track function lets you record custom events in your application with LaunchDarkly using an event key.

Here’s how:

JavaScript
1client.track('event-key-123abc', { customProperty: someValue });

Flutter

The track method lets you record custom events end users take in your application using an event key. You can also attach custom data to your event by passing an extra LDValue parameter or num parameter to track.

Here’s how:

1client.track('event-key-123abc', data: LDValue.objectBuilder().addBool("clicked-button", true).build());

To learn more, read track.

iOS

The track method lets you record custom events end users take in your application using an event key.

In the iOS SDK, track is a custom event added to the LDClient event store. A client app can set a tracking event to allow client customized data analysis. After an app has called track, the app cannot remove the event from the event store. LDClient periodically transmits events to LaunchDarkly based on the frequency set in LDConfig.eventFlushInterval. The LDClient must be started and online.

After the SDK’s event store is full, the SDK discards new events until the event store is cleared when it reports events to LaunchDarkly. Configure the size of the event store using LDConfig.eventCapacity. The first parameter, key, is the key for the event. The SDK does nothing with the key, which can be any string the client app sends. The second parameter, data, is the data for the event. The data parameter is optional. The SDK does nothing with the data, which can be any valid JSON item as an LDValue instance.

Optionally, you can add a metricValue parameter of type Double to track in Swift or as a required parameter to the overloaded track method in Objective-C.

1let data: LDValue = ["some-custom-key": "some-custom-value", "another-custom-key": 7]
2try LDClient.get()!.track(key: "event-key-123abc", data: data)

To learn more, read the generated API documentation for Swift or Objective-C.

JavaScript

The track method lets you record custom events end users take in your application using an event key.

To call track:

JavaScript
1client.track('event-key-123abc', { customProperty: someValue });

The second argument is optional. It assists with observational analytics for metrics or for Data Export destinations:

  • If you define clicked or tapped conversion or page viewed conversion metrics in LaunchDarkly, the SDK sends them automatically after you have initialized the client. You do not have to do anything else with the client to send click or page view events. The SDK will generate page view events correctly regardless of how the URL is changed, such as by the HTML5 history API or by changing the URL hash fragment. To learn more about metrics, read Metrics.
  • With Data Export, the second argument gives additional information without saving the data to the LaunchDarkly context. To learn more about Data export, read Data Export.

To learn how to attach custom data to your event with optional parameters, read track.

Single-page apps

The SDK automatically handles URL changes made by the HTML5 history API or by changing the URL hash fragment, and will trigger click and page view events correctly.

Do Not Track and ad blocking software

The JavaScript-based SDKs respect the Do Not Track events header. If an end user has Do Not Track enabled in their browser, the SDK does not send analytics events for flag evaluations or metrics to events.launchdarkly.com. To learn more, read Browser privacy settings block analytic events to LaunchDarkly. In addition, ad blocking software may block analytics events from being sent. This does not impact feature flag evaluations. To learn more about the events SDKs send to LaunchDarkly, read Analytics events.

Node.js (client-side)

The track method lets you record custom events end users take in your application using an event key.

Here’s an example:

JavaScript
1client.track('event-key-123abc');
2
3client.track('event-key-456def', { someData: 2 });

To learn how to attach custom data to your event with optional parameters, read track.

React Native

The track method lets you record custom events end users take in your application using an event key. You can also attach custom JSON data to your event by passing an extra parameter to track.

Availability

This parameter is available in v2.1.0 and later.

Optionally, you can add a metricValue parameter to the track method if you are using the latest version of Experimentation.

Here’s how:

JavaScript
1client.track('event-key-123abc', false);
2client.track('event-key-456def', { someData: 'value' });

To learn how to attach custom data to your event with optional parameters, read track.

Roku

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

BrightScript
1' without optional data
2launchDarkly.track("event-key-123abc")
3
4' with optional data
5launchDarkly.track("event-key-123abc", {"customField": 123})
6
7' with optional numeric metric
8launchDarkly.track("event-key-123abc", invalid, 52.3)

Server-side SDKs

This feature is available in the following server-side SDKs:

.NET (server-side)

The Track method lets you record custom events end users take in your application using an event key.

Here’s an example:

1client.Track("event-key-123abc", context);

You can also attach custom data to your event by passing an extra parameter to Track. To do this, use the LdValue type, which can contain any kind of data supported by JSON. You can also pass another parameter for a custom metric value.

Apex

The track method lets you record actions your users take in your you do application. You can also attach custom JSON data to your event by passing an LDValue parameter to track, or a custom metric value by passing a Double parameter.

Here’s how:

Apex
1client.track(user, 'event-key-123abc', 52.3, LDValue.of('my value'));

To learn how to attach custom data to your event with extra parameters, read Other methods.

C++ (server-side)

The Track method lets you record custom events end users take in your application using an event key. Optionally, you can include additional data associated with the event.

Here’s how:

1/* track the event */
2client.Track(context, "event-key-123abc");
3
4/* track the event and associate data with it */
5client.Track(context, "event-key-123abc", 42)

To learn more, read Track() in Client.

Erlang

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

ldclient:track(<<"event-key-123abc">>, #{key => <<"context-key-123abc">>}, #{data => <<"example">>})

To learn more, read track.

You can also attach a JSON object containing arbitrary data to your event, or a custom metric value. To learn how, read track_metric.

Go

The Track method lets you record custom events end users take in your application using an event key.

In this example, we use TrackEvent to send an event with the key event-key-123abc.

Here’s how:

1client.TrackEvent("event-key-123abc", context)

You can also attach custom data to your event by calling TrackData, which takes an extra parameter. Or, if you are using experimentation, you can specify a numeric metric with TrackMetric.

Haskell

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

1track client context "event-key-123abc" Nothing Nothing

You can also attach a JSON object containing arbitrary data to your event, or a custom metric value. To learn how, read track.

Java

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

1client.track("event-key-123abc", context);

You can also attach custom JSON data to your event with an alternate version of track, trackData. You can set the data to any value that can be represented in JSON. To learn more, read LDValue.

If you are using Experimentation, you can specify a numeric metric with trackMetric.

Lua

The track method lets you record custom events end users take in your application using an event key. Optionally, you can include additional data associated with the event.

Here’s how:

1client:track("event-key-123abc", context);

You can also attach an object containing arbitrary data to your event. To learn how, read track.

Node.js (server-side)

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

1client.track('event-key-123abc', context);

You can also attach custom data to your event with optional parameters. To learn how, read track.

PHP

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

1$client->track("event-key-123abc", $context);

You can also attach custom data, including anything that can be marshaled to JSON, to your event by passing an extra parameter to track. To learn how, read track().

Python

The track method lets you record actions end users take on your site. This lets you record events that take place on your server.

Here’s how:

1ldclient.get().track("event-key-123abc", context)

You can also attach custom data to your event with optional parameters. To learn how, read track.

Ruby

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

1client.track("event-key-123abc", context)

You can also attach an extra hash containing arbitrary data to your event. To learn how, read track.

Rust

The track method lets you record custom events end users take in your application using an event key.

In this example, we use track_event to send an event with the key event-key-123abc.

Here’s how:

1client.track_event(context, "event-key-123abc");

You can also attach custom data to your event by calling track_data, which takes an extra parameter. Or, if you are using experimentation, you can specify a numeric metric with track_metric.

Edge SDKs

Edge SDKs can be used with one of the LaunchDarkly client-side SDKs as follows:

  • The edge SDK gets all flags at the edge for a given context, and bootstraps them onto a cached payload
  • The client-side SDK initializes the bootstrapped payload
  • The client-side SDK evaluates the flags and sends events back to LaunchDarkly

Some edge SDKs support sending events directly. This means that using a client-side SDK is not necessary. This enables Experimentation and metrics use cases if you are using the edge SDK in conjunction with a server-side SDK. To learn more, read Experimentation and metric events.

You need to configure the edge SDK to enable sending events. To learn more, read Configuration.

This feature is available in the following edge SDKs:

Cloudflare

Sending events is available in Cloudflare SDK version 2.3.0 and later. You must explicitly enable sending events in your configuration.

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

Cloudflare SDK v2.3.0+ (TypeScript)
1client.track('event-key-123abc', context);

You can also attach custom data to your event with optional parameters.

Here’s how:

Cloudflare SDK v2.3.0+ (TypeScript)
1const exampleData = { customProperty: 'someValue' };
2const metricValue = 10;
3
4client.track('event-key-123abc', context, exampleData, metricValue);

When you are working with an edge SDK, you must also call flush after sending any events:

Cloudflare SDK v2.3.0+ (TypeScript)
1executionContext.waitUntil(
2 client.flush((err, res) => {
3 console.log(`flushed events result: ${res}, error: ${err}`);
4 }),
5);

To learn more, read Flushing events.

Vercel

Sending events is available in Vercel SDK version 1.2.0 and later. You must explicitly enable sending events in your configuration.

The track method lets you record custom events end users take in your application using an event key.

Here’s how:

Vercel SDK v1.2.0+ (TypeScript)
1client.track('event-key-123abc', context);

You can also attach custom data to your event with optional parameters.

Here’s how:

Vercel SDK v1.2.0+ (TypeScript)
1const exampleData = { customProperty: 'someValue' };
2const metricValue = 10;
3
4client.track('event-key-123abc', context, exampleData, metricValue);

When you are working with an edge SDK, you must also flush the events before your worker exits to ensure that they are sent back to LaunchDarkly. If you call flush inside the waitUntil method, then flushing events will not impact the handler’s response time. To learn more, read the Vercel documentation on waitUntil.

Here’s how:

Vercel SDK v1.2.0+ (TypeScript)
1context.waitUntil(
2 client.flush((err, res) => {
3 console.log(`flushed events result: ${res}, error: ${err}`);
4 }),
5);

To learn more, read Flushing events.

Built with