Re: Using {{program.id}} inside velocity?

Chris_Wilcox
Level 9

Having the hardest time figuring out how to access the {{program.id}} value inside of a velocity script token.

 

I found @SanfordWhiteman 's article with the workaround, but having trouble implementing it. 

 

https://blog.teknkl.com/accessing-the-program-id-and-campaign-id-from-velocity-the-new-way-post-june...

 

I put this short script in the <head> my email template (I can't using the unsubscribe HTML section like he suggested), and I can see the value populating correctly in the head.

<script>
#set ($MAContext = {
	"programId" : "{{program.id}}"
})
</script>

Now, I'm struggling with how to access that value later in my email inside my script token. 

 

I tried using this, but didn't work. 

${MAContext.programId}

 

Anyone ever done this before?

14 REPLIES 14
Jep_Castelein2
Level 10

It's my experience that if you assign the Program ID to a Velocity variable in the email itself, it's not available in the context of Email Script Tokens and vice versa. However, as long as you don't need Custom Object or Opportunity Data, you can just write your entire Email Script in the Email Template. Surprisingly enough, it doesn't have to be in an Email Script token. 

In Sanford's blog post, he puts it in the "Unsubscribe HTML" area. I have never tested whether Velocity variables defined there are indeed available in Email Script Tokens. 

SanfordWhiteman
Level 10 - Community Moderator

In Sanford's blog post, he puts it in the "Unsubscribe HTML" area. I have never tested whether Velocity variables defined there are indeed available in Email Script Tokens. 

They are (or were at the time the post was published). The goal is to transform {{program.tokens}} into Velocity ${references} by knowing there’s a common context where both are available

 

There’s actually yet another way to do it, but it’s even more experimental so don’t want to go there now.

Caleb_Kelly
Level 2

Hi @SanfordWhiteman -  in your reply, you mention:


There’s actually yet another way to do it, but it’s even more experimental so don’t want to go there now.




Are you able to elaborate more on this? I'm curious about your experimental workaround. 🙂

SanfordWhiteman
Level 10 - Community Moderator




Are you able to elaborate more on this? I'm curious about your experimental workaround. 🙂


I can’t describe it here — yet — because the risks are high if you use it wrong and it needs a whole blog post. But think about #define and you’ll eventually figure it out.

Caleb_Kelly
Level 2

For whatever reason when we declare regular tokens in the body of the email, we are able to leverage the $program directive in velocity without having to set a velocity object in the unsubscribe box.

 

Email:

<!--{{Program.Id}} {{Program.name}} --> 

 

Velocity:

$program
SanfordWhiteman
Level 10 - Community Moderator

@Chris_Wilcox I just tested the code in that post in a new instance and it still works fine. Don't know what else you could be doing differently.

 

 

Chris_Wilcox
Level 9

It must be because I'm putting my code in the email template, not the unsubscribe language. We have a custom unsub link so getting that unsubscribe language to appear on our emails is actually an issue (even if it's just the comment). 

 

I'll play around some more. At this point, my solution isn't working for other reasons so I might not be able to go down this path anyway.  Thanks for your help as always!

Caleb_Kelly
Level 2

Hi Chris - Were you ever able to make this work? We have been looking for a similar workaround for a while now.

Chris_Wilcox
Level 9

Nope, I abandoned this idea and went instead to try to get Asset ID (which I could then, via the API, associate back to a program ID since an asset can only exist in 1 program). 

 

I did KIND OF get asset ID working. It worked in the sense that the code does dynamically populate the asset ID of the email it's within, however my goal was to use the script to populate a querystring element with that emails' Asset ID to associate pageviews to individual emails dynamically and at scale, but the velocity script does not run if it's included as a part of a TRACKED link. Works perfectly in an email body or in the HTML, or even an untracked link.  But as soon as you try to use velocity in a tracked link, it breaks, does not run, and the query element just shows the velocity code instead of the code executing.  

 

It's apparently a limitation of the email compiler to be able to run velocity and within the tracked URL, then generate the tracked link. I was disappointed. 

 

#set( $delim = ":|-" )
#set( $values = $mktmail.xMarketoIdHdr.split($delim) )
#set( $assetId = $values[7] )
${assetId}

 

SanfordWhiteman
Level 10 - Community Moderator

In my blog post, it’s in comment tags, not a script.

 

But you are accessing it the right way — what do you see when you output just

${MAContext}
Chris_Wilcox
Level 9

It just output's the text "${MAContext}" when I output

${MAContext}

 

I realize you had it as a comment, but since I had to move the code to my email template, I wrapped it in <script> tags instead. 

Chris_Wilcox
Level 9

One update...

 

I looked at the source code of my delivered email using the script, and it looks like the value is not appearing in the code. I tried with the script wrapped both in the <script> tags, as well as adding it between comment tags. In both instances, none of the code is present in the header. But if I add the script to body of an email, seems to work. 

 

Going to try a few more things...

 

Chris_Wilcox
Level 9

Even tried putting the script (both wrapped in <script> tags and commented out) in the <body> section, still same thing. The code itself is not rendering when the email goes to compile. I just see two script tags with nothing in between.

<script>
</script>

 

SanfordWhiteman
Level 10 - Community Moderator

Well the #set wouldn’t render anything. I’ll recheck the code in one of my live instances, it’s been running for a long time and I’m not aware of a regression.