Skip to content

Instantly share code, notes, and snippets.

@elconejito
Created November 1, 2017 16:11
Show Gist options
  • Save elconejito/5af29b32844238920a1accc5e54b6e52 to your computer and use it in GitHub Desktop.
Save elconejito/5af29b32844238920a1accc5e54b6e52 to your computer and use it in GitHub Desktop.
Gravity Forms - Fill in hidden fields with data from URL Query Parameters such as UTM_*
/**
* This function parses the URL for a given parameter and returns the value of that parameter
*/
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
/**
* Enter all the parameters you want to capture from the URL into the array.
*/
var urlParamsToForward = [
'email',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_source'
];
/**
* Loop through the array of query parameters you want, Save the results into a hidden field with a VALUE that matches
* the name of the parameter. This should be setup properly in GravityForms.
*/
jQuery.each(urlParamsToForward, function(i, param) {
var selector = 'input[value="' + param + '"]';
jQuery(selector).val(getUrlParameter(param));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment