DataQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Analytics
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Gdata_Query
  24. */
  25. require_once 'Zend/Gdata/Query.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Gdata
  29. * @subpackage Analytics
  30. */
  31. class Zend_Gdata_Analytics_DataQuery extends Zend_Gdata_Query
  32. {
  33. const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/data';
  34. /**
  35. * The default URI used for feeds.
  36. */
  37. protected $_defaultFeedUri = self::ANALYTICS_FEED_URI;
  38. // D1. Visitor
  39. const DIMENSION_BROWSER = 'ga:browser';
  40. const DIMENSION_BROWSER_VERSION = 'ga:browserVersion';
  41. const DIMENSION_CITY = 'ga:city';
  42. const DIMENSION_CONNECTIONSPEED = 'ga:connectionSpeed';
  43. const DIMENSION_CONTINENT = 'ga:continent';
  44. const DIMENSION_COUNTRY = 'ga:country';
  45. const DIMENSION_DATE = 'ga:date';
  46. const DIMENSION_DAY = 'ga:day';
  47. const DIMENSION_DAYS_SINCE_LAST_VISIT= 'ga:daysSinceLastVisit';
  48. const DIMENSION_FLASH_VERSION = 'ga:flashVersion';
  49. const DIMENSION_HOSTNAME = 'ga:hostname';
  50. const DIMENSION_HOUR = 'ga:hour';
  51. const DIMENSION_JAVA_ENABLED= 'ga:javaEnabled';
  52. const DIMENSION_LANGUAGE= 'ga:language';
  53. const DIMENSION_LATITUDE = 'ga:latitude';
  54. const DIMENSION_LONGITUDE = 'ga:longitude';
  55. const DIMENSION_MONTH = 'ga:month';
  56. const DIMENSION_NETWORK_DOMAIN = 'ga:networkDomain';
  57. const DIMENSION_NETWORK_LOCATION = 'ga:networkLocation';
  58. const DIMENSION_OPERATING_SYSTEM = 'ga:operatingSystem';
  59. const DIMENSION_OPERATING_SYSTEM_VERSION = 'ga:operatingSystemVersion';
  60. const DIMENSION_PAGE_DEPTH = 'ga:pageDepth';
  61. const DIMENSION_REGION = 'ga:region';
  62. const DIMENSION_SCREEN_COLORS= 'ga:screenColors';
  63. const DIMENSION_SCREEN_RESOLUTION = 'ga:screenResolution';
  64. const DIMENSION_SUB_CONTINENT = 'ga:subContinent';
  65. const DIMENSION_USER_DEFINED_VALUE = 'ga:userDefinedValue';
  66. const DIMENSION_VISIT_COUNT = 'ga:visitCount';
  67. const DIMENSION_VISIT_LENGTH = 'ga:visitLength';
  68. const DIMENSION_VISITOR_TYPE = 'ga:visitorType';
  69. const DIMENSION_WEEK = 'ga:week';
  70. const DIMENSION_YEAR = 'ga:year';
  71. // D2. Campaign
  72. const DIMENSION_AD_CONTENT = 'ga:adContent';
  73. const DIMENSION_AD_GROUP = 'ga:adGroup';
  74. const DIMENSION_AD_SLOT = 'ga:adSlot';
  75. const DIMENSION_AD_SLOT_POSITION = 'ga:adSlotPosition';
  76. const DIMENSION_CAMPAIGN = 'ga:campaign';
  77. const DIMENSION_KEYWORD = 'ga:keyword';
  78. const DIMENSION_MEDIUM = 'ga:medium';
  79. const DIMENSION_REFERRAL_PATH = 'ga:referralPath';
  80. const DIMENSION_SOURCE = 'ga:source';
  81. // D3. Content
  82. const DIMENSION_EXIT_PAGE_PATH = 'ga:exitPagePath';
  83. const DIMENSION_LANDING_PAGE_PATH = 'ga:landingPagePath';
  84. const DIMENSION_PAGE_PATH = 'ga:pagePath';
  85. const DIMENSION_PAGE_TITLE = 'ga:pageTitle';
  86. const DIMENSION_SECOND_PAGE_PATH = 'ga:secondPagePath';
  87. // D4. Ecommerce
  88. const DIMENSION_AFFILIATION = 'ga:affiliation';
  89. const DIMENSION_DAYS_TO_TRANSACTION = 'ga:daysToTransaction';
  90. const DIMENSION_PRODUCT_CATEGORY = 'ga:productCategory';
  91. const DIMENSION_PRODUCT_NAME = 'ga:productName';
  92. const DIMENSION_PRODUCT_SKU = 'ga:productSku';
  93. const DIMENSION_TRANSACTION_ID = 'ga:transactionId';
  94. const DIMENSION_VISITS_TO_TRANSACTION = 'ga:visitsToTransaction';
  95. // D5. Internal Search
  96. const DIMENSION_SEARCH_CATEGORY = 'ga:searchCategory';
  97. const DIMENSION_SEARCH_DESTINATION_PAGE = 'ga:searchDestinationPage';
  98. const DIMENSION_SEARCH_KEYWORD = 'ga:searchKeyword';
  99. const DIMENSION_SEARCH_KEYWORD_REFINEMENT = 'ga:searchKeywordRefinement';
  100. const DIMENSION_SEARCH_START_PAGE = 'ga:searchStartPage';
  101. const DIMENSION_SEARCH_USED = 'ga:searchUsed';
  102. // D6. Navigation
  103. const DIMENSION_NEXT_PAGE_PATH = 'ga:nextPagePath';
  104. const DIMENSION_PREV_PAGE_PATH= 'ga:previousPagePath';
  105. // D7. Events
  106. const DIMENSION_EVENT_CATEGORY = 'ga:eventCategory';
  107. const DIMENSION_EVENT_ACTION = 'ga:eventAction';
  108. const DIMENSION_EVENT_LABEL = 'ga:eventLabel';
  109. // D8. Custon Variables
  110. const DIMENSION_CUSTOM_VAR_NAME_1 = 'ga:customVarName1';
  111. const DIMENSION_CUSTOM_VAR_NAME_2 = 'ga:customVarName2';
  112. const DIMENSION_CUSTOM_VAR_NAME_3 = 'ga:customVarName3';
  113. const DIMENSION_CUSTOM_VAR_NAME_4 = 'ga:customVarName4';
  114. const DIMENSION_CUSTOM_VAR_NAME_5 = 'ga:customVarName5';
  115. const DIMENSION_CUSTOM_VAR_VALUE_1 = 'ga:customVarValue1';
  116. const DIMENSION_CUSTOM_VAR_VALUE_2 = 'ga:customVarValue2';
  117. const DIMENSION_CUSTOM_VAR_VALUE_3 = 'ga:customVarValue3';
  118. const DIMENSION_CUSTOM_VAR_VALUE_4 = 'ga:customVarValue4';
  119. const DIMENSION_CUSTOM_VAR_VALUE_5 = 'ga:customVarValue5';
  120. // M1. Visitor
  121. const METRIC_BOUNCES = 'ga:bounces';
  122. const METRIC_ENTRANCES = 'ga:entrances';
  123. const METRIC_EXITS = 'ga:exits';
  124. const METRIC_NEW_VISITS = 'ga:newVisits';
  125. const METRIC_PAGEVIEWS = 'ga:pageviews';
  126. const METRIC_TIME_ON_PAGE = 'ga:timeOnPage';
  127. const METRIC_TIME_ON_SITE = 'ga:timeOnSite';
  128. const METRIC_VISITORS = 'ga:visitors';
  129. const METRIC_VISITS = 'ga:visits';
  130. // M2. Campaign
  131. const METRIC_AD_CLICKS = 'ga:adClicks';
  132. const METRIC_AD_COST = 'ga:adCost';
  133. const METRIC_CPC = 'ga:CPC';
  134. const METRIC_CPM = 'ga:CPM';
  135. const METRIC_CTR = 'ga:CTR';
  136. const METRIC_IMPRESSIONS = 'ga:impressions';
  137. // M3. Content
  138. const METRIC_UNIQUE_PAGEVIEWS = 'ga:uniquePageviews';
  139. // M4. Ecommerce
  140. const METRIC_ITEM_REVENUE = 'ga:itemRevenue';
  141. const METRIC_ITEM_QUANTITY = 'ga:itemQuantity';
  142. const METRIC_TRANSACTIONS = 'ga:transactions';
  143. const METRIC_TRANSACTION_REVENUE = 'ga:transactionRevenue';
  144. const METRIC_TRANSACTION_SHIPPING = 'ga:transactionShipping';
  145. const METRIC_TRANSACTION_TAX = 'ga:transactionTax';
  146. const METRIC_UNIQUE_PURCHASES = 'ga:uniquePurchases';
  147. // M5. Internal Search
  148. const METRIC_SEARCH_DEPTH = 'ga:searchDepth';
  149. const METRIC_SEARCH_DURATION = 'ga:searchDuration';
  150. const METRIC_SEARCH_EXITS = 'ga:searchExits';
  151. const METRIC_SEARCH_REFINEMENTS = 'ga:searchRefinements';
  152. const METRIC_SEARCH_UNIQUES = 'ga:searchUniques';
  153. const METRIC_SEARCH_VISIT = 'ga:searchVisits';
  154. // M6. Goals
  155. const METRIC_GOAL_COMPLETIONS_ALL = 'ga:goalCompletionsAll';
  156. const METRIC_GOAL_STARTS_ALL = 'ga:goalStartsAll';
  157. const METRIC_GOAL_VALUE_ALL = 'ga:goalValueAll';
  158. // TODO goals 1-20
  159. const METRIC_GOAL_1_COMPLETION = 'ga:goal1Completions';
  160. const METRIC_GOAL_1_STARTS = 'ga:goal1Starts';
  161. const METRIC_GOAL_1_VALUE = 'ga:goal1Value';
  162. // M7. Events
  163. const METRIC_TOTAL_EVENTS = 'ga:totalEvents';
  164. const METRIC_UNIQUE_EVENTS = 'ga:uniqueEvents';
  165. const METRIC_EVENT_VALUE = 'ga:eventValue';
  166. // suported filter operators
  167. const EQUALS = "==";
  168. const EQUALS_NOT = "!=";
  169. const GREATER = ">";
  170. const LESS = ">";
  171. const GREATER_EQUAL = ">=";
  172. const LESS_EQUAL = "<=";
  173. const CONTAINS = "=@";
  174. const CONTAINS_NOT ="!@";
  175. const REGULAR ="=~";
  176. const REGULAR_NOT ="!~";
  177. /**
  178. * @var string
  179. */
  180. protected $_profileId;
  181. /**
  182. * @var array
  183. */
  184. protected $_dimensions = array();
  185. /**
  186. * @var array
  187. */
  188. protected $_metrics = array();
  189. /**
  190. * @var array
  191. */
  192. protected $_sort = array();
  193. /**
  194. * @var array
  195. */
  196. protected $_filters = array();
  197. /**
  198. * @param string $id
  199. * @return Zend_Gdata_Analytics_DataQuery
  200. */
  201. public function setProfileId($id)
  202. {
  203. $this->_profileId = $id;
  204. return $this;
  205. }
  206. /**
  207. * @return string
  208. */
  209. public function getProfileId()
  210. {
  211. return $this->_profileId;
  212. }
  213. /**
  214. * @param string $dimension
  215. * @return Zend_Gdata_Analytics_DataQuery
  216. */
  217. public function addDimension($dimension)
  218. {
  219. $this->_dimensions[$dimension] = true;
  220. return $this;
  221. }
  222. /**
  223. * @param string $metric
  224. * @return Zend_Gdata_Analytics_DataQuery
  225. */
  226. public function addMetric($metric)
  227. {
  228. $this->_metrics[$metric] = true;
  229. return $this;
  230. }
  231. /**
  232. * @return array
  233. */
  234. public function getDimensions()
  235. {
  236. return $this->_dimensions;
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function getMetrics()
  242. {
  243. return $this->_metrics;
  244. }
  245. /**
  246. * @param string $dimension
  247. * @return Zend_Gdata_Analytics_DataQuery
  248. */
  249. public function removeDimension($dimension)
  250. {
  251. unset($this->_dimensions[$dimension]);
  252. return $this;
  253. }
  254. /**
  255. * @param string $metric
  256. * @return Zend_Gdata_Analytics_DataQuery
  257. */
  258. public function removeMetric($metric)
  259. {
  260. unset($this->_metrics[$metric]);
  261. return $this;
  262. }
  263. /**
  264. * @param string $value
  265. * @return Zend_Gdata_Analytics_DataQuery
  266. */
  267. public function setStartDate($date)
  268. {
  269. $this->setParam("start-date", $date);
  270. return $this;
  271. }
  272. /**
  273. * @param string $value
  274. * @return Zend_Gdata_Analytics_DataQuery
  275. */
  276. public function setEndDate($date)
  277. {
  278. $this->setParam("end-date", $date);
  279. return $this;
  280. }
  281. /**
  282. * @param string $filter
  283. * @return Zend_Gdata_Analytics_DataQuery
  284. */
  285. public function addFilter($filter)
  286. {
  287. $this->_filters[] = array($filter, true);
  288. return $this;
  289. }
  290. /**
  291. * @param string $filter
  292. * @return Zend_Gdata_Analytics_DataQuery
  293. */
  294. public function addOrFilter($filter)
  295. {
  296. $this->_filters[] = array($filter, false);
  297. return $this;
  298. }
  299. /**
  300. * @param string $sort
  301. * @param boolean[optional] $descending
  302. * @return Zend_Gdata_Analytics_DataQuery
  303. */
  304. public function addSort($sort, $descending=false)
  305. {
  306. // add to sort storage
  307. $this->_sort[] = ($descending?'-':'').$sort;
  308. return $this;
  309. }
  310. /**
  311. * @return Zend_Gdata_Analytics_DataQuery
  312. */
  313. public function clearSort()
  314. {
  315. $this->_sort = array();
  316. return $this;
  317. }
  318. /**
  319. * @param string $segment
  320. * @return Zend_Gdata_Analytics_DataQuery
  321. */
  322. public function setSegment($segment)
  323. {
  324. $this->setParam('segment', $segment);
  325. return $this;
  326. }
  327. /**
  328. * @return string url
  329. */
  330. public function getQueryUrl()
  331. {
  332. $uri = $this->_defaultFeedUri;
  333. if (isset($this->_url)) {
  334. $uri = $this->_url;
  335. }
  336. $dimensions = $this->getDimensions();
  337. if (!empty($dimensions)) {
  338. $this->setParam('dimensions', implode(",", array_keys($dimensions)));
  339. }
  340. $metrics = $this->getMetrics();
  341. if (!empty($metrics)) {
  342. $this->setParam('metrics', implode(",", array_keys($metrics)));
  343. }
  344. // profile id (ga:tableId)
  345. if ($this->getProfileId() != null) {
  346. $this->setParam('ids', 'ga:'.ltrim($this->getProfileId(), "ga:"));
  347. }
  348. // sorting
  349. if ($this->_sort) {
  350. $this->setParam('sort', implode(",", $this->_sort));
  351. }
  352. // filtering
  353. $filters = "";
  354. foreach ($this->_filters as $filter) {
  355. $filters.=($filter[1]===true?';':',').$filter[0];
  356. }
  357. if ($filters!="") {
  358. $this->setParam('filters', ltrim($filters, ",;"));
  359. }
  360. $uri .= $this->getQueryString();
  361. return $uri;
  362. }
  363. }