Many Hatchbuck users ask us if it's possible to capture a lead source when a form is submitted. The term Lead Source means different things to different people, but for most use cases, the referring page's URL is enough. Luckily, this can be captured using a simple script and a bit of configuration.
The first thing to do is to create a custom field to house the data. In this example, I named the custom field "Lead Source."
Next, this custom field needs to exist on your form. Don't worry, we'll hide it from view later and automatically populate it with the correct data. Just create a form and use the Custom Textbox option to add a field and hook it up to your newly-created custom field.
Now that the custom field is made and added to your form, we need to make sure to embed the form with either the HTML/CSS or Plain HTML options. This is because we need to modify the code a bit, and we will be unable to do so if the link or iframe options are used.
I cleaned up the code a bit for readability here and put it on a blank HTML page. Here's what the default code looks like.
Now for the magic. First, take a look at line 18. This is the input for the custom field we created. Instead of a type of text, we want to change it to hidden so it's not visible to visitors. Likewise, you'll want to delete line 17, which is just the label for the input. Also, take note of the ID value for this input. In this example, the ID is "input_5". We will need to use this in the script.
The final step is to modify this script and put it at the bottom of the HTML body:
document.getElementById('input_5').value = document.referrer;
If your custom field has an ID that differs from "input_5," you will need to change the above script to match it. What this script does is find an element with the ID of "input_5" and set the value property to the referral URL. That means if someone clicked a link on http://hatchbuck.com/partners that led them to your site, this now-hidden input field will be given a value of "http://hatchbuck.com/partners", which will be pushed into your custom field when the form is submitted.
Here's a look at the final code: