I’m trying to capture the GCLID and MSCKLID values from the URL, and pass them into the appropriate GF form fields. I found a way to pull them from the url, but I’m running into the DRY thing when trying to pass them into their field values.
Code that pulls them from the URL:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
console.log(value);
});
return vars;
}
var gclid = getUrlVars()["gclid"];
var mscklid = getUrlVars()["mscklid"];
Then this is the way I’m trying to pass them into the field values currently:
var selectorGCLID = "input[name='input_25']";
var selectorMSCKLID = "input[name='input_31']";
var fieldGCLID = document.querySelector(selectorGCLID);
var fieldMSCKLID = document.querySelector(selectorMSCKLID);
if(fieldGCLID) { fieldGCLID.value = gclid; }
if(fieldMSCKLID) { fieldMSCKLID.value = mscklid; }
What’s a way where I can just check to see if the field selector is GCLID or MSCKLID, and then pass it to the appropriate field, instead of doing it 1×1 like this?
2
Answers
Can you just use the feature built into Gravity Forms to grab the URL parameter values?
If so, you can just go to the advanced tab of field 25 and click "Allow field to be populated dynamically" and then add: gclid. Go to field 31 and add: mscklid
Was able to capture UTM parameters without any add-on or coding by following these instructions: https://docs.gravityforms.com/using-dynamic-population/.
Added
utm_source
,utm_medium
,utm_campaign
as hidden fields and checked "Allow field to be populated dynamically" in the field settings.