Ejemplo n.º 1
0
 /**
  * @expectedException \Widop\GoogleAnalytics\GoogleAnalyticsException
  */
 public function testQueryWithHtmlError()
 {
     $this->clientMock->expects($this->once())->method('getAccessToken')->will($this->returnValue('token'));
     $this->queryMock->expects($this->once())->method('build')->with($this->equalTo('token'))->will($this->returnValue('uri'));
     $this->httpAdapterMock->expects($this->once())->method('getContent')->with($this->equalTo('uri'))->will($this->returnValue('<html></html>'));
     $this->service->query($this->queryMock);
 }
 private function getQuery($profileId, $path)
 {
     $query = new Query($profileId);
     $query->setStartDate(new \DateTime('-1year'));
     $query->setEndDate(new \DateTime());
     // See https://developers.google.com/analytics/devguides/reporting/core/dimsmets
     $query->setMetrics(array('ga:visits', 'ga:bounceRate', 'ga:avgTimeOnPage'));
     $query->setDimensions(array('ga:pagePath'));
     // See https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters
     $query->setFilters(array('ga:pagePath=~^' . $path));
     return $query;
 }
Ejemplo n.º 3
0
 public function testBuild()
 {
     $this->query->setIds($ids = 'ids');
     $this->query->setStartDate($startDate = new \DateTime('2013-01-01'));
     $this->query->setEndDate($endDate = new \DateTime('2013-01-31'));
     $this->query->setMetrics($metrics = array('m1', 'm2'));
     $this->query->setDimensions($dimensions = array('d1', 'd2'));
     $this->query->setSorts(array('s1', 's2'));
     $this->query->setFilters(array('f1', 'f2'));
     $this->query->setSegment('seg');
     $this->query->setStartIndex(10);
     $this->query->setMaxResults(100);
     $this->query->setPrettyPrint(true);
     $this->query->setCallback('call');
     $expected = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ids&metrics=m1%2Cm2&start-date=2013-01-01&' . 'end-date=2013-01-31&access_token=token&start-index=10&max-results=100&segment=seg&dimensions=d1%2Cd2&' . 'filters=f1%2Cf2&sort=s1%2Cs2&prettyPrint=true&callback=call';
     $this->assertSame($expected, $this->query->build('token'));
 }
Ejemplo n.º 4
0
 public function test_init_analytics_instance()
 {
     $client = $this->prepareClientInstance();
     $profileId = 'ga:67356838';
     $query = new Query($profileId);
     $query->setStartDate(new \DateTime('-2months'));
     $query->setEndDate(new \DateTime());
     $query->setMetrics(array('ga:visits', 'ga:bounces'));
     $query->setDimensions(array('ga:browser', 'ga:city'));
     $service = new Service($client);
     $response = $service->query($query);
     var_dump($response);
 }
Ejemplo n.º 5
0
 /**
  * Index page.
  *
  * @return void
  */
 public function home()
 {
     if (Configure::read('Analytics.enabled') === true) {
         $httpAdapter = new CurlHttpAdapter();
         $client = new Client(Configure::read('Analytics.client_id'), Configure::read('Analytics.private_key'), $httpAdapter);
         $service = new Service($client);
         $statistics = Cache::remember('statistics', function () use($service) {
             $statistics = new Query(Configure::read('Analytics.profile_id'));
             $statistics->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setMetrics(array('ga:visits', 'ga:visitors', 'ga:pageviews', 'ga:pageviewsPerVisit', 'ga:avgtimeOnSite', 'ga:visitBounceRate', 'ga:percentNewVisits'));
             return $service->query($statistics);
         }, 'analytics');
         $browsers = Cache::remember('browsers', function () use($service) {
             $browsers = new Query(Configure::read('Analytics.profile_id'));
             $browsers->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setDimensions(array('ga:browser'))->setMetrics(array('ga:pageviews'))->setSorts(array('ga:pageviews'))->setFilters(array('ga:browser==Chrome,ga:browser==Firefox,ga:browser==Internet Explorer,ga:browser==Safari,ga:browser==Opera'));
             return $service->query($browsers);
         }, 'analytics');
         $continents = Cache::remember('continents', function () use($service) {
             $continentsRows = new Query(Configure::read('Analytics.profile_id'));
             $continentsRows->setStartDate(new \DateTime(Configure::read('Analytics.start_date')))->setEndDate(new \DateTime())->setDimensions(array('ga:continent'))->setMetrics(array('ga:visitors'))->setSorts(array('ga:visitors'))->setFilters(array('ga:continent==Africa,ga:continent==Americas,ga:continent==Asia,ga:continent==Europe,ga:continent==Oceania'));
             $continentsRows = $service->query($continentsRows);
             $color = new Color("1abc9c");
             $light = 1;
             $continents = [];
             foreach (array_reverse($continentsRows->getRows()) as $continentRow) {
                 $continent = [];
                 $continent['label'] = $continentRow[0];
                 $continent['data'] = $continentRow[1];
                 $continent['color'] = '#' . $color->lighten($light);
                 array_push($continents, $continent);
                 $light += 10;
             }
             return $continents;
         }, 'analytics');
         $graphVisitors = Cache::remember('graphVisitors', function () use($service) {
             $graphVisitors = new Query(Configure::read('Analytics.profile_id'));
             $graphVisitors->setStartDate(new \DateTime('-7 days'))->setEndDate(new \DateTime())->setDimensions(array('ga:date'))->setMetrics(array('ga:visits', 'ga:pageviews'))->setSorts(array('ga:date'));
             return $service->query($graphVisitors);
         }, 'analytics');
         $this->set(compact('statistics', 'browsers', 'continents', 'graphVisitors'));
     }
     $this->loadModel('Users');
     //UsersGraph
     $usersGraphCount = $this->Users->find('all')->select(['date' => 'DATE_FORMAT(created,\'%d-%m-%Y\')', 'count' => 'COUNT(id)'])->group('DATE(created)')->order(['date' => 'desc'])->where(['UNIX_TIMESTAMP(DATE(created)) >' => (new \DateTime('-8 days'))->getTimestamp()])->toArray();
     $usersGraph = array();
     //Fill the new array with the date of the 8 past days and give them the value 0.
     for ($i = 0; $i < 8; $i++) {
         $date = new \DateTime("{$i} days ago");
         $usersGraph[$date->format('d-m-Y')] = 0;
     }
     //Foreach value that we got in the database, parse the array by the key date,
     //and if the key exist, attribute the new value.
     foreach ($usersGraphCount as $user) {
         $usersGraph[$user->date] = intval($user->count);
     }
     $usersGraph = array_reverse($usersGraph);
     $usersCount = Number::format($this->Users->find()->count());
     $this->loadModel('BlogArticles');
     $articlesCount = Number::format($this->BlogArticles->find()->count());
     $this->loadModel('BlogArticlesComments');
     $commentsCount = Number::format($this->BlogArticlesComments->find()->count());
     $this->loadModel('BlogCategories');
     $categoriesCount = Number::format($this->BlogCategories->find()->count());
     $this->set(compact('usersCount', 'articlesCount', 'commentsCount', 'categoriesCount', 'usersGraph'));
 }