我尝试使用此库在服务器端执行GA内容实验:https://github.com/thomasbachem/php-gacx
但它似乎失去了网站上发生的大部分转化(实验页面并未显示GA所做的所有转化)。
以下是代码:
<?php
include('php-gacx/autoload.php');
use UnitedPrototype\GoogleAnalytics;
$experiment = new GoogleAnalytics\Experiment('xxxxxxxxxxx');
$variation = $experiment->chooseVariation();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxx-x', 'auto');
ga('send', 'pageview');
</script>
<script>
window.gaData = {
expId: '<?= $experiment->getId(); ?>',
expVar: '<?= $experiment->getChosenVariation(); ?>'
};
</script>
<!-- the rest of the page -->