Marketo Velocity Script Error While Changing CTA Link Based on Lead Country | Community
Skip to main content
Level 2
July 7, 2026
Solved

Marketo Velocity Script Error While Changing CTA Link Based on Lead Country

  • July 7, 2026
  • 7 replies
  • 170 views

Hi everyone,

I have a requirement where the email content remains the same, but the CTA link needs to change dynamically based on the lead's country using a Velocity Script in Marketo.

However, I'm getting an error when I deploy the campaign.

Requirement:

Behavior:

  • ✅ Email Preview: Works as expected.
  •  

  • ❌ Smart Campaign Send: Fails with a Velocity Script error.

Velocity Script:

#set($country = ${lead.Country})
#set($url = "www.google.com")
#if($country == "India")
    #set($url = "example.com/in")
#elseif($country == "United States")
    #set($url = "www.youtube.com ")
#elseif($country == "Canada")
    #set($url = "example.com/ca")
#else
    #set($url = "www.google.com")
#end

${url}

HTML:

 <td valign="top" align="center" style="text-align: center; padding: 12px 20px; color: #1570EF; font-size: 16px; font-family:Arial; font-weight: bold;background-color: #FFFFFF;" bgcolor="#FFFFFF;"> <span class="mktoText" id="text" mktoname="text" style="color:#1570EF; background-color:#FFFFFF;  text-decoration: none !important;"><p><a href="https://{{my.urlcountry}}" target="_blank" style="text-decoration: none !important; color: #1570ef;" data-bipfieldlink="Text-12" data-=""><strong style="text-decoration: none;" data-bipfieldid="Text-12" data-bipfieldlabel="Banner CTA">${CtaText}</strong></a></p></span> </td> 

Error:

 

 

Has anyone encountered this issue before? Am I missing something in the Velocity syntax, or is there a better approach for dynamically changing CTA links based on the lead's country?

Any help or suggestions would be greatly appreciated. Thank you!

Best answer by PeeyushSachdeva

@Ram_SaiDa

I tested your implementation and was able to reproduce the behavior.

 

The Velocity script itself is fine for resolving the URL based on the lead's country. However, the issue is that the script is only returning the URL, and you're then using that Velocity token inside the href attribute:

 

<a href="https://{{my.urlcountry}}">

 

While this correctly resolves the destination URL, however, this approach causing issues with Marketo's link tracking.

 

What worked for me was generating the complete <a> tag within the Velocity script instead of returning only the URL. Then, in the email, I simply rendered the Velocity token directly rather than wrapping it inside another <a> tag.

#set($country = ${lead.Country})
#set($url = "google.com")
#if($country == "India")
#set($url = "example.com/in")
#elseif($country == "United States")
#set($url = "youtube.com ")
#elseif($country == "Canada")
#set($url = "example.com/ca")
#else
#set($url = "google.com")
#end

<a href="https://${url}" target="_blank">CTA Button</a>

 

After testing with multiple country values (US, Canada, and India), the CTA resolved to the correct destination for each recipient, and Marketo click tracking also worked as expected.

I'd recommend trying that approach if your goal is to have both dynamic URLs and reliable click tracking.

 

I'd recommend using Dynamic Content feature to personalize the CTA link based on the recipient's country instead of velocity script.

7 replies

BlaneMcMichen-1
Adobe Employee
Adobe Employee
July 7, 2026

Hi

This has been explained in this post, but you cannot use a local script token to complete a url within a tracked link.

Use this setup:

Email Script Token

#set($country = $lead.Country)
#set($url = "https://www.google.com")

#if($country == "India")
#set($url = "https://example.com/in")
#elseif($country == "United States")
#set($url = "https://www.youtube.com")
#elseif($country == "Canada")
#set($url = "https://example.com/ca")
#end

${url}

HTML

 

<a href="{{my.urlcountry}}" target="_blank" style="text-decoration:none !important; color:#1570ef;">
<strong style="text-decoration:none;">${CtaText}</strong>
</a>

If it still doesn't work

The likely reason is Marketo tracked-link compilation with Email Script Tokens. In that case:

<a href="{{my.urlcountry}}" class="mktNoTrack" target="_blank" style="text-decoration:none !important; color:#1570ef;">
<strong style="text-decoration:none;">${CtaText}</strong>
</a>

Why this fixes it

Issue Fix
Invalid variable assignment syntax Use $lead.Country instead of ${lead.Country} inside #set
Bad URL output Remove trailing spaces and include full https:// URLs
Token mismatch Make sure {{my.urlcountry}} is the actual Email Script Token name
Link tracking/resolution issues Use fully formed URLs or class="mktNoTrack" if needed

 

Ram_SaiDaAuthor
Level 2
July 14, 2026

Hi ​@BlaneMcMichen-1, First of all, thank you for your input. I had already tried this approach, but the issue is that Marketo is not tracking the clicks when it's implemented this way. My requirement is not only to redirect users to the correct URL based on the lead's country, but also to have Marketo capture and report the click activity for the CTA.

SanfordWhiteman
Level 10
July 8, 2026

If you’re deliberately untracking a link instead of fixing the code, you should at least add

mkt_tok=${mktmail.MKT_TOK}

to the query string so you don’t lose cookie association.

 

Also ​@Ram_SaiDa please ensure you use the Insert Code function here on the community so your code is readable.

 

PeeyushSachdeva
Level 4
July 15, 2026

Hi ​@Ram_SaiDa, I followed the script you shared, selected Country from Person Standard Objects in the right panel of the script window, used the token in the email CTA as you did, and sent a test email via smart campaign. It delivered successfully.

 

 

Ram_SaiDaAuthor
Level 2
July 16, 2026

Hi ​@PeeyushSachdeva, First of all, thank you for your input. I had already tried this approach, but the issue is that Marketo is not tracking the clicks when it's implemented this way. My requirement is not only to redirect users to the correct URL based on the lead's country, but also to have Marketo capture and report the click activity for the CTA.

PeeyushSachdeva
Level 4
July 16, 2026

Ah, I see - in that case, the approach ​@BlaneMcMichen-1  shared is the right one to follow.

Quick question on your testing: how are you checking click tracking? Are you using "Send Sample" to yourself, or sending a real email either through a Smart Campaign or via DB > Person Actions?