APIs & Integrations

JKBaseer1
Member

How to get UTM passed back to the destination URL after user click submit button

Situation:

when a user visit this page > https://promo.exness.com/vi/educ1707/ebook/10-ways-to-improve-trading-strategy-v1?utm_source=jk_test...
And he fill the form > Click signup button > Go to the next page, his UTM are missed. Meaning the utm_source is not the source on the NEXT PAGE.

Previously we are able to solve using a JS script code to query the current page UTM and put it across all external URL (Below is the code) but it’s not working on this situation as its a button and it doesn’t have HREF element,

---- script—
function updateQueryString(uri, key, value) {
var re = new RegExp("([?&])" + key + “=.*?(&|$)”, “i”);
var separator = uri.indexOf(’?’) !== -1 ? “&” : “?”;
if (uri.match(re)) {return uri.replace(re, ‘$1’ + key + “=” + value + ‘$2’);}
else {return uri + separator + key + “=” + value;}
}

var links = $(“a.external-link”);
for(var i = 0; i < links.length; i++) {
links[i].href = updateQueryString(links[i].href, “utm_source”, “{{utm_source}}”);
links[i].href = updateQueryString(links[i].href, “utm_campaign”, “{{utm_campaign}}”);
links[i].href = updateQueryString(links[i].href, “utm_medium”, “{{utm_medium}}”);
links[i].href = updateQueryString(links[i].href, “utm_term”, “{{utm_term}}”);
links[i].href = updateQueryString(links[i].href, “utm_content”, “{{utm_content}}”);
}
— script

any idea how we can solve this?

0 Upvotes
2 Replies 2
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

How to get UTM passed back to the destination URL after user click submit button

Hi @JKBaseer,

You could use the HubL variable request.query to add the query parameters to the redirectUrl field of your form’s custom embed code; the developer doc below has some more info on customizing the form embed code.

How to customize the form embed code

This is a list of options that you can use if you just need to tweak a default hubspot form. If you need complete control over the styles and actions of your form, you will still want to use the Forms API.

JKBaseer1
Member

How to get UTM passed back to the destination URL after user click submit button

Thanks Derek. Somehow missed this message. We solved it

This is the solution our tech team found
var urlParam = function(name){
var results = new RegExp(’[?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results || [])[1];
};
var setUrlParamToForm = function(name, form) {
var param = urlParam(name);
if (param) {
$(’’).attr({
type: ‘hidden’,
name: name,
value: param,
}).appendTo(form);
}
};
setUrlParamToForm(‘utm_source’, $form);
setUrlParamToForm(‘utm_campaign’, $form);
setUrlParamToForm(‘utm_medium’, $form);
setUrlParamToForm(‘utm_term’, $form);
setUrlParamToForm(‘utm_content’, $form);

code which makes hidden fields in form, with utm params from url