Reading personalization token into a javascript var

Hi,

I am trying to get the data from a personalization token into a javascript var. Is this possible?

I have a form where people can score on 5 questions. The average of the 5 scores are put in a var “averageScore” (via Javascript). This averageScore is put in a hidden form field (average).

When people submit the form, they get a landingpage with a Google Graph (bar) and it must show the the average.

Thanks for helping.

@basvanKesteren yes, it’s possible. In the past when I’ve done this you do have to add a few second delay for the personalization token to populate with the value. Usually it takes a bout 5 seconds after the form was submitted from my own experience.

In your coded template, or in the HubL section of a custom module, you can do this:

<script>
 var myJavascriptVar = {{ contact.my_custom_contact_property }};
</script>

There is also a new HubL function for this: HubL functions - HubSpot docs

Thanks Stefen. Sometimes I overlook the simple things.

Any tips for coding a delay?

@basvanKesteren last time I did something like this I used some javascript on the thank you page that checked for the “submissionGuid” query parameters in the URL which would trigger a loading spinner and then refresh the page after a couple seconds. Here’s the example code:

if (window.location.search.indexOf('submissionGuid') > -1) {
 $('body').addClass('loading');
 $('body').append('<div class="loader"></div>');
 setTimeout(function(){ 
 window.location.href = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
 }, 2000);
}