We're using php to set some cookies for our users based on where they came from - some of these will be set based on the referrer URL, and some will be set based on a short query string in the URL.
We only set 2 cookies, and the purpose is to track where traffic is coming from and include the data in the user's submission for our product - then we are able to track which submissions come from which campaign.
It's a custom wordpress website, so the code is split across a few different files as follows:
header.php:
<?php
$origin = $_SERVER['HTTP_REFERER'];
$current = $_SERVER['PHP_SELF'];
$bestbefore = time() + 60 * 60 * 24 * 7;
if (isset($_COOKIE['ccsvissource']))
{
}
else
{
if (isset($_GET[gclid]))
{
setcookie('ccsvissource', 'Google', $bestbefore);
setcookie('ccsvismedium', 'Adwords', $bestbefore);
}
elseif (stripos($origin, 'facebook') !== false)
{
setcookie('ccsvissource', 'Advertising', $bestbefore);
setcookie('ccsvismedium', 'Facebook', $bestbefore);
}
elseif ($_GET[utm_medium] == "111")
{
setcookie('ccsvissource', 'Advertising', $bestbefore);
setcookie('ccsvismedium', 'emailcampaign', $bestbefore);
}
else
{
setcookie('ccsvissource', $origin, $bestbefore);
setcookie('ccsvismedium', 'Unknown', $bestbefore);
}
?>
footer.php:
<?php
$mktSource = $_COOKIE['ccsvissource'];
$mktMedium = $_COOKIE['ccsvismedium'];
?>
<p class="hide-me" id="mkt-source"><?php echo $mktSource; ?></p>
<p class="hide-me" id="mkt-medium"><?php echo $mktMedium; ?></p>
<script type="text/javascript">
$(document).ready(function(){
var mktSource = $('#mkt-source').text();
var mktMedium = $('#mkt-medium').text();
$('#hid-ms').val(mktSource);
$('#hid-mm').val(mktMedium);
});
</script>
The values will then get passed into the user's application form, with our lead management system doing the rest.
The problem is inconsistency - sometimes the cooks are being set, and sometimes not. Is there a certain circumstance client-side which will prevent cookies from being set?
I understand the user may have cookie tracking switched off, and have accounted for this - the volume of empty cookies is still too high so I think I'm missing something.
Please let me know if you need any further information!
Many thanks.