-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathAnalytics.php
More file actions
137 lines (108 loc) · 3.39 KB
/
Copy pathAnalytics.php
File metadata and controls
137 lines (108 loc) · 3.39 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Thujohn\Analytics;
class Analytics
{
protected $client;
protected $service;
private $site_ids = [];
public function __construct(\Google_Client $client)
{
$this->setClient($client);
$this->setService($client);
}
public function getClient()
{
return $this->client;
}
public function setClient(\Google_Client $client)
{
$this->client = $client;
return $this;
}
public function getService()
{
return $this->service;
}
public function setService(\Google_Client $client)
{
$this->service = new \Google_Service_Analytics($client);
return $this;
}
public function query($id, $start_date, $end_date, $metrics, $others = [])
{
return $this->service->data_ga->get($id, $start_date, $end_date, $metrics, $others);
}
/**
* Runs analytics query calls in batch mode
* It accepts an array of queries as specified by the parameters of the Analytics::query function
* With an additional optional parameter named key, which is used to identify the results for a specific object.
*
* Returns an array with object keys as response-KEY where KEY is the key you specified or a random key returned
* from analytics.
*
* @param array $queries
*
* @return array|null
*/
public function batchQueries(array $queries)
{
/*
* Set the client to use batch mode
* When batch mode is activated calls to Analytics::query will return
* the request object instead of the resulting data
*/
$this->client->setUseBatch(true);
$batch = new \Google_Http_Batch($this->client);
foreach ($queries as $query) {
// pull the key from the array if specified so we can later identify our result
$key = array_pull($query, 'key');
// call the original query method to get the request object
$req = call_user_func_array(__NAMESPACE__.'\Analytics::query', $query);
$batch->add($req, $key);
}
$results = $batch->execute();
// Set the client back to normal mode
$this->client->setUseBatch(false);
return $results;
}
public function segments()
{
return $this->service->management_segments;
}
public function accounts()
{
return $this->service->management_accounts;
}
public function goals()
{
return $this->service->management_goals;
}
public function profiles()
{
return $this->service->management_profiles;
}
public function webproperties()
{
return $this->service->management_webproperties;
}
public function getAllSitesIds()
{
if (empty($this->site_ids)) {
$sites = $this->service->management_profiles->listManagementProfiles('~all', '~all');
foreach ($sites['items'] as $site) {
$this->site_ids[$site['websiteUrl']] = 'ga:'.$site['id'];
}
}
return $this->site_ids;
}
public function getSiteIdByUrl($url)
{
if (!isset($this->site_ids[$url])) {
$this->getAllSitesIds();
}
if (isset($this->site_ids[$url])) {
return $this->site_ids[$url];
}
throw new \Exception("Site $url is not present in your Analytics account.");
}
}