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