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);
 }
示例#2
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);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $clientId = $this->getContainer()->getParameter('ga_client_id');
     $profileId = $this->getContainer()->getParameter('ga_profile_id');
     $privateKeyFile = $this->getContainer()->getParameter('kernel.root_dir') . '/../bin/ga_p12_key/certificate.p12';
     $httpAdapter = new CurlHttpAdapter();
     $client = new Client($clientId, $privateKeyFile, $httpAdapter);
     $em = $this->getContainer()->get('doctrine')->getManager();
     $contentItems = $em->getRepository('AppBundle:ContentItem')->getValidContentItems()->getResult();
     $rows = [];
     foreach ($contentItems as $key => $item) {
         $service = new Service($client);
         $response = $service->query($this->getQuery($profileId, $item->getGaPath()));
         $item->setVisits($response->getTotalsForAllResults()['ga:visits']);
         $item->setBounceRate($response->getTotalsForAllResults()['ga:bounceRate']);
         $item->setAvgTimeOnPage($response->getTotalsForAllResults()['ga:avgTimeOnPage']);
         $item->setValuesUpdatedDate(new \DateTime());
         $rows[] = array($item->getGaPath(), $item->getVisits(), $item->getBounceRate(), $item->getAvgTimeOnPage());
     }
     $em->flush();
     $table = new Table($output);
     $table->setHeaders(array('Path', 'Visits', 'Bounce Rate', 'Avg Time On Page'))->setRows($rows);
     $table->render();
 }
示例#4
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'));
 }