cookie persistence issue - how to resolve for UTM data
If someone completes a form (on an LP) where the URL has UTM values included, those values reveal in the form-mail email receipt we receive. Some visitors will arrive at LP and complete form directly, no UTM data. In those cases form-mail receipt should reveal 'direct visit' or 'not available' where I've got the tokens for each UTM property.
Due to cookies preserving the end-user's last visit, it is not correctly tracking campaign info IF the end-user is a direct visit to the landing pg... it records the last campaign they interacted with and lists that, incorrectly in the email receipt (form-mail).
Set up that is not working:
Key Notes:
Place this script in the LP HTML, ideally just before the closing </body> tag, OR in an open text field in footer's HTML. Rich Text Block. (see JS below) ...I cannot access closing body tag in the form or my LP, so I am utilizing a rich text block in the footer's HTML where I have some other public facing text... I just dropped the script in after in HTML vieew.
Ensure the form fields in Marketo are named exactly as “UTM Source”, “UTM Medium”, and “UTM Campaign.”
In the hidden UTM field within the form, under Autofill modal, Default Value: None, Direct Visit
Get Value from... I have: Use Default Value as Marketo Support advised not to use URL parameter; that the JS would take care of everything. And there is no other option here for "Not Set".
Please advise how I can resolve.
<script>
// Utility function to fetch URL parameters
function getUrlParam(param) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}
// Function to set values for UTM fields
function setUtmField(form, fieldName, utmParam) {
const value = getUrlParam(utmParam) || "Direct Visit";
const field = form.getFormElem().find(`[name="${fieldName}"]`);
if (field.length) {
field.val(value);
// Store in sessionStorage
if (value !== "Direct Visit") {
sessionStorage.setItem(fieldName, value);
} else {
sessionStorage.removeItem(fieldName);
}
}
}
// Wait for Marketo form to be ready
MktoForms2.whenReady(function(form) {
setUtmField(form, "UTM Source", "utm_source");
setUtmField(form, "UTM Medium", "utm_medium");
setUtmField(form, "UTM Campaign", "utm_campaign");
});
</script>