File indexing completed on 2024-12-22 05:36:43

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Gdata
0017  * @subpackage Analytics
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Gdata_Query
0025  */
0026 // require_once 'Zend/Gdata/Query.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Gdata
0031  * @subpackage Analytics
0032  */
0033 class Zend_Gdata_Analytics_DataQuery extends Zend_Gdata_Query
0034 {
0035     const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/data';
0036 
0037     /**
0038      * The default URI used for feeds.
0039      */
0040     protected $_defaultFeedUri = self::ANALYTICS_FEED_URI;
0041 
0042     // D1. Visitor
0043     const DIMENSION_BROWSER = 'ga:browser';
0044     const DIMENSION_BROWSER_VERSION = 'ga:browserVersion';
0045     const DIMENSION_CITY = 'ga:city';
0046     const DIMENSION_CONNECTIONSPEED = 'ga:connectionSpeed';
0047     const DIMENSION_CONTINENT = 'ga:continent';
0048     const DIMENSION_COUNTRY = 'ga:country';
0049     const DIMENSION_DATE = 'ga:date';
0050     const DIMENSION_DAY = 'ga:day';
0051     const DIMENSION_DAYS_SINCE_LAST_VISIT= 'ga:daysSinceLastVisit';
0052     const DIMENSION_FLASH_VERSION = 'ga:flashVersion';
0053     const DIMENSION_HOSTNAME = 'ga:hostname';
0054     const DIMENSION_HOUR = 'ga:hour';
0055     const DIMENSION_JAVA_ENABLED= 'ga:javaEnabled';
0056     const DIMENSION_LANGUAGE= 'ga:language';
0057     const DIMENSION_LATITUDE = 'ga:latitude';
0058     const DIMENSION_LONGITUDE = 'ga:longitude';
0059     const DIMENSION_MONTH = 'ga:month';
0060     const DIMENSION_NETWORK_DOMAIN = 'ga:networkDomain';
0061     const DIMENSION_NETWORK_LOCATION = 'ga:networkLocation';
0062     const DIMENSION_OPERATING_SYSTEM = 'ga:operatingSystem';
0063     const DIMENSION_OPERATING_SYSTEM_VERSION = 'ga:operatingSystemVersion';
0064     const DIMENSION_PAGE_DEPTH = 'ga:pageDepth';
0065     const DIMENSION_REGION = 'ga:region';
0066     const DIMENSION_SCREEN_COLORS= 'ga:screenColors';
0067     const DIMENSION_SCREEN_RESOLUTION = 'ga:screenResolution';
0068     const DIMENSION_SUB_CONTINENT = 'ga:subContinent';
0069     const DIMENSION_USER_DEFINED_VALUE = 'ga:userDefinedValue';
0070     const DIMENSION_VISIT_COUNT = 'ga:visitCount';
0071     const DIMENSION_VISIT_LENGTH = 'ga:visitLength';
0072     const DIMENSION_VISITOR_TYPE = 'ga:visitorType';
0073     const DIMENSION_WEEK = 'ga:week';
0074     const DIMENSION_YEAR = 'ga:year';
0075 
0076     // D2. Campaign
0077     const DIMENSION_AD_CONTENT = 'ga:adContent';
0078     const DIMENSION_AD_GROUP = 'ga:adGroup';
0079     const DIMENSION_AD_SLOT = 'ga:adSlot';
0080     const DIMENSION_AD_SLOT_POSITION = 'ga:adSlotPosition';
0081     const DIMENSION_CAMPAIGN = 'ga:campaign';
0082     const DIMENSION_KEYWORD = 'ga:keyword';
0083     const DIMENSION_MEDIUM = 'ga:medium';
0084     const DIMENSION_REFERRAL_PATH = 'ga:referralPath';
0085     const DIMENSION_SOURCE = 'ga:source';
0086 
0087     // D3. Content
0088     const DIMENSION_EXIT_PAGE_PATH = 'ga:exitPagePath';
0089     const DIMENSION_LANDING_PAGE_PATH = 'ga:landingPagePath';
0090     const DIMENSION_PAGE_PATH = 'ga:pagePath';
0091     const DIMENSION_PAGE_TITLE = 'ga:pageTitle';
0092     const DIMENSION_SECOND_PAGE_PATH = 'ga:secondPagePath';
0093 
0094     // D4. Ecommerce
0095     const DIMENSION_AFFILIATION = 'ga:affiliation';
0096     const DIMENSION_DAYS_TO_TRANSACTION = 'ga:daysToTransaction';
0097     const DIMENSION_PRODUCT_CATEGORY = 'ga:productCategory';
0098     const DIMENSION_PRODUCT_NAME = 'ga:productName';
0099     const DIMENSION_PRODUCT_SKU = 'ga:productSku';
0100     const DIMENSION_TRANSACTION_ID = 'ga:transactionId';
0101     const DIMENSION_VISITS_TO_TRANSACTION = 'ga:visitsToTransaction';
0102 
0103     // D5. Internal Search
0104     const DIMENSION_SEARCH_CATEGORY = 'ga:searchCategory';
0105     const DIMENSION_SEARCH_DESTINATION_PAGE = 'ga:searchDestinationPage';
0106     const DIMENSION_SEARCH_KEYWORD = 'ga:searchKeyword';
0107     const DIMENSION_SEARCH_KEYWORD_REFINEMENT = 'ga:searchKeywordRefinement';
0108     const DIMENSION_SEARCH_START_PAGE = 'ga:searchStartPage';
0109     const DIMENSION_SEARCH_USED = 'ga:searchUsed';
0110 
0111     // D6. Navigation
0112     const DIMENSION_NEXT_PAGE_PATH = 'ga:nextPagePath';
0113     const DIMENSION_PREV_PAGE_PATH= 'ga:previousPagePath';
0114 
0115     // D7. Events
0116     const DIMENSION_EVENT_CATEGORY = 'ga:eventCategory';
0117     const DIMENSION_EVENT_ACTION = 'ga:eventAction';
0118     const DIMENSION_EVENT_LABEL = 'ga:eventLabel';
0119 
0120     // D8. Custon Variables
0121     const DIMENSION_CUSTOM_VAR_NAME_1 = 'ga:customVarName1';
0122     const DIMENSION_CUSTOM_VAR_NAME_2 = 'ga:customVarName2';
0123     const DIMENSION_CUSTOM_VAR_NAME_3 = 'ga:customVarName3';
0124     const DIMENSION_CUSTOM_VAR_NAME_4 = 'ga:customVarName4';
0125     const DIMENSION_CUSTOM_VAR_NAME_5 = 'ga:customVarName5';
0126     const DIMENSION_CUSTOM_VAR_VALUE_1 = 'ga:customVarValue1';
0127     const DIMENSION_CUSTOM_VAR_VALUE_2 = 'ga:customVarValue2';
0128     const DIMENSION_CUSTOM_VAR_VALUE_3 = 'ga:customVarValue3';
0129     const DIMENSION_CUSTOM_VAR_VALUE_4 = 'ga:customVarValue4';
0130     const DIMENSION_CUSTOM_VAR_VALUE_5 = 'ga:customVarValue5';
0131 
0132     // M1. Visitor
0133     const METRIC_BOUNCES = 'ga:bounces';
0134     const METRIC_ENTRANCES = 'ga:entrances';
0135     const METRIC_EXITS = 'ga:exits';
0136     const METRIC_NEW_VISITS = 'ga:newVisits';
0137     const METRIC_PAGEVIEWS = 'ga:pageviews';
0138     const METRIC_TIME_ON_PAGE = 'ga:timeOnPage';
0139     const METRIC_TIME_ON_SITE = 'ga:timeOnSite';
0140     const METRIC_VISITORS = 'ga:visitors';
0141     const METRIC_VISITS = 'ga:visits';
0142 
0143     // M2. Campaign
0144     const METRIC_AD_CLICKS = 'ga:adClicks';
0145     const METRIC_AD_COST = 'ga:adCost';
0146     const METRIC_CPC = 'ga:CPC';
0147     const METRIC_CPM = 'ga:CPM';
0148     const METRIC_CTR = 'ga:CTR';
0149     const METRIC_IMPRESSIONS = 'ga:impressions';
0150 
0151     // M3. Content
0152     const METRIC_UNIQUE_PAGEVIEWS = 'ga:uniquePageviews';
0153 
0154     // M4. Ecommerce
0155     const METRIC_ITEM_REVENUE = 'ga:itemRevenue';
0156     const METRIC_ITEM_QUANTITY = 'ga:itemQuantity';
0157     const METRIC_TRANSACTIONS = 'ga:transactions';
0158     const METRIC_TRANSACTION_REVENUE = 'ga:transactionRevenue';
0159     const METRIC_TRANSACTION_SHIPPING = 'ga:transactionShipping';
0160     const METRIC_TRANSACTION_TAX = 'ga:transactionTax';
0161     const METRIC_UNIQUE_PURCHASES = 'ga:uniquePurchases';
0162 
0163     // M5. Internal Search
0164     const METRIC_SEARCH_DEPTH = 'ga:searchDepth';
0165     const METRIC_SEARCH_DURATION = 'ga:searchDuration';
0166     const METRIC_SEARCH_EXITS = 'ga:searchExits';
0167     const METRIC_SEARCH_REFINEMENTS = 'ga:searchRefinements';
0168     const METRIC_SEARCH_UNIQUES = 'ga:searchUniques';
0169     const METRIC_SEARCH_VISIT = 'ga:searchVisits';
0170 
0171     // M6. Goals
0172     const METRIC_GOAL_COMPLETIONS_ALL = 'ga:goalCompletionsAll';
0173     const METRIC_GOAL_STARTS_ALL = 'ga:goalStartsAll';
0174     const METRIC_GOAL_VALUE_ALL = 'ga:goalValueAll';
0175     // TODO goals 1-20
0176     const METRIC_GOAL_1_COMPLETION = 'ga:goal1Completions';
0177     const METRIC_GOAL_1_STARTS = 'ga:goal1Starts';
0178     const METRIC_GOAL_1_VALUE = 'ga:goal1Value';
0179 
0180     // M7. Events
0181     const METRIC_TOTAL_EVENTS = 'ga:totalEvents';
0182     const METRIC_UNIQUE_EVENTS = 'ga:uniqueEvents';
0183     const METRIC_EVENT_VALUE = 'ga:eventValue';
0184     
0185     // suported filter operators
0186     const EQUALS = "==";
0187     const EQUALS_NOT = "!=";
0188     const GREATER = ">";
0189     const LESS = ">";
0190     const GREATER_EQUAL = ">=";
0191     const LESS_EQUAL = "<=";
0192     const CONTAINS = "=@";
0193     const CONTAINS_NOT ="!@";
0194     const REGULAR ="=~";
0195     const REGULAR_NOT ="!~";
0196     
0197     /**
0198      * @var string
0199      */
0200     protected $_profileId;
0201     /**
0202      * @var array
0203      */
0204     protected $_dimensions = array();
0205     /**
0206      * @var array
0207      */
0208     protected $_metrics = array();
0209     /**
0210      * @var array
0211      */
0212     protected $_sort = array();
0213     /**
0214      * @var array
0215      */
0216     protected $_filters = array();
0217     
0218     /**
0219      * @param string $id
0220      * @return Zend_Gdata_Analytics_DataQuery
0221      */
0222     public function setProfileId($id)
0223     {
0224         $this->_profileId = $id;
0225         return $this;
0226     }
0227 
0228     /**
0229      * @return string
0230      */
0231     public function getProfileId()
0232     {
0233         return $this->_profileId;
0234     }
0235 
0236     /**
0237      * @param string $dimension
0238      * @return Zend_Gdata_Analytics_DataQuery
0239      */
0240     public function addDimension($dimension)
0241     {
0242         $this->_dimensions[$dimension] = true;        
0243         return $this;
0244     }
0245 
0246     /**
0247      * @param string $metric
0248      * @return Zend_Gdata_Analytics_DataQuery
0249      */
0250     public function addMetric($metric)
0251     {
0252         $this->_metrics[$metric] = true;
0253         return $this;
0254     }
0255 
0256     /**
0257      * @return array
0258      */
0259     public function getDimensions()
0260     {
0261         return $this->_dimensions;
0262     }
0263 
0264     /**
0265      * @return array
0266      */
0267     public function getMetrics()
0268     {
0269         return $this->_metrics;
0270     }
0271 
0272     /**
0273      * @param string $dimension
0274      * @return Zend_Gdata_Analytics_DataQuery
0275      */
0276     public function removeDimension($dimension)
0277     {
0278         unset($this->_dimensions[$dimension]);
0279         return $this;
0280     }
0281     /**
0282      * @param string $metric
0283      * @return Zend_Gdata_Analytics_DataQuery
0284      */
0285     public function removeMetric($metric)
0286     {
0287         unset($this->_metrics[$metric]);
0288         return $this;
0289     }
0290     /**
0291      * @param string $value
0292      * @return Zend_Gdata_Analytics_DataQuery
0293      */
0294     public function setStartDate($date)
0295     {
0296         $this->setParam("start-date", $date);
0297         return $this;
0298     }
0299     /**
0300      * @param string $value
0301      * @return Zend_Gdata_Analytics_DataQuery
0302      */
0303     public function setEndDate($date)
0304     {
0305         $this->setParam("end-date", $date);
0306         return $this;
0307     }
0308     
0309     /**
0310      * @param string $filter
0311      * @return Zend_Gdata_Analytics_DataQuery
0312      */
0313     public function addFilter($filter)
0314     {
0315         $this->_filters[] = array($filter, true);
0316         return $this;
0317     }
0318     
0319     /**
0320      * @param string $filter
0321      * @return Zend_Gdata_Analytics_DataQuery
0322      */
0323     public function addOrFilter($filter)
0324     {
0325         $this->_filters[] = array($filter, false);
0326         return $this;
0327     }
0328     
0329     /**
0330      * @param string $sort
0331      * @param boolean[optional] $descending
0332      * @return Zend_Gdata_Analytics_DataQuery
0333      */
0334     public function addSort($sort, $descending=false)
0335     {
0336         // add to sort storage
0337         $this->_sort[] = ($descending?'-':'').$sort;
0338         return $this;
0339     }
0340     
0341     /**
0342      * @return Zend_Gdata_Analytics_DataQuery
0343      */
0344     public function clearSort()
0345     {
0346         $this->_sort = array();
0347         return $this;
0348     }
0349     
0350     /**
0351      * @param string $segment
0352      * @return Zend_Gdata_Analytics_DataQuery
0353      */
0354     public function setSegment($segment)
0355     {
0356         $this->setParam('segment', $segment);
0357         return $this;
0358     }
0359 
0360     /**
0361      * @return string url
0362      */
0363     public function getQueryUrl()
0364     {
0365         $uri = $this->_defaultFeedUri;
0366         if (isset($this->_url)) {
0367             $uri = $this->_url;
0368         }
0369         
0370         $dimensions = $this->getDimensions();
0371         if (!empty($dimensions)) {
0372             $this->setParam('dimensions', implode(",", array_keys($dimensions)));
0373         }
0374         
0375         $metrics = $this->getMetrics();
0376         if (!empty($metrics)) {
0377             $this->setParam('metrics', implode(",", array_keys($metrics)));
0378         }
0379         
0380         // profile id (ga:tableId)
0381         if ($this->getProfileId() != null) {
0382             $this->setParam('ids', 'ga:'.ltrim($this->getProfileId(), "ga:"));
0383         }
0384                 
0385         // sorting
0386         if ($this->_sort) {
0387             $this->setParam('sort', implode(",", $this->_sort));
0388         }
0389         
0390         // filtering
0391         $filters = "";
0392         foreach ($this->_filters as $filter) {
0393             $filters.=($filter[1]===true?';':',').$filter[0];
0394         }
0395         
0396         if ($filters!="") {
0397             $this->setParam('filters', ltrim($filters, ",;"));
0398         }
0399         
0400         $uri .= $this->getQueryString();
0401         return $uri;
0402     }
0403 }