forked from innocraft/php-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking.php
32 lines (26 loc) · 1.66 KB
/
tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/**
* This is mainly useful for users of A/B Testing for Piwik (www.ab-tests.net). It shows you how to send
* a tracking request form your server to your Piwik letting it know that you just activated a variation
* for a user.
*/
date_default_timezone_set('utc');
use InnoCraft\Experiments\Experiment;
include_once '../vendor/autoload.php';
// gives you a JavaScript tracking snippet that you can print in your HTML website to let Piwik know
// that you just activated a specific variation. This is useful when you track your visitors via Piwik
// with the regular JavaScript tracking code and just activated an experiment server side.
$experiment = new Experiment('myExperimentName', [['name' => 'myVariationName']]);
$activatedVariation = $experiment->getActivatedVariation();
// important: you should escape the passed experiment name and variation name if needed to prevent XSS.
$script = $experiment->getTrackingScript($experiment->getExperimentName(), $activatedVariation->getName());
echo $script; // prints eg "<script>_paq.push(['AbTesting::enter', {...}])"
// This is useful when you track your users server side via the PiwikTracker (https://github.com/piwik/piwik-php-tracker)
// and just activated an experiment server side. It will make sure to call the correct PiwikTracker method to let Piwik
// know you just activated an experiment.
if (class_exists('\PiwikTracker')) {
$tracker = new \PiwikTracker($idSite = 1, $apiUrl = 'https://piwik.example.com');
$experiment = new Experiment('myExperimentName', [['name' => 'myVariationName']]);
$activatedVariation = $experiment->getActivatedVariation();
$experiment->trackVariationActivation($tracker);
}