Created
July 7, 2014 11:38
-
-
Save tszym/73bb4673b2e58f1a100c to your computer and use it in GitHub Desktop.
Google Analytics API - Get sessions on time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The first step is the authentication, in an other gist | |
// ... | |
function runDemo($analytics) { | |
try { | |
// Step 2. Get the user's first view (profile) ID. | |
$profileId = getFirstProfileId($analytics); | |
if (isset($profileId)) { | |
// Step 3. Query the Core Reporting API. | |
$results = getResults($analytics, $profileId); | |
// Step 4. Output the results. | |
printResults($results); | |
} | |
} catch (Exception $e) { | |
print 'There was a general error : ' . $e->getMessage(); | |
} | |
} | |
function getFirstprofileId($analytics) { | |
$accounts = $analytics->management_accounts->listManagementAccounts(); | |
if (count($accounts->getItems()) > 0) { | |
$items = $accounts->getItems(); | |
$firstAccountId = $items[0]->getId(); | |
$webproperties = $analytics->management_webproperties | |
->listManagementWebproperties($firstAccountId); | |
if (count($webproperties->getItems()) > 0) { | |
$items = $webproperties->getItems(); | |
$firstWebpropertyId = $items[0]->getId(); | |
$profiles = $analytics->management_profiles | |
->listManagementProfiles($firstAccountId, $firstWebpropertyId); | |
if (count($profiles->getItems()) > 0) { | |
$items = $profiles->getItems(); | |
return $items[0]->getId(); | |
} else { | |
throw new Exception('No views (profiles) found for this user.'); | |
} | |
} else { | |
throw new Exception('No webproperties found for this user.'); | |
} | |
} else { | |
throw new Exception('No accounts found for this user.'); | |
} | |
} | |
function getResults($analytics, $profileId) { | |
$begin_date = isset($_POST['begin']) ? $_POST['begin'] : '31daysAgo'; | |
$end_date = isset($_POST['end']) ? $_POST['end'] : 'yesterday'; | |
return $analytics->data_ga->get( | |
'ga:' . $profileId, | |
$begin_date, | |
$end_date, | |
'ga:sessions', | |
array('dimensions' => 'ga:date') //Aggregation level | |
); | |
} | |
function printResults($results) { | |
if (count($results->getRows()) > 0) { | |
$profileName = $results->getProfileInfo()->getProfileName(); | |
$rows = $results->getRows(); | |
$sessions = $rows[0][0]; | |
//------------ | |
// Fetch data in 2 arrays | |
//------------ | |
$dates = array(); | |
$values = array(); | |
foreach($rows as $row) { | |
$dates[] = $row[0]; | |
$values[] = $row[1]; | |
} | |
require('display.php'); | |
} else { | |
print '<p>No results found.</p>'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Sessions per day from 2014-04-01 to 2014-06-01</title> | |
<script type="text/javascript" src="Chart.min.js"></script> | |
</head> | |
<body> | |
<canvas id="myChart" width="1300" height="250"></canvas> | |
<script type="text/javascript"> | |
var data = { | |
labels: [ | |
<?php for($i=0, $size=count($dates); $i<$size-1; $i++){ | |
echo '"', $dates[$i], '", '; | |
} | |
echo '"', $dates[count($dates)-1], '"'; ?> | |
], | |
datasets: [ | |
{ | |
fillColor : "rgba(151,187,205,0.5)", | |
strokeColor : "rgba(151,187,205,1)", | |
pointColor : "rgba(151,187,205,1)", | |
pointStrokeColor : "#fff", | |
data : [ | |
<?php for($i=0, $size=count($values); $i<$size-1; $i++){ | |
echo $values[$i], ', '; | |
} | |
echo $values[count($values)-1]; ?> | |
] | |
} | |
] | |
}; | |
var options = { bezierCurve: false}; | |
var ctx = document.getElementById("myChart").getContext("2d"); | |
var myNewChart = new Chart(ctx).Line(data, options); | |
</script> | |
<form method="post"> | |
<label for="begin">Begin</label> | |
<input type="text" name="begin" required /> | |
<label for="end">End</label> | |
<input type="text" name="end" required /> | |
<input type="submit" value="Show data" /> | |
<br> | |
Format: 2014-02-28 | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment