The instructions below walk through the one-time setup of a code snippet for use in all experiments with subject line and preheader optimisation.
1. Create a code snippet in Salesforce Marketing Cloud (SFMC) using the following code:
<!-- Experiment variables
-->
%%[
SET @experimentId = "XXXXXX"
SET @defaultSL = "Paste fallback subject here"
SET @defaultPH = "Paste fallback preheader here"
]%%
<!--
Phrasee Ltd.
Master Code Snippet
This section needs to appear in an email template once, if it appears more than once the copy is safe to remove
-->
<script type="javascript" runat="server">
Platform.Load("core", "1");
function getRandomVariant(weights, variantIds){
var randomProportion = Math.random(), aggregatedProportion = 0;
for (var i = 0; i < weights.length; i++){
aggregatedProportion += Number(weights[i]);
if (randomProportion < aggregatedProportion){
return variantIds[i];
}
}
}
function getPhraseeVariant(phraseeExperimentId) {
var data = Platform.Function.LookupOrderedRows(phraseeExperimentId + "-batches", 1, "timestamp Desc", "secret", "1").pop();
var proportions = data.proportions.split(" ");
var variantIds = data.variant_ids.split(" ");
var batchId = data.batch_id;
var variantId = getRandomVariant(proportions, variantIds);
var variantText = Platform.Function.Lookup(phraseeExperimentId + "-variants", "variant_text", "variant_id", variantId);
var sendId = Platform.Function.GUID();
var statisticsUrl = "https://mail-tracking.phrasee.co/live/open?batch_id=" + batchId +"&campaign_id=" + phraseeExperimentId +"&variant_id=" + variantId +"&send_id=" + sendId;
try {
Platform.Function.InsertDE(phraseeExperimentId +"-recipients", ["batch_id", "variant_id", "subscriber_key", "timestamp", "job_id", "sfmc_job_batch_id"], [batchId, variantId, Attribute.GetValue("_subscriberkey"), Now(1), Attribute.GetValue("jobid"), Attribute.GetValue("_JobSubscriberBatchID")]);
} catch(err) {
Platform.Function.InsertDE(phraseeExperimentId + "-recipients", ["batch_id", "variant_id", "subscriber_key", "timestamp", "job_id", "sfmc_job_batch_id"], [batchId, variantId, Attribute.GetValue("_subscriberkey"), Now(1), Attribute.GetValue("jobid"), Attribute.GetValue("_JobSubscriberBatchID")]);
}
return [variantText,statisticsUrl]
}
</script>
<!-- Master Code Snippet End --->
<!--
Phrasee Ltd.
Experiment Code Snippet
This section applies to a Phrasee experiment, if you wish to run multiple experiments per email template please add the required logic.
-->
<script type="javascript" runat="server">
var previousVariantText = Variable.GetValue("@phraseeVariantText");
var experimentId = Variable.GetValue("@experimentId");
var defaultSL = Variable.GetValue("@defaultSL");
var defaultPH = Variable.GetValue("@defaultPH");
try {
if (!previousVariantText) {
var phraseeVars = getPhraseeVariant(experimentId);
Variable.SetValue("@phraseeVariantText", phraseeVars[0]);
Variable.SetValue("@statisticsUrl", phraseeVars[1]);
var slHb = phraseeVars[0].split('\u303D');
var phraseeSubjectLine = slHb[0] || '';
var phraseePreheader = slHb[1] || '';
Variable.SetValue('@phraseeSubjectLine', phraseeSubjectLine);
Variable.SetValue('@phraseePreheader', phraseePreheader);
}
} finally {
var currentVariantText = Variable.GetValue("@phraseeVariantText");
if (!currentVariantText) {
var sendId = Platform.Function.GUID();
Variable.SetValue('@phraseeSubjectLine', defaultSL);
Variable.SetValue('@phraseePreheader', defaultPH);
Variable.SetValue("@statisticsUrl", "https://mail-tracking.phrasee.co/live/open?batch_id=0&campaign_id=" + experimentId +"&variant_id=00000&send_id=" + sendId);
}
}
</script>
<img src="%%=v(@statisticsUrl)=%%" style="display:none" width="1" height="1">
<!-- Experiment Code Snippet End -->.png?sv=2026-02-06&spr=https&st=2026-06-04T22%3A02%3A32Z&se=2026-06-04T22%3A15%3A32Z&sr=c&sp=r&sig=UFUDR8a81M73cV30z09cTUISB%2BD1Q7l4ro53OIR4yPg%3D)
2. Create an experiment in Jacquard as per normal, approve it, and start the optimisation.
3. Copy the experiment ID from the Logs panel in the experiment..png?sv=2026-02-06&spr=https&st=2026-06-04T22%3A02%3A32Z&se=2026-06-04T22%3A15%3A32Z&sr=c&sp=r&sig=UFUDR8a81M73cV30z09cTUISB%2BD1Q7l4ro53OIR4yPg%3D)
4. Drag and drop or paste the code snippet created for all experiments into your email. Do not use the code snippet Jacquard automatically creates for the individual experiment when you activated it.
Do not reference the reusable code snippet
You must drag and drop the code snippet into the email or paste the HTML itself. Do not use an AMPscript reference to refer to the code snippet. You must create a local copy in the email in order to include the correct experiment ID.
.png?sv=2026-02-06&spr=https&st=2026-06-04T22%3A02%3A32Z&se=2026-06-04T22%3A15%3A32Z&sr=c&sp=r&sig=UFUDR8a81M73cV30z09cTUISB%2BD1Q7l4ro53OIR4yPg%3D)
5. Copy the experiment ID into the code snippet with the relevant fallback subject and preheader.
6. Set the subject to:
%%=TreatAsContent(@phraseeSubjectLine)=%%
7. Set the preheader to:
%%=TreatAsContent(@phraseePreheader)=%%
8. Test and preview your email. You should see the subject and preheader fields populate if you have configured the snippet, subject, and preheader correctly.
.png?sv=2026-02-06&spr=https&st=2026-06-04T22%3A02%3A32Z&se=2026-06-04T22%3A15%3A32Z&sr=c&sp=r&sig=UFUDR8a81M73cV30z09cTUISB%2BD1Q7l4ro53OIR4yPg%3D)