1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Analytics; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Google_Service_Analytics; |
7
|
|
|
use Illuminate\Contracts\Cache\Repository; |
8
|
|
|
|
9
|
|
|
class AnalyticsClient |
10
|
|
|
{ |
11
|
|
|
/** @var \Google_Service_Analytics */ |
12
|
|
|
protected $service; |
13
|
|
|
|
14
|
|
|
/** @var \Illuminate\Contracts\Cache\Repository */ |
15
|
|
|
protected $cache; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $cacheLifeTimeInMinutes = 0; |
19
|
|
|
|
20
|
|
|
public function __construct(Google_Service_Analytics $service, Repository $cache) |
21
|
|
|
{ |
22
|
|
|
$this->service = $service; |
23
|
|
|
|
24
|
|
|
$this->cache = $cache; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the cache time. |
29
|
|
|
* |
30
|
|
|
* @param int $cacheLifeTimeInMinutes |
31
|
|
|
* |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
|
|
public function setCacheLifeTimeInMinutes(int $cacheLifeTimeInMinutes) |
35
|
|
|
{ |
36
|
|
|
$this->cacheLifeTimeInMinutes = $cacheLifeTimeInMinutes * 60; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Query the Google Analytics Service with given parameters. |
43
|
|
|
* |
44
|
|
|
* @param string $viewId |
45
|
|
|
* @param \DateTime $startDate |
46
|
|
|
* @param \DateTime $endDate |
47
|
|
|
* @param string $metrics |
48
|
|
|
* @param array $others |
49
|
|
|
* |
50
|
|
|
* @return array|null |
51
|
|
|
*/ |
52
|
|
|
public function performQuery(string $viewId, DateTime $startDate, DateTime $endDate, string $metrics, array $others = []) |
53
|
|
|
{ |
54
|
|
|
$cacheName = $this->determineCacheName(func_get_args()); |
55
|
|
|
|
56
|
|
|
if ($this->cacheLifeTimeInMinutes == 0) { |
57
|
|
|
$this->cache->forget($cacheName); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->cache->remember($cacheName, $this->cacheLifeTimeInMinutes, function () use ($viewId, $startDate, $endDate, $metrics, $others) { |
61
|
|
|
$result = $this->service->data_ga->get( |
62
|
|
|
"ga:{$viewId}", |
63
|
|
|
$startDate->format('Y-m-d'), |
64
|
|
|
$endDate->format('Y-m-d'), |
65
|
|
|
$metrics, |
66
|
|
|
$others |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
while ($nextLink = $result->getNextLink()) { |
70
|
|
|
if (isset($others['max-results']) && count($result->rows) >= $others['max-results']) { |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$options = []; |
75
|
|
|
|
76
|
|
|
parse_str(substr($nextLink, strpos($nextLink, '?') + 1), $options); |
77
|
|
|
|
78
|
|
|
$response = $this->service->data_ga->call('get', [$options], 'Google_Service_Analytics_GaData'); |
79
|
|
|
|
80
|
|
|
if ($response->rows) { |
81
|
|
|
$result->rows = array_merge($result->rows, $response->rows); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$result->nextLink = $response->nextLink; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getAnalyticsService(): Google_Service_Analytics |
92
|
|
|
{ |
93
|
|
|
return $this->service; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* |
97
|
|
|
* Determine the cache name for the set of query properties given. |
98
|
|
|
*/ |
99
|
|
|
protected function determineCacheName(array $properties): string |
100
|
|
|
{ |
101
|
|
|
return 'spatie.laravel-analytics.'.md5(serialize($properties)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|