/**
  * Sends a request based on Analytics_RequestCriteriaModel to Google Analytics' API.
  *
  * @param Analytics_RequestCriteriaModel $criteria
  *
  * @return string
  */
 public function sendRequest(Analytics_RequestCriteriaModel $criteria)
 {
     $criteria->ids = craft()->analytics->getProfileId();
     if ($criteria->realtime) {
         $response = craft()->analytics_api->apiGetGADataRealtime($criteria->ids, $criteria->metrics, $criteria->optParams);
     } else {
         $response = craft()->analytics_api->apiGetGAData($criteria->ids, $criteria->startDate, $criteria->endDate, $criteria->metrics, $criteria->optParams, $criteria->enableCache);
     }
     if ($criteria->format == 'gaData') {
         return $response;
     } else {
         return AnalyticsHelper::gaDataToArray($response);
     }
 }
 public function set($id, $value, $expire = null, $dependency = null, $enableCache = null)
 {
     if (is_null($enableCache)) {
         $enableCache = craft()->config->get('enableCache', 'analytics');
     }
     if ($enableCache) {
         $cacheKey = $this->getCacheKey($id);
         if (!$expire) {
             $expire = craft()->config->get('cacheDuration', 'analytics');
             $expire = AnalyticsHelper::formatDuration($expire);
         }
         return craft()->cache->set($cacheKey, $value, $expire, $dependency);
     }
 }
 /**
  * Transforms a GA Data object to an array
  */
 public static function gaDataToArray($data)
 {
     // cols
     $cols = [];
     foreach ($data->columnHeaders as $col) {
         // define the right type for the chart
         $dataType = $col->dataType;
         $type = $col->dataType;
         $id = $col->name;
         $label = craft()->analytics->getDimMet($col->name);
         switch ($col->name) {
             case 'ga:date':
             case 'ga:yearMonth':
                 $type = 'date';
                 break;
             case 'ga:latitude':
             case 'ga:longitude':
                 $type = 'number';
                 $dataType = 'FLOAT';
                 break;
         }
         switch ($type) {
             case 'INTEGER':
             case 'CURRENCY':
             case 'FLOAT':
             case 'TIME':
             case 'PERCENT':
                 $type = 'number';
                 break;
             case 'STRING':
                 $type = 'string';
                 break;
         }
         $cols[] = array('type' => $type, 'dataType' => $dataType, 'id' => $id, 'label' => Craft::t($label));
     }
     // rows
     $rows = [];
     if ($data->rows) {
         $rows = $data->rows;
         foreach ($rows as $kRow => $row) {
             foreach ($row as $kCell => $value) {
                 $col = $cols[$kCell];
                 // replace value by cell
                 $cell = array('v' => AnalyticsHelper::formatRawValue($col['dataType'], $value), 'f' => AnalyticsHelper::formatValue($col['dataType'], $value));
                 if ($col['id'] == 'ga:continent') {
                     $cell['v'] = craft()->analytics->getContinentCode($cell['v']);
                 }
                 if ($col['id'] == 'ga:subContinent') {
                     $cell['v'] = craft()->analytics->getSubContinentCode($cell['v']);
                 }
                 // translate values
                 switch ($col['id']) {
                     case 'ga:country':
                     case 'ga:city':
                     case 'ga:continent':
                     case 'ga:subContinent':
                     case 'ga:userType':
                     case 'ga:javaEnabled':
                     case 'ga:deviceCategory':
                     case 'ga:mobileInputSelector':
                     case 'ga:channelGrouping':
                     case 'ga:medium':
                         $cell['f'] = Craft::t($cell['f']);
                         break;
                 }
                 // update cell
                 $rows[$kRow][$kCell] = $cell;
             }
         }
     }
     return array('cols' => $cols, 'rows' => $rows);
 }