Skip to content

Instantly share code, notes, and snippets.

@Garconis
Created March 20, 2023 17:38

Revisions

  1. Garconis created this gist Mar 20, 2023.
    75 changes: 75 additions & 0 deletions gravity-forms-gclid-params-on-submit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    // get parameters of URL
    function getParam(p) {
    var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }
    // set expiration date
    function getExpiryRecord(value) {
    var expiryPeriod = 90 * 24 * 60 * 60 * 1000; // 90 day expiry in milliseconds

    var expiryDate = new Date().getTime() + expiryPeriod;
    return {
    value: value,
    expiryDate: expiryDate
    };
    }
    // get desired param (gclid), specify the form field id to add it to, add it to local storage, and set the value in the form field
    function addGclid() {
    var gclidParam = getParam('gclid'); // what param to get
    var gclidFormFields = ['input_6_16']; // all possible gclid form field ids here, e.g., ['gclid_field', 'foobar']
    var gclidRecord = null;
    var currGclidFormField;

    var gclsrcParam = getParam('gclsrc'); //gclsrc param indicates the source of the click ID, but can also be empty
    var isGclsrcValid = !gclsrcParam || gclsrcParam.indexOf('aw') !== -1;

    gclidFormFields.forEach(function (field) {
    if (document.getElementById(field)) {
    currGclidFormField = document.getElementById(field);
    }
    });

    if (gclidParam && isGclsrcValid) {
    gclidRecord = getExpiryRecord(gclidParam);
    localStorage.setItem('gclid', JSON.stringify(gclidRecord));
    }

    var gclid = gclidRecord || JSON.parse(localStorage.getItem('gclid'));
    var isGclidValid = gclid && new Date().getTime() < gclid.expiryDate;

    if (currGclidFormField && isGclidValid) {
    currGclidFormField.value = gclid.value;
    }
    }
    window.addEventListener('load', addGclid);


    // get desired param (device), which we set via the Google Ads ValueTrack parameters, then specify the form field id to add it to, add it to local storage, and set the value in the form field
    function addAdsDevice() {
    var adsDeviceParam = getParam('device'); // what param to get
    var adsDeviceFormFields = ['input_6_21']; // form field to put the param value
    var adsDeviceRecord = null; // set the local storage record as null to start
    var currAdsDeviceFormField;

    // for each input field we specified
    adsDeviceFormFields.forEach(function (field) {
    if (document.getElementById(field)) {
    currAdsDeviceFormField = document.getElementById(field); // get the desired input field(s)
    }
    });

    // if the parameter existed
    if (adsDeviceParam) {
    adsDeviceRecord = getExpiryRecord(adsDeviceParam); // get the desired expiration date that we specified in our previous function
    localStorage.setItem('device', JSON.stringify(adsDeviceRecord)); // set the local storage item with the name and value, with the desired expiration date
    }

    var device = adsDeviceRecord || JSON.parse(localStorage.getItem('device')); // the value we found or was previously set
    var isAdsDeviceValid = device && new Date().getTime() < device.expiryDate; // variable if param value existed and had a date set previously

    // if we found the form input to add to, and there was a valid param value to send
    if (currAdsDeviceFormField && isAdsDeviceValid) {
    currAdsDeviceFormField.value = device.value; // set the form input value
    }
    }
    window.addEventListener('load', addAdsDevice);