Hope someone can help me : )
What Im trying to do is, get the first visit referrer and send it to form. We user Contact form 7 plugin, so I created a hidden input, get the referrer URL and assign that value of referrer to that hidden input field.
Everything was working fine, till I enabled the caching.
This is the function I use (in case anyone will need it)
function getRefererPage( $form_tag ){
if(!isset($_COOKIE['external_referrer_cookie']))
{
}
if ( $form_tag['name'] == 'referer-page' ) {
$form_tag['values'][] = $_COOKIE['external_referrer_cookie'];
}
return $form_tag;
}
add_filter( 'wpcf7_form_tag', 'getRefererPage' );
Where "referer-page" is the name and ID of the hidden field in Contact Form 7 form.
Once I enabled cache, I had to think of a Javascript of jQuery solution to set the cookie.
I added jQuery Cookie Plugin (https://github.com/carhartl/jquery-cookie) and added then tried multiple different variations of the code below to create a cookie and set the cookie value.
My Goal is: When website visitor comes to first time, set the cookie value to the referrer url, but then keep the value, for example if visitor comes to website from google.com and then moves around the pages on website, once he fills the form, the value in the hidden field still should be the google url with its parameters.
This is the JS code I have in the footer.php of theme now.
jQuery(document).ready(function($){ // BEGIN CUSTOM JQUERY NO CONFLICT
// $.cookie('mysite_referrer', document.referrer);
// var initreferrer = document.referrer;
// $.cookie('mysite_referrer', initreferrer);
//console.log('current cookie ref:'+$.cookie('external_referrer_cookie'));
//console.log('testing');
$.cookie('external_referrer_cookie');
if ($.cookie('external_referrer_cookie') == 'undefined' ) {
// console.log('setting cookie for'+ document.referrer);
$.cookie('external_referrer_cookie', document.referrer);
$('.wpcf7-text.referer-page').val($.cookie('wsg_external_referrer_cookie'));
console.log('current cookie ref:'+$.cookie('wsg_external_referrer_cookie'));
}
// $('#referer-page').val($.cookie('external_referrer_cookie'));
console.log('current cookie out of function ref:'+$.cookie('external_referrer_cookie'));
/*
var cookieName = 'cookietest';
$(function() {
checkCookie();
});
function checkCookie() {
if (document.cookie.length > 0 && document.cookie.indexOf(cookieName + '=') != -1) {
// do nothing, cookie already sent
} else {
// handle jQuery animation
// set the cookie to show user has already visited
document.cookie = cookieName + "=1";
}
}
*/
}); // END CUSTOM JQUERY NO CONFLICT
As you can see in commented out lines, I tried lots of approaches I found in several website tutorials. Ive been struggling on this for couple days now, please help : ))
Thank You