Basically, we want to A/B test 2 different page layout headers. There are some structural differences (it's not just switching out the CSS). We also don't want to wait for Google to flip the coin to determine which variation the visitor should see; instead, we want to choose the variation server-side and avoid a page redirect.
The code below does what I hoped it would; the UTMX cookie it generates looks identical to the one that the Google-supplied javascript would generate if I didn't omit it from the head tag.
Server-side PHP code:
public function setUtmxCookie($cookieName, $experimentsString)
{
$domainHash = $this->getDomainHash($_SERVER['SERVER_NAME']);
$cookieVal = $domainHash . $experimentsString;
$expire = time() + 60 * 60 * 24 * 30;
$domain = '.' . $_SERVER['SERVER_NAME'];
setrawcookie($cookieName, $cookieVal, $expire, '/', $domain);
}
private function getExperimentsFromUtmxCookie($cookieName)
{
if (isset($_COOKIE[$cookieName])) {
$cookieVal = $_COOKIE[$cookieName];
$experimentsArray = array();
$experimentMatches = preg_split('/\./', $cookieVal);
$domainHash = array_shift($experimentMatches); //remove the first item. All that will remain in $experimentMatches is an array of experimentIds with their combos.
foreach ($experimentMatches as $m) {
$segments = preg_split('/:/', $m);
$experimentsArray[$segments[0]] = $segments[1];
}
return $experimentsArray;
}
return array();
}
private function getExperimentsString($cookieName, $experimentId, $variation)
{
$experiments = $this->getExperimentsFromUtmxCookie($cookieName);
$experiments[$experimentId] = $variation;
$experimentsString = '';
foreach ($experiments as $key => $val) {
$experimentsString .= '.' . $key . ':' . $val;
}
return $experimentsString;
}
Why isn't my Google Analytics Content Experiments dashboard showing any visitors to my experiment, then? Did I set the utmx cookie imperfectly? Other than setting the UTMX cookie, is GACE looking for anything else?