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