function fetch_daily_stats($ga_user, $ga_password, $ga_profile_id) { global $LOC; $data = array(); $data['cache_date'] = date('Y-m-d', $LOC->set_localized_time()); require_once PATH_LIB . 'analytics_panel/gapi.class.php'; // Compile yesterday's stats $yesterday = new gapi($ga_user, $ga_password); $ga_auth_token = $yesterday->getAuthToken(); $yesterday->requestReportData($ga_profile_id, array('date'), array('pageviews', 'visits', 'timeOnSite'), '', '', date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime('yesterday'))); // Get account data so we can store the profile info $data['profile'] = array(); $yesterday->requestAccountData(1, 100); foreach ($yesterday->getResults() as $result) { if ($result->getProfileId() == $ga_profile_id) { $data['profile']['id'] = $result->getProfileId(); $data['profile']['title'] = $result->getTitle(); } } $data['yesterday']['visits'] = number_format($yesterday->getVisits()); $data['yesterday']['pageviews'] = number_format($yesterday->getPageviews()); $data['yesterday']['pages_per_visit'] = $this->analytics_avg_pages($yesterday->getPageviews(), $yesterday->getVisits()); $data['yesterday']['avg_visit'] = $this->analytics_avg_visit($yesterday->getTimeOnSite(), $yesterday->getVisits()); // Compile last month's stats $lastmonth = new gapi($ga_user, $ga_password, $ga_auth_token); $lastmonth->requestReportData($ga_profile_id, array('date'), array('pageviews', 'visits', 'newVisits', 'timeOnSite', 'bounces', 'entrances'), 'date', '', date('Y-m-d', strtotime('31 days ago')), date('Y-m-d', strtotime('yesterday'))); $data['lastmonth']['date_span'] = date('F jS Y', strtotime('31 days ago')) . ' – ' . date('F jS Y', strtotime('yesterday')); $data['lastmonth']['visits'] = number_format($lastmonth->getVisits()); $data['lastmonth']['visits_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'visits'); $data['lastmonth']['pageviews'] = number_format($lastmonth->getPageviews()); $data['lastmonth']['pageviews_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'pageviews'); $data['lastmonth']['pages_per_visit'] = $this->analytics_avg_pages($lastmonth->getPageviews(), $lastmonth->getVisits()); $data['lastmonth']['pages_per_visit_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'avgpages'); $data['lastmonth']['avg_visit'] = $this->analytics_avg_visit($lastmonth->getTimeOnSite(), $lastmonth->getVisits()); $data['lastmonth']['avg_visit_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'time'); $data['lastmonth']['bounce_rate'] = $lastmonth->getBounces() > 0 && $lastmonth->getBounces() > 0 ? round($lastmonth->getBounces() / $lastmonth->getEntrances() * 100, 2) . '%' : '0%'; $data['lastmonth']['bounce_rate_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'bouncerate'); $data['lastmonth']['new_visits'] = $lastmonth->getNewVisits() > 0 && $lastmonth->getVisits() > 0 ? round($lastmonth->getNewVisits() / $lastmonth->getVisits() * 100, 2) . '%' : '0%'; $data['lastmonth']['new_visits_sparkline'] = $this->analytics_sparkline($lastmonth->getResults(), 'newvisits'); // Compile last month's top content $topcontent = new gapi($ga_user, $ga_password, $ga_auth_token); $topcontent->requestReportData($ga_profile_id, array('hostname', 'pagePath'), array('pageviews'), '-pageviews', '', date('Y-m-d', strtotime('31 days ago')), date('Y-m-d', strtotime('yesterday')), null, 20); $data['lastmonth']['content'] = array(); $i = 0; // Make a temporary array to hold page paths // (for checking dupes resulting from www vs non-www hostnames) $paths = array(); foreach ($topcontent->getResults() as $result) { // Do we already have this page path? $dupe_key = array_search($result->getPagePath(), $paths); if ($dupe_key !== FALSE) { // Combine the pageviews of the dupes $data['lastmonth']['content'][$dupe_key]['count'] = $result->getPageviews() + $data['lastmonth']['content'][$dupe_key]['count']; } else { $url = strlen($result->getPagePath()) > 30 ? substr($result->getPagePath(), 0, 30) . '…' : $result->getPagePath(); $data['lastmonth']['content'][$i]['title'] = '<a href="http://' . $result->getHostname() . $result->getPagePath() . '" target="_blank">' . $url . '</a>'; $data['lastmonth']['content'][$i]['count'] = $result->getPageviews(); // Store the page path at the same position so we can check for dupes $paths[$i] = $result->getPagePath(); $i++; } } // Slice down to 10 results $data['lastmonth']['content'] = array_slice($data['lastmonth']['content'], 0, 10); // Compile last month's top referrers $referrers = new gapi($ga_user, $ga_password, $ga_auth_token); $referrers->requestReportData($ga_profile_id, array('source', 'referralPath', 'medium'), array('visits'), '-visits', '', date('Y-m-d', strtotime('31 days ago')), date('Y-m-d', strtotime('yesterday')), null, 10); $data['lastmonth']['referrers'] = array(); $i = 0; foreach ($referrers->getResults() as $result) { $data['lastmonth']['referrers'][$i]['title'] = $result->getMedium() == 'referral' ? '<a href="http://' . $result->getSource() . $result->getReferralPath() . '" target="_blank">' . $result->getSource() . '</a>' : $result->getSource(); $data['lastmonth']['referrers'][$i]['count'] = number_format($result->getVisits()); $i++; } return $data; }