The first step to this solution was to add jquery.cookie.js to my WordPress theme files. I am using a child theme of Astra and inserted the jquery file into my theme via Cpanel / File Manager. (not shown in this post)
The current version of the code can be found on GitHub here –> https://github.com/js-cookie/js-cookie. The version I used in this project was is slightly older, as the first time I used this code was in 2017. The most recent summit I used this code for was the Expand Online Summit which I hosted in 2020, 2021 and 2022. That is the site where the screenshots and excerpts are pulled from.
I inserted this code onto my homepage to pull the affiliate ID from the url (param = affiliate) and store it in the cookie called first-aff. For this summit, I used first affiliate, so I also checked to make sure that we didn’t already have an affiliate stored before storing the new affiliate in the cookie.
jQuery( document ).ready(function() {
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
$cur_affiliate = getUrlParameter('affiliate');
$savedRef = jQuery.cookie('first-aff');
if ($savedRef) {
if ($cur_affiliate) {
if ($savedRef === 'no-affiliate')
jQuery.cookie('first-aff', $cur_affiliate, { expires: 30, path: '/' });
}
} else {
if ($cur_affiliate) {
jQuery.cookie('first-aff', $cur_affiliate, { expires: 30, path: '/' });
} else {
jQuery.cookie('first-aff', 'no-affiliate', { expires: 1, path: '/' });
}
}
});
Next, I created the Gravity Forms form
The Credited Affiliate field is a Hidedn field that is populated by the variable aff_id. So how do I get the affiliate information into the aff_id? I use a filter action in my theme’s function.php
// This filter pulls the affiliate id from the cookie and populates it on the registration form
// This assures us that we are tracking first click
add_filter( 'gform_field_value_aff_id', 'aff_id_dynamic_population' );
function aff_id_dynamic_population( $value ) {
$cookie_name = 'first-aff';
if(!isset($_COOKIE[$cookie_name])) {
return '0';
} else {
return $_COOKIE[$cookie_name];
}
}
To alter any gravity forms field data programmatically, you add a filter that starts with gform_field_value_ followed by your parameter name. This filter is super siple, if the cookie is not present, we load a value of 0 otherwise we load the affiliate ID value. I could have not returned a value, but decided that I wanted definitive proof that no cookie was stored.
Now, I was also insanely curious how many “last cookie” were different than “first cookie”, so I also loaded the affiliate directly from the url, if present into a second hidden field. This was mostly for research purposes 🙂
Next, I connected the form to ActiveCampaign, using the Gravity Forms ActiveCampaign Add-On. I set my custom fields (for the affiliate ids) in ActiveCampaign before I connected Gravity Forms to Active Campaign.