[SalesForce] Guide Template Language to provide dynamic links via href attribute from JSON

I'm using GTL to parse a JSON attribute coming from a Data Extension.
Everything works fine except links in href attribute. For some reason/limitation they don't render in the href attribute.

I was trying to set an AMPScript variable with the GTL attribute but it doesn't seem to work.

I've found this other question which is pretty similar but it's hard to apply in my scenario since I need to use GTL to parse JSON – Guide Template Language to provide dynamic links via href attribute

This is an excerpt of the code I'm using

%%[ var @Json set @Json = data ]%%
{{.datasource JSONVar type=variable}}
  {{.data}}
    { "target" : "@Json" }
  {{/data}}
  %%[ var @event_url set @event_url = "{{url}}" ]%%
  <table border="0" cellpadding="0" cellspacing="0" height="415" style="border:1px solid #cccccc;" width="100%">
    <tr>
      <td align="center" bgcolor="#333333" valign="top">
        <a href="{{url}}"><img alt="{{name}}" height="375" src="{{thumb}}" width="500" /></a>
      </td>
    </tr>
    <tr>

Is there any workaround for this? Any help would be great!

Thanks.

Best Answer

I ran into the same problem. GTL will only insert a link from JSON data into an href if link tracking is disabled. I also looked for a way to let GTL parse the value out of my JSON data and then pass it into AMPscript to workaround the href issue.

It is possible to read the GTL value into AMPscript via the TreatAsContent function. With the GTL tag inside of the TreatAsContent function then it will be correctly dereferenced within the scope of the current data context, and the value gets returned to the caller of the AMPscript function. Thus you can put together an href that follows a link from a GTL tag like so:

<a href="%%=TreatAsContent('{{url}}')=%%">click me</a>

And if you want to explicitly enable link tracking (considered a good practice by SFMC), you can chain the TreatAsContent function with the RedirectTo function like so:

<a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%">click me</a>

If you prefer not to inline the AMPscript call or if you need the json value as a variable, you can use TreatAsContent to set an AMPscript variable with the GTL value:

  %%[ var @url set @url = TreatAsContent("{{url}}") ]%%
  <a href="%%=v(@url)=%%">click me</a>

Similarly, you can do the same thing in SSJS if you need the GTL value there:

<script runat="server">
    Platform.Load("core","1");
    var url = Platform.Function.TreatAsContent("{{url}}");
</script>

And here is a sendable example showing what it looks like when you put this together with the GTL datasource:

<html>
%%[
var @Json set @Json =
'{"url":"http://www.google.com/?query=42","thumb":"http://www.wpclipart.com/small_icons/buttons/.cache/button_green.png"}'
]%%

<body>

{{.datasource JSONVar type=variable}}
{{.data}}
{ "target" : "@Json" }
{{/data}}
    Click the button to follow the link: <a href="%%=RedirectTo(TreatAsContent('{{url}}'))=%%"><img alt="{{name}}" src="%%=TreatAsContent('{{thumb}}')=%%" height="100" width="100"/></a>
{{/datasource}}

</body>
</html>
Related Topic