Exemple #1
0
 public function getAnalytics($k = false)
 {
     require LIB_PATH . 'analytics/GAPI.php';
     $cache = \StarterKit\Cache::getInstance();
     $key = 'admin:analytics';
     $data = $cache->get($key);
     if ($data === -1) {
         $ga = new \GoogleAnalyticsAPI('service');
         $ga->auth->setClientId('444232255351-jp55p2lohhfu4oao0bpm0lggo00sotuc.apps.googleusercontent.com');
         // From the APIs console
         $ga->auth->setEmail('*****@*****.**');
         // From the APIs console
         $ga->auth->setPrivateKey(LIB_PATH . 'analytics/secret.p12');
         $auth = $ga->auth->getAccessToken();
         if ($auth['http_code'] != 200) {
             throw new \exception('Fail to connect');
         }
         $token = $auth['access_token'];
         $expires = $auth['expires_in'];
         $created = time();
         $ga->setAccessToken($token);
         $ga->setAccountId('ga:109638630');
         //set some common dates
         $now = date('Y-m-d', strtotime(date('Y-m-d') . '-1 day'));
         // $dates represents the start date parameter for each timespan. no need to specify end date, we already have it in $now
         $dates = ['year' => date("Y-m-d", strtotime('first day of January ' . date('Y'))), 'month' => date('Y-m-01', strtotime('this month')), 'day' => date('Y-m-d', strtotime($now . ' -1 day'))];
         $data = [];
         //device
         $data['devices'] = $ga->getVisitsBySystemOs(['max-results' => 100])['rows'];
         //visits by location
         $data['locations'] = $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:country', 'sort' => '-ga:visits', 'max-results' => 50, 'start-date' => $dates['year']])['rows'];
         //new vs returning
         $data['new_vs_returning'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:userType'])['rows'];
         //visits by city
         $data['visits_by_city'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city', 'max-results' => 10])['rows'];
         //map view
         $data['map_view'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city,ga:Latitude,ga:Longitude'])['rows'];
         //visitors by browser
         $data['browser'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:browser'])['rows'];
         //visitors by screensize
         $data['screen_sizes'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:screenResolution'])['rows'];
         //visits by service provider
         $data['isp'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:networkDomain', 'max-results' => 50])['rows'];
         //most popular pages
         $data['pages'] = $ga->query(['metrics' => 'ga:pageViews', 'dimensions' => 'ga:pagePath', 'max-results' => 10])['rows'];
         $data['hits_by_day'] = $ga->query(['metrics' => 'ga:pageviews,ga:visitors', 'dimensions' => 'ga:date', 'start-date' => $dates['month'], 'end-date' => $now])['rows'];
         $data['hits_by_country'] = $ga->query(['metrics' => 'ga:visitors', 'dimensions' => 'ga:country', 'start-date' => $dates['month'], 'end-date' => $now])['rows'];
         $data['hits_by_city'] = $ga->query(['metrics' => 'ga:visitors', 'dimensions' => 'ga:city', 'start-date' => $dates['month'], 'end-date' => $now])['rows'];
         $data['this_month'] = ['name' => date('M'), 'data' => $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:date', 'start-date' => $dates['month'], 'end-date' => $now])['rows']];
         $data['this_year'] = ['name' => date('Y'), 'data' => $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:date', 'start-date' => $dates['year'], 'end-date' => $now])['rows']];
         $data['months_in_year'] = $ga->query(['metrics' => 'ga:pageviews', 'dimensions' => 'ga:month', 'sort' => 'ga:month', 'start-date' => $dates['year'], 'end-date' => $now])['rows'];
         $data['social'] = $ga->query(['metrics' => 'ga:socialActivities', 'dimensions' => 'ga:week', 'sort' => 'ga:week', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 25])['rows'];
         $data['referrals'] = $ga->query(['metrics' => 'ga:users', 'dimensions' => 'ga:referralPath', 'sort' => 'ga:referralPath', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows'];
         $data['device_type'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:deviceCategory', 'sort' => 'ga:deviceCategory', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows'];
         $data['mobile_devices'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:mobileDeviceInfo', 'sort' => 'ga:mobileDeviceInfo', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 20])['rows'];
         try {
             $data['search_terms'] = $ga->query(['metrics' => 'ga:searchResultViews', 'dimensions' => 'ga:searchKeyword', 'sort' => 'ga:searchKeyword', 'start-date' => '30daysAgo', 'end-date' => 'yesterday', 'max-results' => 50])['rows'];
         } catch (\exception $e) {
             $data['search_terms'] = [['None Yet', 0]];
         }
         $data['last_seven'] = $ga->query(['metrics' => 'ga:pageviews', 'dimensions' => 'ga:dayOfWeek', 'sort' => 'ga:dayOfWeek', 'start-date' => '8daysAgo', 'end-date' => 'yesterday', 'max-results' => 50])['rows'];
         if (!empty($data) && $data !== false && $data !== 0 && !is_null($data)) {
             $cache->set($key, $data, 60 * 60);
             //never set empty arrays to cache cache!
         }
     }
     if ($k) {
         if (isset($data[$k])) {
             return $data[$k];
         }
     }
     return $data;
 }
Exemple #2
0
if ($auth['http_code'] != 200) {
    print_r($auth);
    throw new \exception('Fail to connect');
}
$token = $auth['access_token'];
$expires = $auth['expires_in'];
$created = time();
$ga->setAccessToken($token);
$ga->setAccountId('ga:109638630');
//set some common dates
$now = date('Y-m-d', strtotime(date('Y-m-d') . '-1 day'));
// $dates represents the start date parameter for each timespan. no need to specify end date, we already have it in $now
$dates = ['year' => date("Y-m-d", strtotime('first day of January ' . date('Y'))), 'month' => date('Y-m-01', strtotime('this month')), 'day' => date('Y-m-d', strtotime($now . ' -1 day'))];
$data = [];
//device
$data['devices'] = $ga->getVisitsBySystemOs(['max-results' => 100]);
//visits by location
$data['locations'] = $ga->query(['metrics' => 'ga:visits', 'dimensions' => 'ga:country', 'sort' => '-ga:visits', 'max-results' => 50, 'start-date' => $dates['year']]);
//new vs returning
$data['new_vs_returning'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:userType']);
//visits by city
$data['visits_by_city'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city', 'max-results' => 10]);
//map view
$data['map_view'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:city,ga:Latitude,ga:Longitude']);
//visitors by browser
$data['browser'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:browser']);
//visitors by screensize
$data['screen_sizes'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:screenResolution']);
//visits by service provider
$data['isp'] = $ga->query(['metrics' => 'ga:sessions', 'dimensions' => 'ga:networkDomain', 'max-results' => 50]);
//most popular pages