> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom conversion binary metrics

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains how to create a custom conversion binary metric in LaunchDarkly.

## About custom conversion binary metrics

Custom conversion binary metrics track how many contexts trigger an event. You can use this metric with any event you set up in your codebase.

For example, you can use custom conversion binary metrics to track:

* How many customers submitted a form
* How many customers made a successful payment
* How many customers encountered an error

<Warning>
  **Call track and flush when an end user is redirected**

  SDKs send events at regular intervals, such as every five seconds. If a browser redirects an end user to another page, any events that occurred between when the SDK last sent events and when the browser redirected are lost. To avoid this, call track and then flush when you know the browser redirects an end user to another page, such as on click. To learn more, read [Tracking custom events](/docs/sdk/features/events) and [Flushing events](/docs/sdk/features/flush).
</Warning>

### Example metric in SQL

Some customers find it helpful to think about LaunchDarkly metric calculations in terms of SQL statements. The following example shows how a custom conversion binary metric could be represented as a SQL expression, using example table and column names to represent metric components:

<CodeGroup>
  ```sql title="Custom conversion binary metric" lines wrap theme={null}
  WITH user_conversions AS (
     SELECT
        exposures.user_id,
        MAX(CASE WHEN events.user_id IS NOT NULL THEN 1 ELSE 0 END) AS converted
     FROM exposures
     LEFT JOIN events
        ON exposures.user_id = events.user_id
     GROUP BY exposures.user_id
  )
  SELECT AVG(converted) AS proportion_converted
  FROM user_conversions;
  ```
</CodeGroup>

In this example:

* `events` is a table that stores all of the metric events on which the metric is defined.
* `exposures` is a table that contains flag evaluation data collected during an experiment or guarded rollout that uses the metric.
* `user_id` is a table column that represents the context both for the metric analysis unit and for the randomization unit used in the experiment or guarded rollout. This example uses the "user" context kind, but a metric can use other context kinds, such as "account," "session," or "device."

## Metric definition

When you configure the metric definition for a custom conversion binary metric, you first select **Occurrence (Percent)** to define both the aggregation method and analysis method for the metric. You then select one or more context kinds for which you will count events.

Then, custom conversion binary metric definition options include:

* The analysis units to use for measuring the event. This can be one or more context kinds, such as "user," "device," or "request," that the metric can measure events from.
* Success criteria:
  * **higher is better**: choose this option for metrics measuring positive things like cart checkouts or sign-ups.
  * **lower is better**: choose this option for metrics measuring negative things like errors.

To learn more about the metric definition options, read [Metric components](/docs/home/metrics/components).

## Create custom conversion binary metrics

Custom conversion binary metrics require an event key from your application's code to track conversions for any event in your code. To add a custom conversion binary metric in LaunchDarkly, you must identify it with a code snippet embedded in your app.

This is an example of sending a `custom` event:

<CodeGroup>
  ```java title="Example event" lines wrap theme={null}
  client.track("Example event key", context);
  ```
</CodeGroup>

The area of your code you should put custom conversion binary metric information into, and the type of information you should include, varies based on which SDKs you use. Often, the information you should put in your code includes the event key, context object, data field, or numeric value fields.

<Warning>
  **Event keys and metric keys are different**

  Sending custom events to LaunchDarkly requires a unique event key. 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. To learn more, read [Tracking custom events](/docs/sdk/features/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](/docs/home/metrics/create-metrics).
</Warning>

<Card icon="rectangle-terminal" horizontal>
  Try it in your SDK: [Tracking custom events](/docs/sdk/features/events)
</Card>

To create a custom conversion binary metric:

1. Open the **Data** section and navigate to the **Metrics** list.
2. Click **Create metric**. The "Create metric" dialog appears.
3. If you use [warehouse native metrics](/docs/home/warehouse-native/metrics), you can select either **LaunchDarkly hosted** or **Warehouse native**.
   * Select **LaunchDarkly hosted** to measure events from LaunchDarkly SDKs.
   * Select **Warehouse native** to measure events from an external warehouse, such as Snowflake.
     * Select an existing **Metric data source** or click **+ Create** to create a new data source. To learn more, read [Metric data sources](/docs/home/warehouse-native/metric-data-sources).
4. Select an event kind of **Custom** if you chose the **LaunchDarkly hosted** event source.
5. Search for or enter an **Event key**.
   * A list of events your environment has recently received appears when you click into the **Event key** field. Begin typing an event key to view a list of events that match your search. Hover over an event from the list to view which environments the event appears in, which context kinds sent the latest event, and which SDKs the event is coming from.
6. Choose **Occurrence (Percent)** from the menu in the [Metric definition](#metric-definition) section. The window populates a full metric definition using default values.

<Frame caption="A custom conversion binary metric.">
  <img src="https://mintcdn.com/launchdarkly/A4UXoRTW8ATNw4yq/images/auto/metrics-event-kind-custom-conversion.auto.png?fit=max&auto=format&n=A4UXoRTW8ATNw4yq&q=85&s=a4d363b1c806f80590b941f8983e6e85" alt="A custom conversion binary metric." width="1220" height="966" data-path="images/auto/metrics-event-kind-custom-conversion.auto.png" />
</Frame>

7. Change options in the "Metric definition" drop-down menus as needed:
   * Change or select additional context kinds to use as the analysis units for the metric. Only context kinds marked as available for experiments and guarded rollouts appear as options. To learn more, read [Mark context kinds available for experiments](/docs/home/experimentation/randomization#mark-context-kinds-available-for-experiments).
   * Choose **higher is better** or **lower is better** to define success criteria for attached experiments or guarded rollouts.
8. Enter a metric **Name**.
9. (Optional) Add a **Description**.
10. (Optional) Add any **Tags**.
11. (Optional) Update the **Maintainer**.
12. Click **Create**.

You can connect the metric to an [experiment](/docs/home/experimentation/create) or [guarded rollout](/docs/home/releases/guarded-rollouts) to monitor the impact of a flag change.

<Card icon="https://mintcdn.com/launchdarkly/YIC2H8XW-fomhquw/assets/icons/openapi-logo.svg?fit=max&auto=format&n=YIC2H8XW-fomhquw&q=85&s=dd9578a9668a86e1a8b8c314921f204c" horizontal width="2500" height="2452" data-path="assets/icons/openapi-logo.svg">
  You can also use the REST API: [Create metric](/docs/api/metrics/create-metric)
</Card>
