-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrt-visitors.php
More file actions
115 lines (93 loc) · 3.09 KB
/
rt-visitors.php
File metadata and controls
115 lines (93 loc) · 3.09 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
use Stash\Interfaces\PoolInterface;
use Stash\Invalidation;
require_once 'vendor/autoload.php';
$config = require 'config/config.php';
/**
* @return Google_Client
*/
$googleClientFactory = function() use ($config) {
$client = new Google_Client();
$client->setAuthConfig($config["authConfig"]);
if (array_key_exists("cacert", $config)) {
// fix of the unknown certs
// https://github.com/google/google-api-php-client/issues/1011
$client->setHttpClient(new \GuzzleHttp\Client(array(
'verify' => $config["cacert"],
)));
}
return $client;
};
/**
* @param $clientFactory
* @return Google_Service_Analytics
*/
$googleServiceAnalyticsFactory = function($clientFactory) {
$client = $clientFactory();
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
return new Google_Service_Analytics($client);
};
/**
* @param Google_Service_Analytics $service
* @param $profileId
* @return array
*/
$getRealTimeActiveUsers = function (Google_Service_Analytics $service, $profileId) {
/* @var $result Google_Service_Analytics_RealtimeData */
$result = $service->data_realtime->get(
'ga:' . $profileId,
'rt:activeUsers'
);
$totals = $result->getTotalsForAllResults();
return [
'time' => time(),
'result' => (int) $totals['rt:activeUsers'],
];
};
/**
* @param string $cacheManagerName
* @return PoolInterface
*/
$cacheFactory = function ($cacheManagerName = "default") use ($config) {
$cacheManagerConfig = $config['cache_pool'][$cacheManagerName];
$driverName = $cacheManagerConfig['driver'];
$driverOptions = $cacheManagerConfig['options'];
$driver = new $driverName($driverOptions);
return new Stash\Pool($driver);
};
/**
* @param callable $callback
* @param PoolInterface $cache
* @param $cacheKey
* @param $ttl
*
* @return mixed
*/
$runCached = function (callable $callback, PoolInterface $cache, $cacheKey, $ttl) {
$item = $cache->getItem($cacheKey);
$item->setInvalidationMethod(Invalidation::OLD);
if (!$item->isHit()) {
$item->lock();
$result = $callback();
$item->setTTL($ttl);
$item->set($result);
$cache->save($item);
}
else {
$result = $item->get();
}
return $result;
};
$profileId = filter_input(INPUT_GET, "profileId", FILTER_SANITIZE_NUMBER_INT);
$getRealTimeActiveUsersCached = function ($profileId) use ($runCached, $getRealTimeActiveUsers, $googleClientFactory, $googleServiceAnalyticsFactory, $cacheFactory, $config) {
$cache = $cacheFactory();
$cacheKey = "ga-rt/activeUsers/" . $profileId;
$ttl = $config['cache']['ttl'];
return $runCached(function() use ($profileId, $getRealTimeActiveUsers, $googleClientFactory, $googleServiceAnalyticsFactory) {
$service = $googleServiceAnalyticsFactory($googleClientFactory);
return $getRealTimeActiveUsers($service, $profileId);
}, $cache, $cacheKey, $ttl);
};
$result = $getRealTimeActiveUsersCached($profileId);
header("Last-Modified: " . date("r", $result['time']));
echo $result['result'];