Sending custom events
Read time: 7 minutes
Last edited: Jun 17, 2022
Overview
This topic explains how to send custom
events using the track feature in your SDK. The track feature is available on both client-side and server-side SDKs.
Tracking events
The track feature lets you record actions your users take in your application as events. You can connect these events to metrics in experiments in the flag dashboard. To learn more about the events SDKs send to LaunchDarkly, read Analytics events.
You can view events in the debugger. To learn more, read The debugger.
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)
- Android
- C/C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
The Track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
client.Track("your-goal-key");
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
Expand Android code sample
The track
method lets you record actions your users take in your application. You can also attach custom JSON data to your event by passing an extra JsonElement
parameter to track
.
Here's how:
client.track("your-goal-key", data);
To learn more, read track
.
You can also attach custom data to your event by calling TrackData
or TrackMetric
.
C/C++ (client-side)
Expand C/C++ (client-side) code sample
The track
method allows you to record custom
events in your application with LaunchDarkly:
LDClientTrack(client, "YOUR_EVENT_KEY");
To learn more, read LDClientTrack
.
You can also attach custom data to your event by calling LDClientTrackData
or LDClientTrackMetric
.
Electron
Expand Electron code sample
Evaluating flags, either with variation()
or with allFlags()
, produces analytics events which you can observe on your LaunchDarkly Debugger page. The initial user you specify in the client constructor, as well as any user you specify with identify()
, produces an analytics event which is how LaunchDarkly receives your user data.
You can also explicitly send an event with any data you like using the track
function:
client.track('my-custom-event-key', { customProperty: someValue });
Flutter
Expand Flutter code sample
The track
method lets you record actions your users take in your you do application. You can have also attach custom JSON data to your event by passing an extra LDValue
parameter to track
.
Here's how:
await LDClient.track('your-goal-key', data: LDValue.objectBuilder().addBool("clicked-button", true).build());
You can also attach custom data to your event using the track method with optional parameters.
iOS
Expand iOS code sample
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.
let data: LDValue = ["some-custom-key": "some-custom-value", "another-custom-key": 7]try LDClient.get()!.track(key: "MY_TRACK_EVENT_NAME", data: data)
To learn more, read the generated API documentation for Swift or Objective-C.
JavaScript
Expand JavaScript code sample
You can record custom
events using the track
function. In LaunchDarkly, you can connect these events to metrics in experiments. To learn more, read About Experimentation.
Call track
with the event name to record custom conversion metrics.
The second argument is optional. It assists with observational analytics for Data Export destinations. With Data Export, the second argument gives additional context without saving the data to the LaunchDarkly user.
To call track
:
client.track('your-event-name', { customProperty: someValue });
To learn how to attach custom data to your event with optional parameters, read track
.
Tracking click and page view events in JavaScript
If you've defined click or page view goals in LaunchDarkly, they'll be sent automatically once the client has been initialized. You do not have to do anything else with the client to send click or page view goals.
To learn more about click and page view goals, read About Experimentation.
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.
Analytics in JavaScript
Evaluating flags, either with variation()
or with allFlags()
, produces analytics events which you can observe on your LaunchDarkly Debugger page. The initial user you specify in the client constructor, as well as any user you specify with identify()
, produces an analytics event which is how LaunchDarkly receives your user data.
You can also explicitly send an event with any data you like using the track
function. The second argument is optional. For example:
client.track('your-event-name', { customProperty: someValue });
If you define click or page view metrics in LaunchDarkly, they are sent automatically once the client has been initialized. You do not have to do anything else with the client to send click or page view goals. The SDK will generate page view
events correctly regardless of how the URL is changed, such as by the HTML5 history API, by changing the URL hash fragment, etc.
The JavaScript SDK respects 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 goals to events.launchdarkly.com. In addition, ad blocking software may block analytics events from being sent. This does not impact feature flag evaluations.
Node.js (client-side)
Expand Node.js (client-side) code sample
The track
method lets you record actions your users take, giving them any event name you want. This lets you record events that take place client-side. The current user data automatically accompany the event.
Here's an example:
client.track('something-happened');client.track('something-happened-with-custom-data', { someData: 2 });
To learn how to attach custom data to your event with optional parameters, read track
.
React Native
Expand React Native code sample
The track
method lets you record actions your users take in your app. You can also attach custom JSON data to your event by passing an extra parameter to track
.
Optionally, you can add a metricValue
parameter to the track
method if you are using the latest version of Experimentation.
Here's how:
client.track('YOUR_GOAL_FLAG_KEY', false);client.track('YOUR_GOAL_FLAG_KEY_WITH_DATA', {some_data: 'value'});
To learn how to attach custom data to your event with optional parameters, read track
.
Roku
Expand Roku code sample
The track
method lets you record custom
events in your application with LaunchDarkly.
Here's how:
REM without optional datalaunchDarkly.track("YOUR_EVENT_KEY")REM with optional datalaunchDarkly.track("YOUR_EVENT_KEY", {"customField": 123})REM with optional numeric metriclaunchDarkly.track("YOUR_EVENT_KEY", invalid, 52.3)
Server-side SDKs
This feature is available in the following server-side SDKs:
- .NET (server-side)
- Apex
- C/C++ (server-side)
- Erlang
- Go
- Haskell
- Java
- Lua
- Node.js (server-side)
- PHP
- Python
- Ruby
- Rust
.NET (server-side)
Expand .NET (server-side) code sample
The Track
method lets you record actions users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests.
Here's an example:
client.Track("your-goal-key", user);
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
Expand Apex code sample
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:
client.track(user, 'your-goal-key', 52.3, LDValue.of('my value'));
To learn how to attach custom data to your event with extra parameters, read Other methods.
C/C++ (server-side)
Expand C/C++ (server-side) code sample
The LDClientTrack
function lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
LDClientTrack(client, "your-goal-key", user, NULL);
To learn more, read LDClientTrack()
.
You can also attach custom data to your event by calling LDClientTrackMetric()
.
Erlang
Expand Erlang code sample
The track
function lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
ldclient:track(<<"YOUR-METRIC-KEY">>, #{key => <<"aa0ceb">>}, #{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
Expand Go code sample
The Track
methods allow you to record actions your users take on your site. This lets you record events that take place on your server.
In this example, we use TrackEvent
to send an event called completed-purchase
. This event can correspond to a metric with the same key.
Here's how:
client.TrackEvent("completed-purchase", user)
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
Expand Haskell code sample
The track
function lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
track client user "your-metric-key" 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
Expand Java code sample
The track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
client.track("your-goal-key", user);
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
Expand Lua code sample
The track
method lets you record actions your users take. This lets you record events that take place on your server.
Here's how:
client:track("your-goal-key", user);
You can also attach an object containing arbitrary data to your event. To learn how, read track
.
Node.js (server-side)
Expand Node.js (server-side) code sample
The track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
client.track('your-goal-key', user);
You can also attach custom data to your event with optional parameters. To learn how, read track.
PHP
Expand PHP code sample
The track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
$client->track("your-goal-key", user);
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
Expand Python code sample
The track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
ldclient.get().track("your-goal-key", user)
You can also attach custom data to your event with optional parameters. To learn how, read track
.
Ruby
Expand Ruby code sample
The track
method lets you record actions your users take on your site. This lets you record events that take place on your server.
Here's how:
client.track("your-goal-key", user)
You can also attach an extra hash containing arbitrary data to your event. To learn how, read track
.
Rust
Expand Rust code sample
The track
methods allow you to record actions your users take on your site. This lets you record events that take place on your server.
In this example, we use track_event
to send an event called completed-purchase
. This event can correspond to a metric with the same key.
Here's how:
client.track_event(user, "completed-purchase");
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
.