简体   繁体   English

Google Analytics内容实验A / B测试没有页面刷新的服务器端代码

[英]Google Analytics Content Experiments A/B testing server-side code without page refresh

Basically, we want to A/B test 2 different page layout headers. 基本上,我们希望A / B测试2个不同的页面布局标题。 There are some structural differences (it's not just switching out the CSS). 存在一些结构差异(它不仅仅是切换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. 如果我没有从head标签中省略它,那么它生成的UTMX cookie看起来与Google提供的javascript会生成的cookie相同。

Server-side PHP code: 服务器端PHP代码:

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? 为什么我的Google Analytics内容实验信息中心没有显示我的实验的所有访问者呢? Did I set the utmx cookie imperfectly? 我是否将utmx cookie设置得不完美? Other than setting the UTMX cookie, is GACE looking for anything else? 除了设置UTMX cookie之外,GACE还在寻找其他什么吗?

We've been using a totally different approach for the past couple months: Amazon load-balancers (AWS ELB) plus Google Analytics (not Content Experiments). 在过去的几个月里,我们一直在使用完全不同的方法:亚马逊负载均衡器(AWS ELB)和Google Analytics(不是内容实验)。 (See my comment above.) As we hoped, it has greatly improved our experience with merging back to trunk. (参见上面的评论。)正如我们所希望的那样,它大大改善了我们合并回主干的经验。

_gaq.push(['_setCustomVar', 2, varName, varValue, 2]);//https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
_gaq.push(['_trackPageview']);//This must come AFTER the setCustomVar
//varName should be whatever you want to call the experiment
//varValue should be something like "original" for the original and "variation" for the variation.  We just use "trunk" and [name of variation branch].

Obvious drawbacks are that Google doesn't do the math for us (telling us whether the variation has statistically significantly outperformed the original) and that we can't easily run multiple experiments at once. 显而易见的缺点是谷歌没有为我们做数学计算(告诉我们变异在统计上是否明显优于原始数据),并且我们不能轻易地同时进行多个实验。 We also wouldn't be able to have many variations (we'd need to add more load-balanced instances than we'd want). 我们也不能有很多变化(我们需要添加比我们想要的更多负载平衡的实例)。

But for our purposes (eg given how important it is to us not to have a page refresh), it has worked better than other approaches. 但是出于我们的目的(例如,考虑到我们不重新刷页的重要性),它比其他方法更有效。

@danmaz74 took an interesting approach to this using only Google Analytics on the client side as well: @ danmaz74在客户端使用Google Analytics也采用了一种有趣的方法:

https://github.com/danmaz74/ABalytics https://github.com/danmaz74/ABalytics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM