php-gacx
php-gacx copied to clipboard
Conversions are lost
I tried to use php-gacx to perform GA content experiments on the server-side.
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).
At the same time, the engine for choosing the experiment variation works fine. It shows different versions of the page in different browsers, so that works just fine.
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 -->
Would really appreciaty any comments / suggestions.
Move the second script before the first. GA sends expId
& expVar
with the pageview
, so you need to set them before send
ing the pageview
.
Here is (a snippet of) the approach I'm using: Change these two lines:
ga('create', 'UA-xxxxxx-x', 'auto');
ga('send', 'pageview');
To:
ga('create', 'UA-xxxxxx-x', 'auto');
ga('set', 'expId', '<?= $experiment->getId(); ?>');
ga('set', 'expVar', '<?= $experiment->getChosenVariation(); ?>');
ga('send', 'pageview');
This Chrome extension is helpful, you can see exactly what is being sent (also possible just by looking in Chrome DevTools, but easier to parse with the extension): https://chrome.google.com/webstore/detail/tag-assistant-by-google/kejbdjndbnbjgmefkgdddjlbokphdefk?hl=en