Server-side Google Analytics experiments with PHP

306 Views Asked by At

I tried to use this library to perform GA content experiments on the server-side: https://github.com/thomasbachem/php-gacx

But it seems to lose the majority of the conversions that happen on the site (the experiments page doesn't show all the conversions that GA does).

Here is the code:

 <?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 -->
0

There are 0 best solutions below