thomasbachem / php-gacx Goto Github PK
View Code? Open in Web Editor NEWServer-Side Google Analytics Content Experiments PHP Client
License: GNU Lesser General Public License v3.0
Server-Side Google Analytics Content Experiments PHP Client
License: GNU Lesser General Public License v3.0
HI @thomasbachem! Do you think you'd be able to add a composer.json
, tag a new version with the latest changes and publish to packagist.org?
Thanks!
My CI test webserver is on port 8000 so my tests continually failed but only on this server. The reason is that Firefox doesn't set cookies when there is a port in the domain. I'm not sure what other browsers could be affected.
I didn't want to fork for this simple fix but here it is for Experiments.php (notice the addition of a parse_url call when using $_SERVER):
/**
* @return string
*/
public static function getDomainName() {
if(static::$domainName == 'auto') {
return isset($_SERVER['HTTP_HOST']) ? parse_url(strtolower($_SERVER['HTTP_HOST']), PHP_URL_HOST) : null;
} elseif(static::$domainName) {
return static::$domainName;
} else {
throw new UnexpectedValueException('Unable to determine domain name, please provide one via setDomainName()!');
}
}
use UnitedPrototype\GoogleAnalytics;
$experiment = new GoogleAnalytics\Experiment('xxxxxxxxxxxxxxxxxx');
$variation = $experiment->chooseVariation();
everytime I get different variations, the reason is that $_COOKIE['__utmx'] is unset...
@thomasbachem do you know why? do I have to add more code apart from these 3 lines?? Thanks
I need to run composer update at my hosting so I log in with ssh and try to run comand:
composer update
inside /www folder where I have laravel and composer instalation
but I get error: http://i.stack.imgur.com/FGC01.png
in contact with my hosting provider they tell me to run command:
php -d memory_limit=512M composer update
I run this command but I get: "Could not open file: composer"
What to do? What is the soluton here?
Hi,
Basically it's just a question... Do we need to insert the official Google Analytics Content Experiment javascript code (which is provided when you create a new Experiment in GA) in the view as well? Is that necessary too? Or if I use your code is that enough to send the variation data back to GA?
Thanks!
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.
This caught me off guard in the early hours of this morning, 4 hours of downtime because this library throws a fatal error when the experiment ends and can't be found. Recommended fix is wrapping calls in try/catch
try {
$experiment = new GoogleAnalytics\Experiment('xxxx');
$variation = $experiment->chooseVariation();
} catch(Exception $e) {
$variation = 1;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.