A couple months ago I wrote a tutorial on how to use UTM parameters to capture lead source. The tutorial is an in-depth overview of how to capture URL parameters when a lead visits a landing page directly but if a lead visits a different page then fills out the form, those URL parameters won’t be captured. To fix this issue, you can store cookies in the lead’s browser and configure the form fields to pull from the cookie values instead of the URL parameter.

hidden-fields-in-forms-2

In this example, we will be setting 3 different cookies.

  1. utm_source
  2. utm_medium
  3. utm_campaign

Parse the URL

The URL parameters need to be parsed so the cookie values can be set. This can be done using javascript.

// Parse the URL
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');

Setting the Cookie Values

In order to set the cookie values, jQuery and the jQuery Cookie Plugin must be on the page.



The syntax for creating a cookie using the jQuery plugin is very simple.

$.cookie('name', 'value');

Using the variables defined in the first step, the cookie values can be set.

// Set the cookies
if($.cookie('utm_source') == null || $.cookie('utm_source') == "") {
$.cookie('utm_source', source);
}
if($.cookie('utm_medium') == null || $.cookie('utm_medium') == "") {
$.cookie('utm_medium', medium);
}
if($.cookie('utm_campaign') == null || $.cookie('utm_campaign') == "") {
$.cookie('utm_campaign', campaign);
}

Testing

To ensure the cookie values are being stored in the browser correctly, open up Chrome -> Developer Tools and click on the Resources tab.

utm-cookies

The cookies utm_source, utm_medium and utm_campaign should be visible in the console.

Setting the Form Field Values

Now that all the UTM parameters are being stored in the lead’s browser, the forms need to be configured to grab the cookie value.

$(document).ready(function(){
 $('input[name=utm_source').val(utm_source);
 $('input[name=utm_medium').val(utm_medium);
 $('input[name=utm_campaign').val(utm_campaign);
});

Putting it All Together

The Code on your page should look similar to this.

// Parse the URL
function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
	var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
		results = regex.exec(location.search);
	return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');

// Set the cookies
if($.cookie('utm_source') == null || $.cookie('utm_source') == "") {
$.cookie('utm_source', source);
}
if($.cookie('utm_medium') == null || $.cookie('utm_medium') == "") {
$.cookie('utm_medium', medium);
}
if($.cookie('utm_campaign') == null || $.cookie('utm_campaign') == "") {
$.cookie('utm_campaign', campaign);
}

// Grab the cookie value and set the form field values
$(document).ready(function(){
	$('input[name=utm_source').val(utm_source);
	$('input[name=utm_medium').val(utm_medium);
	$('input[name=utm_campaign').val(utm_campaign);
});

Questions?

Email me, or send me a tweet @jennamolby.

Author

I'm a Freelance Marketing Operations Consultant With 15 years of experience in Marketing Operations, I’ve worked with a wide range of tools including Salesforce, Marketing Cloud Account Engagement (Pardot), Marketo, and many other sales and marketing platforms. I help teams optimize their tech stacks, improve processes, and get accurate, actionable reporting. Whether it’s setting up your Marketing Automation Platform, building Salesforce reports, managing lead lifecycles, tracking attribution, or integrating your tech stack, I ensure everything is aligned to drive real results.

10 Comments

  1. Hi Jenna !
    I have a Sitecore website that stores the UTM values inside a cookie !
    Is there any chance to capture those values in the Pardot form inside an iframe on this side ?
    Thanks
    Tom

    • Jenna Molby

      Hi Tom, It’s difficult to answer without seeing the website. Usually, there are issues when passing cookies into an iFrame if the iFrame content is hosted on another website or a subdomain.

  2. Jenna- any chance we can get an updated version of this with using the updated plugin js-cookie released?

  3. Thank you very much for the code!

    To fix your issue: You need to pass the cookie value to de input hidden:

    $(‘input[name=utm_source’).val($.cookie(utm_source));

    Cheers,

Write A Comment