Skip to content

Server-Side Google Analytics Content Experiments PHP Client

License

Notifications You must be signed in to change notification settings

hlorofos/php-gacx

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Server-Side GA Content Experiments

While this is a standalone library, you might also be interested in the companion project for general server-side Google Analytics tracking: php-ga.

About

php-gacx is basically Google's cx/api.js in PHP: A server-side implementation that allows you to control and implement Content Experiments entirely on the server.

This is done by parsing experiment data to make use of GA's multi-armed bandit algorithm as well as programmatically modifying the "__utmx" and "__utmxx" cookies.

This does however depend on Google Analytics being used in the browser, see "Usage" below.

This library might become obsolete as soon as Google implements Content Experiments into their Measurement Protocol.

Requirements

Requires PHP 5.3+ as namespaces and closures are used. Has no other dependencies and can be used independantly from any framework or whatsoever environment.

Usage Example

All methods match the ones from the JS API, so using php-gacx is pretty straightforward if you've experience with cx/api.js:

use UnitedPrototype\GoogleAnalytics;

$experiment = new GoogleAnalytics\Experiment('reBreiK2QpOws-pJlkla1o');

$variation = $experiment->chooseVariation();

In order to have the experiment data transferred to Google Analytics, you need to use Google Analytics on the client side either via the traditional ga.js or Google's new analytics.js (Universal Analytics).

ga.js will work out of the box, as it simply consider and include the "__utmx" cookie value when sending tracking data to Google Analytics.

analytics.js does sadly no longer consider the "__utmx" cookie. You will therefore have to tell it via Javascript which variation of an experiment has been chosen, and it's limited to one experiment per page. Example:

// Hand over experiment data to analytics.js, as it ignores the "__utmx" cookie
window.gaData = {
	expId:  '<?= $experiment->getId(); ?>',
	expVar: '<?= $experiment->getChosenVariation(); ?>'
};

The advantage of using php-gacx here is still that you can choose the variation on the server-side while still making use of the multi-armed bandit algorithm considering the variation's different weights.

Disclaimer

Google Analytics is a registered trademark of Google Inc.

About

Server-Side Google Analytics Content Experiments PHP Client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%