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>
(Please use the Syntax Highlighter (“Insert/Edit Code Sample”) when adding code so it’s readable. I edited your post this time.)
Who wrote this code? It makes little sense as the form fields can never be called UTM Source
, UTM Medium
, and UTM Campaign
. Form field names (a.k.a. SOAP API names) do not have spaces.
The code also
1. never reads from sessionStorage
despite writing to it
2. doesn’t correctly set the field value (you use addHiddenFields
/setValues
for this).
It looks like a jumble of half-abandoned code.
Adobe Marketo Support provided the JS.
And, they confirmed the API (soap) or (rest) field values from our CRM are not to be used for this purpose. (those utm values exist in CRM and are synced down into Marketo).
The Marketo name is what we were advised to use, and those have a space.
Well, code should be written by experienced developers. Support doesn't know how to code, and testing will show they're wrong in the ways I noted. Simply look at the name
attribute of any form field.