I'm picking up salesforce marketing cloud and refreshing myself in AMPscript/SSJS. Could you give me some feedback on the following snippet? I don't have access to SFMC to test out, so I'm just reading the documentation and using the forums.
For context, I was given a javascript script snippet and told to reproduce in SFMC for an email send. I thought about doing it all in AMPscript but creating a functions to create a random string, and reproducing Math.Random() and charCodeAt in AMPscript isn't quite the same. I feel a combination of AMPscript and SSJS would work better? Most of the javascript I was given hasn't been changed, I just refactored. If you could let me know if this would work and would be an efficient way to do it.
<!-- Setting AMPscript variables -->
%%[
Var @url, @field_1
Set @url = ""
Set @field_1 = "Random String"
]%%
<!-- SSJS Block to do the math for link -->
<script type="text/javascript" runat="server">
Platform.Load("core", "1.1.1");
var myStr = ""
var gap = "~"
var encString = "";
// Function to get random string
function getRandStr(leng) {
var str = "";
for(var i = 0; i < leng; i++){
str += parseInt(Math.Random()*5);
}
return str;
}
// Get value from AMPscript variable
myStr = Platform.Variable.GetValue('@field_1');
// Create random url
for(j = 0; j < myStr.length; j++) {
encString += getRandStr(5) + myStr.charCodeAt(j) + gap + getRandStr(3);
}
// Pass back to AMPscript variable
Platform.Variable.SetValue("@url", encString);
</script>
<!-- inline AMPscript to print url -->
<a href='%%=Concat("http://", V(@url))=%%' class="button">Click Me</a>