-
Notifications
You must be signed in to change notification settings - Fork 9
/
toppostsrealtime.php
executable file
·80 lines (57 loc) · 2.1 KB
/
toppostsrealtime.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once('setup.php');
include_once('functions.php');
createDigest($analytics);
function createDigest(&$analytics) {
try {
// Step 2. Get the user's first view (profile) ID.
$profileId = GOOGLE_ANALYTICS_PROFILE_ID;
if (isset($profileId)) {
$totalActiveUsers = totalActiveUsers($analytics, $profileId);
$pagePaths = pagePath($analytics, $profileId);
$pagePaths = array_reverse($pagePaths);
$topPosts = '';
$count = 0;
for ($count = 0; $count <= 6; $count++) {
if ($pagePaths[$count] != null) {
if ( strcmp($pagePaths[$count][0], '/') !== 0 ) {
$topPosts .= "\n<https://www.google.com/analytics/web/?hl=en#realtime/rt-content/".GOOGLE_ANALYTICS_WEB_ID."/%3Ffilter.list%3D10%3D%3D".urlencode($pagePaths[$count][0])."|".$pagePaths[$count][1].">\t<".YOUR_DOMAIN.$pagePaths[$count][0]."|".str_replace( ".html", "", preg_replace('/\/\d+\/\d+\/\d+\//', '', $pagePaths[$count][0]) ).">";
}
}
}
if ($topPosts == '') {
$message = "*REALTIME*\nCurrently no active users. Share a story!";
} else {
$message = "*REALTIME*\n Active Users:\t *<https://www.google.com/analytics/web/?hl=en#realtime/rt-overview/".GOOGLE_ANALYTICS_WEB_ID."/|$totalActiveUsers>* \n" . $topPosts;
}
// Step 4. Output the results.
slackMessage($message, "#analytics");
}
} catch (apiServiceException $e) {
// Error from the API.
print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();
} catch (Exception $e) {
print 'There was a general error : ' . $e->getMessage();
}
}
function totalActiveUsers(&$analytics, $profileId) {
$result = $analytics->data_realtime->get(
'ga:' . $profileId,
'rt:activeUsers'
);
return $result->rows[0][0];
}
function pagePath(&$analytics, $profileId) {
$result = $analytics->data_realtime->get(
'ga:' . $profileId,
'rt:activeUsers',
array(
'sort' => 'rt:activeUsers',
'dimensions' => 'rt:pagePath'
)
);
return $result->rows;
}
?>