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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Gdata
* @subpackage Analytics
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* @see Zend_Gdata_Query
*/
require_once 'Zend/Gdata/Query.php';
/**
* @category Zend
* @package Zend_Gdata
* @subpackage Analytics
*/
class Zend_Gdata_Analytics_DataQuery extends Zend_Gdata_Query
{
const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/data';
/**
* The default URI used for feeds.
*/
protected $_defaultFeedUri = self::ANALYTICS_FEED_URI;
// D1. Visitor
const DIMENSION_BROWSER = 'ga:browser';
const DIMENSION_BROWSER_VERSION = 'ga:browserVersion';
const DIMENSION_CITY = 'ga:city';
const DIMENSION_CONNECTIONSPEED = 'ga:connectionSpeed';
const DIMENSION_CONTINENT = 'ga:continent';
const DIMENSION_COUNTRY = 'ga:country';
const DIMENSION_DATE = 'ga:date';
const DIMENSION_DAY = 'ga:day';
const DIMENSION_DAYS_SINCE_LAST_VISIT= 'ga:daysSinceLastVisit';
const DIMENSION_FLASH_VERSION = 'ga:flashVersion';
const DIMENSION_HOSTNAME = 'ga:hostname';
const DIMENSION_HOUR = 'ga:hour';
const DIMENSION_JAVA_ENABLED= 'ga:javaEnabled';
const DIMENSION_LANGUAGE= 'ga:language';
const DIMENSION_LATITUDE = 'ga:latitude';
const DIMENSION_LONGITUDE = 'ga:longitude';
const DIMENSION_MONTH = 'ga:month';
const DIMENSION_NETWORK_DOMAIN = 'ga:networkDomain';
const DIMENSION_NETWORK_LOCATION = 'ga:networkLocation';
const DIMENSION_OPERATING_SYSTEM = 'ga:operatingSystem';
const DIMENSION_OPERATING_SYSTEM_VERSION = 'ga:operatingSystemVersion';
const DIMENSION_PAGE_DEPTH = 'ga:pageDepth';
const DIMENSION_REGION = 'ga:region';
const DIMENSION_SCREEN_COLORS= 'ga:screenColors';
const DIMENSION_SCREEN_RESOLUTION = 'ga:screenResolution';
const DIMENSION_SUB_CONTINENT = 'ga:subContinent';
const DIMENSION_USER_DEFINED_VALUE = 'ga:userDefinedValue';
const DIMENSION_VISIT_COUNT = 'ga:visitCount';
const DIMENSION_VISIT_LENGTH = 'ga:visitLength';
const DIMENSION_VISITOR_TYPE = 'ga:visitorType';
const DIMENSION_WEEK = 'ga:week';
const DIMENSION_YEAR = 'ga:year';
// D2. Campaign
const DIMENSION_AD_CONTENT = 'ga:adContent';
const DIMENSION_AD_GROUP = 'ga:adGroup';
const DIMENSION_AD_SLOT = 'ga:adSlot';
const DIMENSION_AD_SLOT_POSITION = 'ga:adSlotPosition';
const DIMENSION_CAMPAIGN = 'ga:campaign';
const DIMENSION_KEYWORD = 'ga:keyword';
const DIMENSION_MEDIUM = 'ga:medium';
const DIMENSION_REFERRAL_PATH = 'ga:referralPath';
const DIMENSION_SOURCE = 'ga:source';
// D3. Content
const DIMENSION_EXIT_PAGE_PATH = 'ga:exitPagePath';
const DIMENSION_LANDING_PAGE_PATH = 'ga:landingPagePath';
const DIMENSION_PAGE_PATH = 'ga:pagePath';
const DIMENSION_PAGE_TITLE = 'ga:pageTitle';
const DIMENSION_SECOND_PAGE_PATH = 'ga:secondPagePath';
// D4. Ecommerce
const DIMENSION_AFFILIATION = 'ga:affiliation';
const DIMENSION_DAYS_TO_TRANSACTION = 'ga:daysToTransaction';
const DIMENSION_PRODUCT_CATEGORY = 'ga:productCategory';
const DIMENSION_PRODUCT_NAME = 'ga:productName';
const DIMENSION_PRODUCT_SKU = 'ga:productSku';
const DIMENSION_TRANSACTION_ID = 'ga:transactionId';
const DIMENSION_VISITS_TO_TRANSACTION = 'ga:visitsToTransaction';
// D5. Internal Search
const DIMENSION_SEARCH_CATEGORY = 'ga:searchCategory';
const DIMENSION_SEARCH_DESTINATION_PAGE = 'ga:searchDestinationPage';
const DIMENSION_SEARCH_KEYWORD = 'ga:searchKeyword';
const DIMENSION_SEARCH_KEYWORD_REFINEMENT = 'ga:searchKeywordRefinement';
const DIMENSION_SEARCH_START_PAGE = 'ga:searchStartPage';
const DIMENSION_SEARCH_USED = 'ga:searchUsed';
// D6. Navigation
const DIMENSION_NEXT_PAGE_PATH = 'ga:nextPagePath';
const DIMENSION_PREV_PAGE_PATH= 'ga:previousPagePath';
// D7. Events
const DIMENSION_EVENT_CATEGORY = 'ga:eventCategory';
const DIMENSION_EVENT_ACTION = 'ga:eventAction';
const DIMENSION_EVENT_LABEL = 'ga:eventLabel';
// D8. Custon Variables
const DIMENSION_CUSTOM_VAR_NAME_1 = 'ga:customVarName1';
const DIMENSION_CUSTOM_VAR_NAME_2 = 'ga:customVarName2';
const DIMENSION_CUSTOM_VAR_NAME_3 = 'ga:customVarName3';
const DIMENSION_CUSTOM_VAR_NAME_4 = 'ga:customVarName4';
const DIMENSION_CUSTOM_VAR_NAME_5 = 'ga:customVarName5';
const DIMENSION_CUSTOM_VAR_VALUE_1 = 'ga:customVarValue1';
const DIMENSION_CUSTOM_VAR_VALUE_2 = 'ga:customVarValue2';
const DIMENSION_CUSTOM_VAR_VALUE_3 = 'ga:customVarValue3';
const DIMENSION_CUSTOM_VAR_VALUE_4 = 'ga:customVarValue4';
const DIMENSION_CUSTOM_VAR_VALUE_5 = 'ga:customVarValue5';
// M1. Visitor
const METRIC_BOUNCES = 'ga:bounces';
const METRIC_ENTRANCES = 'ga:entrances';
const METRIC_EXITS = 'ga:exits';
const METRIC_NEW_VISITS = 'ga:newVisits';
const METRIC_PAGEVIEWS = 'ga:pageviews';
const METRIC_TIME_ON_PAGE = 'ga:timeOnPage';
const METRIC_TIME_ON_SITE = 'ga:timeOnSite';
const METRIC_VISITORS = 'ga:visitors';
const METRIC_VISITS = 'ga:visits';
// M2. Campaign
const METRIC_AD_CLICKS = 'ga:adClicks';
const METRIC_AD_COST = 'ga:adCost';
const METRIC_CPC = 'ga:CPC';
const METRIC_CPM = 'ga:CPM';
const METRIC_CTR = 'ga:CTR';
const METRIC_IMPRESSIONS = 'ga:impressions';
// M3. Content
const METRIC_UNIQUE_PAGEVIEWS = 'ga:uniquePageviews';
// M4. Ecommerce
const METRIC_ITEM_REVENUE = 'ga:itemRevenue';
const METRIC_ITEM_QUANTITY = 'ga:itemQuantity';
const METRIC_TRANSACTIONS = 'ga:transactions';
const METRIC_TRANSACTION_REVENUE = 'ga:transactionRevenue';
const METRIC_TRANSACTION_SHIPPING = 'ga:transactionShipping';
const METRIC_TRANSACTION_TAX = 'ga:transactionTax';
const METRIC_UNIQUE_PURCHASES = 'ga:uniquePurchases';
// M5. Internal Search
const METRIC_SEARCH_DEPTH = 'ga:searchDepth';
const METRIC_SEARCH_DURATION = 'ga:searchDuration';
const METRIC_SEARCH_EXITS = 'ga:searchExits';
const METRIC_SEARCH_REFINEMENTS = 'ga:searchRefinements';
const METRIC_SEARCH_UNIQUES = 'ga:searchUniques';
const METRIC_SEARCH_VISIT = 'ga:searchVisits';
// M6. Goals
const METRIC_GOAL_COMPLETIONS_ALL = 'ga:goalCompletionsAll';
const METRIC_GOAL_STARTS_ALL = 'ga:goalStartsAll';
const METRIC_GOAL_VALUE_ALL = 'ga:goalValueAll';
// TODO goals 1-20
const METRIC_GOAL_1_COMPLETION = 'ga:goal1Completions';
const METRIC_GOAL_1_STARTS = 'ga:goal1Starts';
const METRIC_GOAL_1_VALUE = 'ga:goal1Value';
// M7. Events
const METRIC_TOTAL_EVENTS = 'ga:totalEvents';
const METRIC_UNIQUE_EVENTS = 'ga:uniqueEvents';
const METRIC_EVENT_VALUE = 'ga:eventValue';
// suported filter operators
const EQUALS = "==";
const EQUALS_NOT = "!=";
const GREATER = ">";
const LESS = ">";
const GREATER_EQUAL = ">=";
const LESS_EQUAL = "<=";
const CONTAINS = "=@";
const CONTAINS_NOT ="!@";
const REGULAR ="=~";
const REGULAR_NOT ="!~";
/**
* @var string
*/
protected $_profileId;
/**
* @var array
*/
protected $_dimensions = array();
/**
* @var array
*/
protected $_metrics = array();
/**
* @var array
*/
protected $_sort = array();
/**
* @var array
*/
protected $_filters = array();
/**
* @param string $id
* @return Zend_Gdata_Analytics_DataQuery
*/
public function setProfileId($id)
{
$this->_profileId = $id;
return $this;
}
/**
* @return string
*/
public function getProfileId()
{
return $this->_profileId;
}
/**
* @param string $dimension
* @return Zend_Gdata_Analytics_DataQuery
*/
public function addDimension($dimension)
{
$this->_dimensions[$dimension] = true;
return $this;
}
/**
* @param string $metric
* @return Zend_Gdata_Analytics_DataQuery
*/
public function addMetric($metric)
{
$this->_metrics[$metric] = true;
return $this;
}
/**
* @return array
*/
public function getDimensions()
{
return $this->_dimensions;
}
/**
* @return array
*/
public function getMetrics()
{
return $this->_metrics;
}
/**
* @param string $dimension
* @return Zend_Gdata_Analytics_DataQuery
*/
public function removeDimension($dimension)
{
unset($this->_dimensions[$dimension]);
return $this;
}
/**
* @param string $metric
* @return Zend_Gdata_Analytics_DataQuery
*/
public function removeMetric($metric)
{
unset($this->_metrics[$metric]);
return $this;
}
/**
* @param string $value
* @return Zend_Gdata_Analytics_DataQuery
*/
public function setStartDate($date)
{
$this->setParam("start-date", $date);
return $this;
}
/**
* @param string $value
* @return Zend_Gdata_Analytics_DataQuery
*/
public function setEndDate($date)
{
$this->setParam("end-date", $date);
return $this;
}
/**
* @param string $filter
* @return Zend_Gdata_Analytics_DataQuery
*/
public function addFilter($filter)
{
$this->_filters[] = array($filter, true);
return $this;
}
/**
* @param string $filter
* @return Zend_Gdata_Analytics_DataQuery
*/
public function addOrFilter($filter)
{
$this->_filters[] = array($filter, false);
return $this;
}
/**
* @param string $sort
* @param boolean[optional] $descending
* @return Zend_Gdata_Analytics_DataQuery
*/
public function addSort($sort, $descending=false)
{
// add to sort storage
$this->_sort[] = ($descending?'-':'').$sort;
return $this;
}
/**
* @return Zend_Gdata_Analytics_DataQuery
*/
public function clearSort()
{
$this->_sort = array();
return $this;
}
/**
* @param string $segment
* @return Zend_Gdata_Analytics_DataQuery
*/
public function setSegment($segment)
{
$this->setParam('segment', $segment);
return $this;
}
/**
* @return string url
*/
public function getQueryUrl()
{
$uri = $this->_defaultFeedUri;
if (isset($this->_url)) {
$uri = $this->_url;
}
$dimensions = $this->getDimensions();
if (!empty($dimensions)) {
$this->setParam('dimensions', implode(",", array_keys($dimensions)));
}
$metrics = $this->getMetrics();
if (!empty($metrics)) {
$this->setParam('metrics', implode(",", array_keys($metrics)));
}
// profile id (ga:tableId)
if ($this->getProfileId() != null) {
$this->setParam('ids', 'ga:'.ltrim($this->getProfileId(), "ga:"));
}
// sorting
if ($this->_sort) {
$this->setParam('sort', implode(",", $this->_sort));
}
// filtering
$filters = "";
foreach ($this->_filters as $filter) {
$filters.=($filter[1]===true?';':',').$filter[0];
}
if ($filters!="") {
$this->setParam('filters', ltrim($filters, ",;"));
}
$uri .= $this->getQueryString();
return $uri;
}
}
|