Email Script Tokens - To Replace Strings in Fields?

Amy_Lepre
Level 7

Email Script Tokens - To Replace Strings in Fields?

Here's our scenario:

We create an interesting moment for a lead when they download a specific type of file from us. The name of that file is in the last part of the URL on the preceding page. Something like: www.southco.com//n-us/c5/c5-11-112 where we want to capture just the c5-11-112 portion of the URL. Right now we're defining the interesting moment by a trigger of clicking on a web page (the thank you page, essentially - but this all on our external website, not Marketo LPs) and giving the interesting moment the value of {{trigger.Referrer}}. This gets us the information about the file (part number) that we need to pass onto sales, but includes the extraneous URL data.

So, I want to rewrite the data in the interesting moment field by replacing the redundant parts of the URL with an empty space. I got partway there using a regular text token using JavaScript, but could not figure out how to pull the value of the token into the JS. My code looked something like this:

<script type="text/javascript">
var re = /http/gi;
var str = {{lead.Last Interesting Moment}};
var newstr = str.replace(re, "");
 
document.write(newstr );
</script>

I also tried:

<script type="text/javascript">
var re = /http/gi;
var str = "{{lead.Last Interesting Moment}}";
var newstr = str.replace(re, "");
 
document.write(newstr );
</script>

Neither of those worked, but if I replaced the token value with a static string like "Here is my URL http://www", it worked when placing that token on a landing page. (I saw "Here is my URL ://www".)

To try to get the value of the token, I thought I'd try the email script token method that's shown here: https://community.marketo.com/MarketoArticle?id=kA050000000LB9MCAW

 
I am not a VLT developer by any means, so I was mostly just sort of trying things out in there. Here's what I tried:

${lead.LastInterestingMoment}
#set ($a = ${lead.LastInterestingMoment})
#set ($b = $a.replace("http://www.southco.com/", ""))
#set (${lead.LastInterestingMoment} = $b)

This did not strip out the "http://www.southco.com/" but did display the value of the {lead.LastInterestingMoment} token when I sent an email. 

Is there a way to change the value of a field like this in Marketo? I'm not sure if I'm going about it the right way or using the right methods. Any feedback or experience is appreciated.

Thanks!
 
Tags (1)
2 REPLIES 2
Anonymous
Not applicable

Re: Email Script Tokens - To Replace Strings in Fields?

Try this:

<script type="text/javascript">
var re = "/http/gi";
var str = "{{lead.Last Interesting Moment}}";
var newstr = str.replace(re, "");
 
document.write(newstr );
</script>

Try adding a "" around /http/gi, as show above.

In JavaScript, what you are doing is creating a variable called re, and assigning it a value of "/http/gi" The value is a data type called a string. Without the "", you would be saying that the value of variable re equals the value of variable /http/gi. This would return an error because you have not assigned a value to that variable. 

Let me know if that works for you. 
Amy_Lepre
Level 7

Re: Email Script Tokens - To Replace Strings in Fields?

Hi Murtza,

Thanks for your response.

This doesn't work, though; in an email, it comes in as nothing. I think the first set of code I had was correct because if I wrote it like this:

<script type="text/javascript">
var re = /http/gi;
var str = "http://www.southco;
var newstr = str.replace(re, "");
 
document.write(newstr );
</script>

I see ://www.southco come through as the value. Interestingly, this does not work in email, but on a landing page. 

I could be going about this from the wrong angle, though, as I'm not sure if even if I can get the value of a My Token to strip out the extraneous text if I can then get that new value to write to a field on the user's profile.