1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Analytics; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
use Google_Client; |
8
|
|
|
use Google_Service_Analytics; |
9
|
|
|
|
10
|
|
|
class GoogleClient |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Google_Service_Analytics |
14
|
|
|
*/ |
15
|
|
|
protected $service; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Spatie\Analytics\Cache |
19
|
|
|
*/ |
20
|
|
|
protected $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $cacheLifeTimeInMinutes; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $realTimeCacheLifeTimeInSeconds; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Google_Client $client |
34
|
|
|
* @param \Spatie\Analytics\Cache $cache |
35
|
|
|
* @param int $cacheLifeTimeInMinutes |
36
|
|
|
* @param int $realTimeCacheLifeTimeInSeconds |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
Google_Client $client, |
40
|
|
|
Cache $cache = null, |
41
|
|
|
$cacheLifeTimeInMinutes = 0, |
42
|
|
|
$realTimeCacheLifeTimeInSeconds = 0 |
43
|
|
|
) { |
44
|
|
|
$this->service = new Google_Service_Analytics($client); |
45
|
|
|
$this->cache = $cache; |
46
|
|
|
|
47
|
|
|
$this->cacheLifeTimeInMinutes = $cacheLifeTimeInMinutes; |
48
|
|
|
$this->realTimeCacheLifeTimeInSeconds = $realTimeCacheLifeTimeInSeconds; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Query the Google Analytics Service with given parameters. |
53
|
|
|
* |
54
|
|
|
* @param int $id |
55
|
|
|
* @param string $startDate |
56
|
|
|
* @param string $endDate |
57
|
|
|
* @param string $metrics |
58
|
|
|
* @param array $others |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function performQuery($id, $startDate, $endDate, $metrics, array $others = array()) |
62
|
|
|
{ |
63
|
|
|
$cacheName = $this->determineCacheName(func_get_args()); |
64
|
|
|
|
65
|
|
|
if ($this->useCache() && $this->cache->has($cacheName)) { |
66
|
|
|
return $this->cache->get($cacheName); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$googleAnswer = $this->service->data_ga->get($id, $startDate, $endDate, $metrics, $others); |
70
|
|
|
|
71
|
|
|
if ($this->useCache()) { |
72
|
|
|
$this->cache->put($cacheName, $googleAnswer, $this->cacheLifeTimeInMinutes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $googleAnswer; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Query the Google Analytics Real Time Reporting Service with given parameters. |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* @param string $metrics |
83
|
|
|
* @param array $others |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function performRealTimeQuery($id, $metrics, array $others = array()) |
87
|
|
|
{ |
88
|
|
|
$realTimeCacheName = $this->determineRealTimeCacheName(func_get_args()); |
89
|
|
|
|
90
|
|
|
if ($this->useRealTimeCache() && $this->cache->has($realTimeCacheName)) { |
91
|
|
|
return $this->cache->get($realTimeCacheName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$googleAnswer = $this->service->data_realtime->get($id, $metrics, $others); |
95
|
|
|
|
96
|
|
|
if ($this->useRealTimeCache()) { |
97
|
|
|
$this->cache->put( |
98
|
|
|
$realTimeCacheName, |
99
|
|
|
$googleAnswer, |
100
|
|
|
Carbon::now()->addSeconds($this->realTimeCacheLifeTimeInSeconds) |
|
|
|
|
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $googleAnswer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a site Id by its URL. |
109
|
|
|
* |
110
|
|
|
* @param string $url |
111
|
|
|
* @return mixed |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
|
|
public function getSiteIdByUrl($url) |
115
|
|
|
{ |
116
|
|
|
$siteIds = $this->getAllSiteIds(); |
117
|
|
|
|
118
|
|
|
if (isset($siteIds[$url])) { |
119
|
|
|
return $siteIds[$url]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw new Exception("Site $url is not present in your Analytics account."); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get all siteIds. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function getAllSiteIds() |
131
|
|
|
{ |
132
|
|
|
static $siteIds = null; |
133
|
|
|
|
134
|
|
|
if (! is_null($siteIds)) { |
135
|
|
|
return $siteIds; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
foreach ($this->service->management_profiles->listManagementProfiles("~all", "~all") as $site) { |
139
|
|
|
$siteIds[$site['websiteUrl']] = 'ga:'.$site['id']; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $siteIds; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Determine the cache name for the set of query properties given. |
147
|
|
|
* |
148
|
|
|
* @param array $properties |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function determineCacheName(array $properties) |
152
|
|
|
{ |
153
|
|
|
return 'spatie.laravel-analytics.'.md5(serialize($properties)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determine if request to Google should be cached. |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
protected function useCache() |
162
|
|
|
{ |
163
|
|
|
return isset($this->cache) && $this->cacheLifeTimeInMinutes > 0; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the cache time. |
168
|
|
|
* |
169
|
|
|
* @param int $cacheLifeTimeInMinutes |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function setCacheLifeTimeInMinutes($cacheLifeTimeInMinutes) |
173
|
|
|
{ |
174
|
|
|
$this->cacheLifeTimeInMinutes = $cacheLifeTimeInMinutes; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Determine the cache name for RealTime function calls for the set of query properties given. |
181
|
|
|
* |
182
|
|
|
* @param array $properties |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function determineRealTimeCacheName(array $properties) |
186
|
|
|
{ |
187
|
|
|
return 'spatie.laravel-analytics.RealTime.'.md5(serialize($properties)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Determine if RealTime request to Google should be cached. |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
protected function useRealTimeCache() |
196
|
|
|
{ |
197
|
|
|
return isset($this->cache) && $this->realTimeCacheLifeTimeInSeconds > 0; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the cache time. |
202
|
|
|
* |
203
|
|
|
* @param int $realTimeCacheLifeTimeInSeconds |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
|
|
public function setRealTimeCacheLifeTimeInMinutes($realTimeCacheLifeTimeInSeconds) |
207
|
|
|
{ |
208
|
|
|
$this->realTimeCacheLifeTimeInSeconds = $realTimeCacheLifeTimeInSeconds; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: