Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show all A/B experiments and the winner for your Google Analytics content experiments
<?php
# Composer: "google/apiclient": "^2.0"
const CREDENTIALS_FILE = '/var/www/vhosts/library/Dashboard-6403d6a8a5b6.json';
function googleGetExperiments()
{
return cache(__FUNCTION__, func_get_args(), 3600*6, function() {
$scopes = [ 'https://www.googleapis.com/auth/analytics.readonly' ];
$client = new Google_Client();
$client->setAuthConfig(CREDENTIALS_FILE);
$client->setScopes($scopes);
$service = new Google_Service_Analytics($client);
$experimentData = array();
$profiles = $service->management_profiles->listManagementProfiles("~all", "~all");
foreach ($profiles['items'] as $profile) {
$experiments = $service->management_experiments->listManagementExperiments($profile['accountId'], $profile['webPropertyId'], $profile['id']);
if (isset($experiments['items']))
foreach ($experiments['items'] as $experiment) {
// if ($experiment['status'] != 'RUNNING') continue;
$retval[] = $experiment;
}
}
return $retval;
});
}
$experimentData = googleGetExperiments();
$progressClasses = ['progress-bar bg-success','progress-bar bg-info','progress-bar bg-warning', 'progress-bar bg-danger','progress-bar bg-success','progress-bar bg-info','progress-bar bg-warning', 'progress-bar bg-danger', 'progress-bar bg-success','progress-bar bg-info','progress-bar bg-warning', 'progress-bar bg-danger','progress-bar bg-success','progress-bar bg-info','progress-bar bg-warning', 'progress-bar bg-danger'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<title>PMT Team Page</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<span class="navbar-brand">
<a href="/" class="text-dark">
<img src="/pmt-logo-square-128px.png" alt="logo" width="30" class="d-inline-block align-top" height="30">
Pacific Medical Training
</a>
<strong>Management Reports</strong>
</span>
<span class="ml-auto nav-text"><i class="fa fa-user"></i> <?= htmlspecialchars($visitorEmail) ?></span>
</nav>
<div class="container mt-3">
<table class="table lead">
<tr><th>Experiment<th>Page<th>Started<th>Status<th>Confidence
<?php
function sortDate($a, $b)
{
return strcmp($b['startTime'], $a['startTime']);
}
usort($experimentData, 'PMTDashboards\sortDate');
foreach ($experimentData as $i => $experiment) {
if ($experiment['status'] != 'RUNNING' && $i > 5) continue;
echo "<tr>";
if ($experiment['status'] == 'RUNNING')
echo '<td><a class="btn btn-xs btn-success"><i class="glyphicon glyphicon-ok"></i></a>';
else
echo '<td><a class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-remove"></i></a>';
$expHref = "https://www.google.com/analytics/web/?pli=1#siteopt-experiment/siteopt-detail/a{$experiment['accountId']}w{$experiment['internalWebPropertyId']}p{$experiment['profileId']}/%3F_r.drilldown%3Danalytics.gwoExperimentId%3A{$experiment['id']}/";
echo " <a href='$expHref' target='_blank'>{$experiment['name']}</a>";
echo "<td>{$experiment['variations'][0]['url']}";
echo "<td>".date('Y-m-d',strtotime($experiment['startTime']));
echo "<td>";
echo '<div class="progress" style="width:200px">';
$max = null;
foreach ($experiment['variations'] as $i => $variation) {
if ($max == null || $variation['weight'] > $max) $max = $variation['weight'];
echo '<div class="'.$progressClasses[$i].'" role="progress-bar" style="width: '.(100*$variation['weight']).'%" title="'.$variation['name'].'"></div>';
}
echo '</div>';
echo "<td>" . round(100*$max, 1) . '%';
}
?>
</table>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.