Skip to content

Commit

Permalink
rebase with new a/b testing doc BEFORE merging this
Browse files Browse the repository at this point in the history
  • Loading branch information
nnennandukwe committed Sep 27, 2024
1 parent c31f829 commit e37e453
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 45 deletions.
34 changes: 21 additions & 13 deletions website/docs/how-to/how-to-capture-impression-data.mdx
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
---
title: How to capture impression data
---
import ApiRequest from '@site/src/components/ApiRequest'
import VideoContent from '@site/src/components/VideoContent.jsx'

import ApiRequest from "@site/src/components/ApiRequest";
import VideoContent from "@site/src/components/VideoContent.jsx";

:::info Placeholders
Placeholders in the code samples below are delimited by angle brackets (i.e. `<placeholder-name>`). You will need to replace them with the values that are correct in _your_ situation for the code samples to run properly.
:::

Unleash allows you to gather [**impression data**](../reference/impression-data.md) from your feature flags, giving you complete visibility into who checked what flags and when. What you do with this data is entirely up to you, but a common use case is to send it off to an aggregation and analytics service such as [Posthog](https://posthog.com/) or [Google Analytics](https://analytics.google.com/), either just for monitoring purposes or to facilitate [A/B testing](../topics/a-b-testing.md).
Unleash allows you to gather [**impression data**](../reference/impression-data.md) from your feature flags, giving you complete visibility into who checked what flags and when. What you do with this data is entirely up to you, but a common use case is to send it off to an aggregation and analytics service such as [Posthog](https://posthog.com/) or [Google Analytics](https://analytics.google.com/), either just for monitoring purposes or to facilitate [A/B testing](../feature-flag-tutorials/use-cases/a-b-testing.md).

This guide will take you through everything you need to do in Unleash to facilitate such a workflow. It will show you how to send data to Posthog as an example sink, but the exact same principles will apply to any other service of the same kind.

<VideoContent videoUrls={["https://www.youtube.com/embed/bxYdeMb9ffw"]}/>
<VideoContent videoUrls={["https://www.youtube.com/embed/bxYdeMb9ffw"]} />

## Prerequisites

We will assume that you have the necessary details for your third-party service:

- **where to send the data to**. We'll refer to this in the code samples below as **`<sink-url>`**.
- **what format the data needs to be in**. This will determine how you transform the event data before you send it.
- **where to send the data to**. We'll refer to this in the code samples below as **`<sink-url>`**.
- **what format the data needs to be in**. This will determine how you transform the event data before you send it.

In addition, you'll need to have a flag to record impression data for and an [Unleash client SDK](../reference/sdks/index.md) that supports impression data. This guide will use the [JavaScript proxy SDK](/docs/generated/sdks/client-side/javascript-browser.md).

Expand All @@ -37,7 +38,12 @@ When you create a new feature flag via the UI, there's an option at the end of t

To create a feature flag with impression data enabled, set the `impressionData` option to `true` in the request payload, as seen below. For more options, check the [reference docs on creating features](/reference/api/legacy/unleash/admin/features-v2.md#create-toggle).

<ApiRequest verb="post" payload={{name: "<feature-flag-name>", impressionData: true}} url="api/admin/projects/<project-id>/features" title="Create a feature flag with impression data enabled."/>
<ApiRequest
verb="post"
payload={{ name: "<feature-flag-name>", impressionData: true }}
url="api/admin/projects/<project-id>/features"
title="Create a feature flag with impression data enabled."
/>

### Enabling impression data for existing feature flags {#step-1-existing-toggles}

Expand All @@ -47,8 +53,12 @@ To enable impression data for an existing flag, go to the "Settings" tab of that

To enable impression data for an existing flag, use the [API's flag patching functionality](/reference/api/legacy/unleash/admin/features-v2.md#patch-toggle):

<ApiRequest verb="patch" payload={[{op: "replace", path: "/impressionData", value: true}]} url="api/admin/projects/<project-id>/features/<feature-flag-name>" title="Enable impression data on an existing flag."/>

<ApiRequest
verb="patch"
payload={[{ op: "replace", path: "/impressionData", value: true }]}
url="api/admin/projects/<project-id>/features/<feature-flag-name>"
title="Enable impression data on an existing flag."
/>

## Step 2: Capture impression events in your client {#step-2}

Expand All @@ -58,18 +68,17 @@ The steps below will use extracts from said documentation.

After initializing the library, you'll probably also want to identify the current user, so that events can be correlated properly:

``` js title="Identify the user."
```js title="Identify the user."
posthog.identify(userId);
```

### Capture and transform the event

Attach an event listener to Unleash that listens for `"impression"` events. Inside the listener, transform the event data to the format you want and send it to your analytics service.

``` js title="Capture, transform, and send impression data."
```js title="Capture, transform, and send impression data."
// listen for impression events
unleash.on("impression", (event) => {

// capture and transform events
posthog.capture(event.eventType, {
...event.context,
Expand All @@ -78,7 +87,6 @@ unleash.on("impression", (event) => {
enabled: event.enabled,
variant: event.variant,
});

});
```

Expand Down
Loading

0 comments on commit e37e453

Please sign in to comment.