コード例 #1
0
 /**
  * Helper method to create a single query instance from an associative
  * array of command-line arguments or XML configuration values.
  *
  * @param array $args
  * @return Google\Analytics\GaDataQuery
  */
 private static function _getQuery(array $args)
 {
     if (!isset($args['profile-name']) && !isset($args['profile-id'])) {
         throw new InvalidArgumentException('A profile name or ID must be specified.');
     }
     if (!isset($args['metric'])) {
         throw new InvalidArgumentException('At least one metric must be specified.');
     }
     if (!isset($args['start-date']) || !isset($args['end-date'])) {
         throw new InvalidArgumentException('A start date and an end date must be specified.');
     }
     // See whether we have to resolve date shortcuts
     $dateKeys = array('start-date', 'end-date');
     foreach ($dateKeys as $key) {
         if (preg_match('/^[A-Z]+_[_A-Z]+_[A-Z]+$/', $args[$key])) {
             $r = new \ReflectionClass(__NAMESPACE__ . '\\GaDataQuery');
             try {
                 $args[$key] = $r->getConstant($args[$key]);
             } catch (\ReflectionException $e) {
                 throw new InvalidArgumentException('"' . $args[$key] . '" is not a valid date shortcut.');
             }
         }
     }
     if (isset($args['split-queries-by'])) {
         $interval = strtoupper($args['split-queries-by']);
         if ($interval != 'DAY' && $interval != 'WEEK' && $interval != 'MONTH' && $interval != 'YEAR') {
             throw new InvalidArgumentException('Queries may only be split by day, week, month, or year.');
         }
         $q = new DateRangeGaDataQuery(null, $args['start-date'], $args['end-date'], new \DateInterval('P1' . $interval[0]));
         $q->setIterativeName(ucfirst(strtolower($interval)));
         if (isset($args['date-format-string'])) {
             $q->setFormatString($args['date-format-string']);
         }
     } else {
         $q = new GaDataQuery();
         $q->setStartDate($args['start-date']);
         $q->setEndDate($args['end-date']);
     }
     if (isset($args['name'])) {
         $q->setName($args['name']);
     }
     if (isset($args['profile-id'])) {
         $q->setProfile($args['profile-id']);
     } else {
         $q->setProfileName($args['profile-name']);
     }
     $q->setMetrics(explode(',', $args['metric']));
     if (isset($args['dimension'])) {
         $q->setDimensions(explode(',', $args['dimension']));
     }
     if (isset($args['sort'])) {
         $sort = new GaDataSortOrder();
         $sortStrings = explode(',', $args['sort']);
         foreach ($sortStrings as $sortString) {
             if (!strlen($sortString)) {
                 continue;
             }
             if ($sortString[0] == '-') {
                 $order = SORT_DESC;
                 $sortString = substr($sortString, 1);
             } else {
                 $order = SORT_ASC;
             }
             $sort->addField($sortString, $order);
         }
         $q->setSort($sort);
     }
     if (isset($args['limit'])) {
         $q->setTotalResults($args['limit']);
     }
     if (isset($args['filter'])) {
         $q->setFilter($args['filter']);
     }
     if (isset($args['segment'])) {
         $q->setSegment($args['segment']);
     }
     if (isset($args['sampling-level'])) {
         $q->setSamplingLevel(strtoupper($args['sampling-level']));
     }
     return $q;
 }
コード例 #2
0
 /**
  * Tests to make sure the array representation of the query is accurate.
  */
 public function testArrayRepresentation()
 {
     $q = new GaDataQuery();
     /* It shouldn't be possible to get the query's array representation
        until the minimum required properties have been set. */
     $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray'));
     $profile = new ProfileSummary();
     $profile->setID('123');
     $profile->setName('foo');
     $q->setProfile($profile);
     $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray'));
     $q->setStartDate('2015-03-15');
     $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray'));
     $q->setEndDate('2015-03-31');
     $this->assertThrows(__NAMESPACE__ . '\\LogicException', array($q, 'getAsArray'));
     $q->setMetrics(array('a_metric', 'another_metric'));
     $expected = array('ids' => 'ga:123', 'start-date' => '2015-03-15', 'end-date' => '2015-03-31', 'metrics' => 'ga:a_metric,ga:another_metric', 'start-index' => 1, 'max-results' => GOOGLE_ANALYTICS_API_PAGE_SIZE);
     $this->assertEquals($expected, $q->getAsArray());
     $q->setMaxResults(234);
     $expected['max-results'] = 234;
     $this->assertEquals($expected, $q->getAsArray());
     $q->setStartIndex(1000);
     $expected['start-index'] = 1000;
     $this->assertEquals($expected, $q->getAsArray());
     $q->setMetrics('foo,bar,baz');
     $expected['metrics'] = 'ga:foo,ga:bar,ga:baz';
     $this->assertEquals($expected, $q->getAsArray());
     $profile = new ProfileSummary();
     $profile->setID('987987');
     $profile->setName('asdf');
     $q->setProfile($profile);
     $expected['ids'] = 'ga:987987';
     $this->assertEquals($expected, $q->getAsArray());
     $q->setStartDate('2000-01-01');
     $q->setEndDate('2000-12-31');
     $expected['start-date'] = '2000-01-01';
     $expected['end-date'] = '2000-12-31';
     $this->assertEquals($expected, $q->getAsArray());
     $q->setSamplingLevel(GaDataQuery::SAMPLING_LEVEL_FASTER);
     $expected['samplingLevel'] = 'FASTER';
     $this->assertEquals($expected, $q->getAsArray());
     // Note the special case here
     $q->setSamplingLevel(GaDataQuery::SAMPLING_LEVEL_NONE);
     unset($expected['samplingLevel']);
     $this->assertEquals($expected, $q->getAsArray());
     $q->setDimensions(array('asdfasdf', 'oijoij'));
     $expected['dimensions'] = 'ga:asdfasdf,ga:oijoij';
     $this->assertEquals($expected, $q->getAsArray());
     $sort = new GaDataSortOrder();
     $sort->addField('foo');
     $sort->addField('bar', SORT_DESC);
     $q->setSort($sort);
     $expected['sort'] = (string) $sort;
     $this->assertEquals($expected, $q->getAsArray());
     $filter = new GaDataFilterCollection(GaDataFilterCollection::OP_AND, new GaDataFilterCollection(GaDataFilterCollection::OP_OR, new GaDataConditionalExpression('foo', GaDataConditionalExpression::OP_GE, 1)), new GaDataFilterCollection(GaDataFilterCollection::OP_OR, new GaDataConditionalExpression('bar', GaDataConditionalExpression::OP_NE, 'baz')));
     $q->setFilter($filter);
     $expected['filters'] = (string) $filter;
     $this->assertEquals($expected, $q->getAsArray());
     $segment = new GaDataSegment(new GaDataSegmentConditionGroup(new GaDataSegmentSimpleCondition('foo', GaDataSegmentSimpleCondition::OP_LT, 20)), GaDataSegment::SCOPE_USERS, new GaDataSegmentSequence(new GaDataSegmentSequenceCondition('bar', GaDataSegmentSequenceCondition::OP_EQ, 'baz'), new GaDataSegmentSequenceCondition('baz', GaDataSegmentSequenceCondition::OP_GT, 0, GaDataSegmentSequenceCondition::OP_FOLLOWED_BY_IMMEDIATE)), GaDataSegment::SCOPE_SESSIONS);
     $q->setSegment($segment);
     $expected['segment'] = (string) $segment;
     $this->assertEquals($expected, $q->getAsArray());
 }