|
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; |
|
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 ($resource, $viewId, $startDate, $endDate, $metrics, $others) { |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $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
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getAnalyticsService(): Google_Service_Analytics |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->service; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/* |
|
78
|
|
|
* Determine the cache name for the set of query properties given. |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function determineCacheName(array $properties): string |
|
81
|
|
|
{ |
|
82
|
|
|
return 'spatie.laravel-analytics.'.md5(serialize($properties)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.