Accessing the AB Test Variant ID from Velocity

In an earlier post I showed how to access (and output) the Campaign ID, Campaign Run ID and other fields from Velocity. One field I left out, as pointed out by reader AK, was the Test Variant ID.

Picking up the Test Variant (vital for correlating Activity Logs to what recipients actually saw!) is easy enough, but it led me to refactor the rest of the code as well.

Unfortunately, Marketo has since hidden the Program ID in Velocity, so we can’t get that anymore and I’ve removed those lines. ☹️

So, going forward, use this snippet to create the $marketoSend object with all the interesting IDs:

#set( $marketoSend = {} )
#set( $delim = ":|-" )
#set( $values = $mktmail.xMarketoIdHdr.split($delim) )
#set( $numParts = $values.size() )
## Campaign Run ID not relevant in Preview, Test Variant not always present, etc.
#set( $keyTemplatesBySize = {
   13 : "MunchkinId-+MunchkinId-+MunchkinId::CampaignId:StepId::AssetId::CampaignRunId:LeadId--TestVariantId",
   12 : "MunchkinId-+MunchkinId-+MunchkinId::CampaignId:StepId::AssetId::CampaignRunId:LeadId-",
   11 : "MunchkinId-+MunchkinId-+MunchkinId::CampaignId:StepId::AssetId::CampaignRunId:LeadId",
  "*" : "MunchkinId-+MunchkinId-+MunchkinId::CampaignId:StepId::AssetId::LeadId"
} )
#set( $marketoIdKeys = $display.alt($keyTemplatesBySize[$numParts],$keyTemplatesBySize["*"]) )
#set( $keys = $marketoIdKeys.split($delim) )
## loop interesting keys
#foreach( $key in $keys )
#if( !$key.isEmpty() )
#if( $key.startsWith("+") )
#set( $finalKey = $key.substring(1) )
#set( $marketoSend[$finalKey] = $marketoSend[$finalKey] + "-" + $values[$foreach.index] )
#else
#set( $finalKey = $key )
#set( $marketoSend[$finalKey] = $values[$foreach.index] )
#end
#end
#end
Munchkin ID is ${marketoSend.MunchkinId}
Campaign ID is ${marketoSend.CampaignId}
Campaign Run ID is ${marketoSend.CampaignRunId}
Step ID is ${marketoSend.StepId}
Asset ID is ${marketoSend.AssetId}
Lead ID is ${marketoSend.LeadId}
Test Variant ID is ${marketoSend.TestVariantId}