# Email AB Testing with SendGrid

Run email A/B tests with Statsig and SendGrid to find which email template performs best. You create an experiment in Statsig, connect it to SendGrid's event webhook, and split your recipients across control and test sends. Statsig tracks opens, clicks, and downstream conversion metrics for each variant so you can measure business-level impact.

{% callout type="info" %}
This guide assumes you have an existing Statsig account. Go here to create a new free account if you don't already have one: https://statsig.com/signup
{% /callout %}

## Step 1: Create an experiment

Start by creating a new experiment on the Statsig console. Enter a name and leave the remaining fields at their defaults.

{% figure %}
![Experiment creation interface](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210731384-8bbfc1e1-076c-4ae3-959a-ef1507801e71.png)
{% /figure %}

## Step 2: Start the experiment

Because you can't start an experiment without a parameter, add a placeholder parameter.

{% figure %}
![Experiment parameter configuration](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210316935-99f13616-c412-47c1-b9ab-99b90462805e.png)
{% /figure %}

{% figure %}
![Experiment setup completion](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210731687-0883c356-5cef-458d-a6aa-bfde9ad36f8b.png)
{% /figure %}

Save the experiment setup and select **Start**.

{% figure %}
![Experiment start confirmation](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210731969-decf481a-b6a2-41ee-b1c1-6b917bb18fab.png)
{% /figure %}

Copy the **Experiment Name**. You need it in the next step.

{% figure %}
![Experiment name copy interface](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210732298-34c0e1f4-6485-425b-af07-fa535ad86396.png)
{% /figure %}

## Step 3: Set up webhook

In your SendGrid console, go to **Settings** > **Mail Settings** > **Event Webhook**.

In the HTTP Post URL, enter:

`https://sendgrid-webhook.statsig.workers.dev/?apikey=[YOUR STATSIG API KEY]`

{% callout type="info" %}
You can find your API key by going to Statsig Project Settings > API Keys and copying the Client API Key.

{% figure %}
![Statsig API key location](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210319717-49ac4a21-8bdd-4348-b534-374b760ab047.png)
{% /figure %}

It should look like this: `client-abcd123efg...`
{% /callout %}

Make sure you select all the **Deliverability Data** and **Engagement Data** checkboxes. Enable the **Event Webhook Status** and select Save.

The setup should look like this:

{% figure %}
![SendGrid webhook configuration](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210737123-86725ff7-2432-4bde-95e6-173f8f194f86.png)
{% /figure %}

## Step 4: Create single sends

In your SendGrid app, create two new **Single Send** campaigns and name them using the experiment name.

The first is the Control, which is the baseline. Name it `[experiment_name]/control`. For example: `drip_campaign_ab_test/control`.

{% figure %}
![Control email template setup](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210733152-5766320a-ed30-45f7-b57f-bf0526f796a8.png)
{% /figure %}

The second is the Test, which is the template you're comparing with the baseline. Name it `[experiment_name]/test`. For example: `drip_campaign_ab_test/test`.

{% figure %}
![Test email template setup](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210733504-581ea181-3ebc-4bc1-a707-391866bcbf62.png)
{% /figure %}

You can customize these templates however you want, and even use different subjects.

Split the recipient list evenly between the Control and Test sends to keep the experiment balanced.

{% callout type="info" %}
To avoid bias, split the recipient list at random. Ensure you distribute recipients from the same company evenly between the two lists.
{% /callout %}

Send the emails. Statsig automatically tracks how each variant performs across email opens, clicks, and other engagement metrics.

## Monitoring the setup

After starting the sends, verify that everything is working by going to the Diagnostics tab in your experiment and reviewing the Exposure Stream at the bottom of the page. The Exposure Stream shows a real-time feed of page loads and the variant Statsig allocated to each user.

{% figure %}
![Experiment exposure stream in diagnostics tab](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210737715-6adf5fd9-3b23-433a-a852-f2c8ea93cf2a.png)
{% /figure %}

## Interpreting results

Go to the **Pulse Results** tab in the Experiment page to add metrics and determine which variant is performing better. Refer to [Reading Pulse Results](https://docs.statsig.com/experiments/interpreting-results/read-results) for details.

{% figure %}
![Pulse results tab showing experiment metrics](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210737928-3f33ca3b-a46b-40ac-b095-0b33fab66109.png)
{% /figure %}

## Using API instead of single send

Statsig also supports A/B testing when using the API or automation to send marketing emails. To enable this, use [unique arguments](https://docs.sendgrid.com/for-developers/sending-email/unique-arguments) and pass `unique_args` as shown below:

```json
{
  "unique_args": {
    "statsig_experiment_name": "[Experiment Name]",
    "statsig_variant_name": "[control or test]"
  }
}
```

For the example above, set up the Control variant like this:

```json
{
  "unique_args": {
    "statsig_experiment_name": "drip_campaign_ab_test",
    "statsig_variant_name": "control"
  }
}
```

And the Test variant looks like this:

```json
{
  "unique_args": {
    "statsig_experiment_name": "drip_campaign_ab_test",
    "statsig_variant_name": "control"
  }
}
```

## Running more than two variants

You can extend this setup to run ABC or ABn tests by adding more variants in the Experiment Setup tab. Make sure you apply the variant name correctly in either the Single Send name or the unique arguments in the API.

{% figure %}
![ABC test variant configuration interface](https://docs.statsig.com/images/guides/sendgrid-email-abtest/210738925-89f91116-fefc-4e3c-992d-c9fc228f1a8c.png)
{% /figure %}
