How to Use Cookies to Capture URL Parameters

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.

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.

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

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.

Putting it All Together

The Code on your page should look similar to this.

Questions?

Email me, or send me a tweet @jennamolby.