How to Track Email Experiment Results in Code
This how-to guide shows you how to track email experiment results in code with marginal, the hosted email MCP server for AI agents. You'll launch a subject line A/B test, fetch open and click metrics, and read within-test lift — all from the same workflow your agent already runs.
What you need before you start
Tracking results in code means your AI agent calls marginal's MCP tools directly instead of clicking through a dashboard. Once your client is connected, the four tools — generate_variants, launch_test, get_results, and recommend_next — become available as functions your agent can invoke.
- A marginal API key in the form marg_live_... configured as a Bearer token
- The MCP endpoint https://marginal.sh/mcp registered in a supported client
- A client like Cursor, Claude Code, Codex, Windsurf, Cline, Continue, or Zed
- The free tier covers 100 experiments/month, enough to follow this tutorial end to end
Step 1: Launch a test you can measure
Before you can track anything, you need a running experiment. Use generate_variants to draft subject lines, then launch_test to start a managed send. Each launch returns a test identifier — keep this ID, because it's the handle you'll use to pull results later.
Because the send is managed by marginal, open and click tracking is wired up automatically. You don't instrument anything yourself; the metrics accrue against the test ID as recipients interact with the email.
- Call generate_variants to produce candidate subject lines for a subject line A/B test
- Call launch_test with your chosen variants and audience to begin the managed send
- Store the returned test ID in your code or agent state for later lookups
Step 2: Pull and track results with get_results
To track email experiment results in code, call get_results with the test ID from Step 1. It returns the open and click figures per variant along with within-test lift metrics, so you can see which subject line is winning relative to the control rather than reading raw counts in isolation.
Since this is a single tool call, it's easy to poll on a schedule — check results an hour after launch, then again the next morning. Your agent can compare the lift values between runs to decide whether the signal is stable enough to act on.
- Open rate and click rate broken out by variant
- Within-test lift so you know the relative winner, not just totals
- A consistent shape you can parse and log in your own code
Step 3: Decide the next move and automate it
Tracking results is only useful if you act on them. Pass the test ID to recommend_next and marginal suggests the follow-up step based on the observed lift — for example promoting the leading subject line to the rest of your list.
This closes the loop for AI agent email marketing: generate, launch, measure, and recommend all happen as MCP tool calls. You can chain them in a single agent run so each new campaign learns from the last. See the full reference at https://marginal.sh/mcp and the docs at https://marginal.sh/docs/ for tool parameters.
- Use recommend_next to turn lift metrics into a concrete next action
- Re-run get_results to confirm the recommendation held after more data arrived
- Wrap the sequence in your agent so this marginal MCP tutorial becomes a repeatable workflow
Get started with marginal
marginal is a hosted email marketing MCP server at marginal.sh. Sign up free, create an API key, and connect https://marginal.sh/mcp from Cursor, Codex, or Claude Desktop.
- 100 experiments/month on the free tier
- Four MCP tools: generate_variants, launch_test, get_results, recommend_next
- Listed in the MCP Registry as sh.marginal/mcp