function __construct($email, $password, $uid, $start_date = null, $end_date = null) { $filter = ''; $this->initialstartdate = date('M j Y', strtotime($start_date)); $this->initialenddate = date('M j Y', strtotime($end_date)); $start_index = 1; $perdayCounts = $this->createDateRangeArray($start_date, $end_date); $max_result = 1000000000; $ga = new gapi($email, $password); $ga->requestReportData($uid, array('date', 'source', 'medium', 'referralPath'), array('pageviews', 'visits', 'entranceBounceRate', 'timeOnSite', 'newVisits'), 'date', $filter, $start_date, $end_date, $start_index, $max_result); $result = $ga->getResults(); $this->pageViews = $ga->getPageviews(); $this->visits = $ga->getVisits(); $this->bounceRate = $ga->getentranceBounceRate(); $this->timeOnSite = $ga->gettimeOnSite() / $this->visits / 60; $this->newVisits = round($ga->getnewVisits() / $this->visits * 100, 2); foreach ($result as $key => $value) { $this->source[] = array('medium' => $value->getMedium(), 'visit' => $value->getVisits(), 'source' => $value->getSource()); $this->dateWise[] = array($value->getDate() => $value->getPageviews()); } foreach ($perdayCounts as $day) { $new_array = ''; foreach ($this->dateWise as $breakPoint) { foreach ($breakPoint as $key => $value) { if ($key == $day) { $new_array[] = $value; } } } $this->valueCountsPerDay[] = array($day => array_sum($new_array)); } }
/** * A temporary function to hold the first example of a chart data return. * We can make this more robust and to handle more uses cases. * @todo Make an entire analytics plugin to be part of the reports plugin. */ public function _monthPageViewsVisits() { if (!empty($instance) && defined('__REPORTS_ANALYTICS_' . $instance)) { extract(unserialize(constant('__REPORTS_ANALYTICS_' . $instance))); } else { if (defined('__REPORTS_ANALYTICS')) { extract(unserialize(__REPORTS_ANALYTICS)); } } if (!empty($userName) && !empty($password) && !empty($setAccount)) { App::import('Vendor', 'Reports.gapi'); $ga = new gapi($userName, $password, isset($_SESSION['ga_auth_token']) ? $_SESSION['ga_auth_token'] : null); $_SESSION['ga_auth_token'] = $ga->getAuthToken(); // $filter = 'country == United States && browser == Firefox || browser == Chrome'; // $report_id, $dimensions, $metrics, $sort_metric=null, $filter=null, $start_date=null, $end_date=null, $start_index=1, $max_results=30 // http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html#ga:visitors foreach ($ga->requestAccountData() as $account) { if (is_object($account)) { if ($account->properties['webPropertyId'] == $setAccount) { $reportId = $account->properties['profileId']; } } } $backMonth = date('Y-m-d', mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"))); $ga->requestReportData(53475, array('date'), array('pageviews', 'visits'), 'date', null, $backMonth, date('Y-m-d')); $i = 0; foreach ($ga->getResults() as $result) { #debug($result); $chartData[$i]['pageviews'] = $result->getPageviews(); $chartData[$i]['visits'] = $result->getVisits(); $chartData[$i]['date'] = $result->getDate(); $i++; } $chartData['totalResults'] = $ga->getTotalResults(); $chartData['totalPageViews'] = $ga->getPageviews(); $chartData['totalVisits'] = $ga->getVisits(); $chartData['updated'] = $ga->getUpdated(); return $chartData; } else { // google analytics username and password must be set in settings (__REPORTS_ANALYTICS) return; } }
function getGAgeneral() { $app =& JFactory::getApplication(); $config = $app->getuserState('rsseoConfig'); try { $ga = new gapi($config['analytics.username'], $config['analytics.password'], $config['ga.token']); $ga->requestReportData($config['ga.account'], '', array('visits', 'pageviews', 'pageviewsPerVisit', 'avgTimeOnSite', 'visitBounceRate', 'percentNewVisits', 'visitors'), null, null, $config['ga.start'], $config['ga.end']); $return = array(); $totalvisits = $ga->getVisits(); $totalvisits = $totalvisits === '' ? JText::_('RSSEO_NOT_AVAILABLE') : $totalvisits; $uniquevisits = $ga->getVisitors(); $uniquevisits = $uniquevisits === '' ? JText::_('RSSEO_NOT_AVAILABLE') : $uniquevisits; $totalpageviews = $ga->getPageviews(); $totalpageviews = $totalpageviews === '' ? JText::_('RSSEO_NOT_AVAILABLE') : $totalpageviews; $avgpageviews = $ga->getPageviewsPerVisit(); $avgpageviews = $avgpageviews === '' ? JText::_('RSSEO_NOT_AVAILABLE') : number_format($avgpageviews, 2); $timeonsite = $ga->getAvgTimeOnSite(); $timeonsite = $timeonsite === '' ? JText::_('RSSEO_NOT_AVAILABLE') : $this->convertseconds(number_format($timeonsite, 0)); $bouncerate = $ga->getVisitBounceRate(); $bouncerate = $bouncerate === '' ? JText::_('RSSEO_NOT_AVAILABLE') : number_format($bouncerate, 2) . ' %'; $newvisits = $ga->getPercentNewVisits(); $newvisits = $newvisits === '' ? JText::_('RSSEO_NOT_AVAILABLE') : number_format($newvisits, 2) . ' %'; $obj1 = new stdClass(); $obj2 = new stdClass(); $obj3 = new stdClass(); $obj4 = new stdClass(); $obj5 = new stdClass(); $obj6 = new stdClass(); $obj7 = new stdClass(); $obj1->title = JText::_('RSSEO_GA_GENERAL_TOTALVISIORS'); $obj4->title = JText::_('RSSEO_GA_GENERAL_AVGPAGEVIEWS'); $obj1->value = $totalvisits; $obj4->value = $avgpageviews; $obj1->descr = JText::_('RSSEO_GA_GENERAL_TOTALVISIORS_DESC'); $obj4->descr = JText::_('RSSEO_GA_GENERAL_AVGPAGEVIEWS_DESC'); $obj2->title = JText::_('RSSEO_GA_GENERAL_UNIQUEVISITS'); $obj5->title = JText::_('RSSEO_GA_GENERAL_TIMEONSITE'); $obj2->value = $uniquevisits; $obj5->value = $timeonsite; $obj2->descr = JText::_('RSSEO_GA_GENERAL_UNIQUEVISITS_DESC'); $obj5->descr = JText::_('RSSEO_GA_GENERAL_TIMEONSITE_DESC'); $obj3->title = JText::_('RSSEO_GA_GENERAL_TOTALPAGEVIEWS'); $obj6->title = JText::_('RSSEO_GA_GENERAL_BOUNCERATE'); $obj3->value = $totalpageviews; $obj6->value = $bouncerate; $obj3->descr = JText::_('RSSEO_GA_GENERAL_TOTALPAGEVIEWS_DESC'); $obj6->descr = JText::_('RSSEO_GA_GENERAL_BOUNCERATE_DESC'); $obj7->title = JText::_('RSSEO_GA_GENERAL_NEWVISITS'); $obj7->value = $newvisits; $obj7->descr = JText::_('RSSEO_GA_GENERAL_NEWVISITS_DESC'); $return[] = $obj1; $return[] = $obj2; $return[] = $obj3; $return[] = $obj4; $return[] = $obj5; $return[] = $obj6; $return[] = $obj7; return $return; } catch (Exception $e) { return $e->getMessage(); } }
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; }
?> </td> </tr> <?php } ?> </table> <table> <tr> <th>Total Results</th> <td><?php echo $ga->getTotalResults(); ?> </td> </tr> <tr> <th>Total Pageviews</th> <td><?php echo $ga->getPageviews(); ?> </tr> <tr> <th>Total Visits</th> <td><?php echo $ga->getVisits(); ?> </td> </tr> </table>
function gaapi() { $this->load->library('ga_api'); $ga = new gapi('*****@*****.**', 'sumitkumarmunjal1'); $ga->requestReportData(60386809, array('browser', 'browserVersion'), array('pageviews', 'visits')); foreach ($ga->getResults() as $result) { echo '<strong>' . $result . '</strong><br />'; echo 'Pageviews: ' . $result->getPageviews() . ' '; echo 'Visits: ' . $result->getVisits() . '<br />'; } echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>'; }
$data_ricerca_mese = date("Y-m-d", time() - 86400 * $giorni_mese); $data_ricerca_settimana = date("Y-m-d", time() - 86400 * $giorni_settimana); $data_ricerca_giorno = date("Y-m-d", time() - 86400 * $giorni_giorno); $oggi = date("Y-m-d"); $ga = new gapi(ga_email, ga_password); ////////////// // GIORNO ////////////// $ga->requestReportData(ga_profile_id, array('visitorType'), array('visitors', 'pageviews', 'visits', 'timeOnSite', 'avgtimeOnsite', 'percentNewVisits', 'newVisits', 'pageviewsPerVisit', 'uniquePageviews'), '', '', $data_ricerca_giorno, date("Y-m-d"), 1, 1000); echo "<div class=\"gacounter\">"; echo "<h3>Periodo osservazione: ultimo giorno </h3>"; echo "<p> dal " . date("d-m-Y", time() - 86400 * $giorni_giorno) . " al " . date("d-m-Y") . "</p>"; echo "<ul><li>visite totali: " . $ga->getVisits() . "</li>"; echo "<li class=\"alternato\">visitatori totali: " . $ga->getVisitors() . "</li>"; echo "<li>nuovi visitatori: " . $ga->getnewVisits() . "</li>"; echo "<li class=\"alternato\">pagine viste: " . $ga->getPageviews() . "</li>"; echo "<li>pagine viste per visita: " . round($ga->getpageviewsPerVisit(), 2) . "</li>"; echo "<li class=\"alternato\">pagine uniche: " . $ga->getuniquePageviews() . "</li>"; $t_medio = tempo_medio($ga->getavgTimeOnSite()); echo "<li>tempo medio di permanenza sul sito: " . $t_medio . "</li></ul>"; echo "</div>"; ////////////// // SETTIMANA ////////////// $ga->requestReportData(ga_profile_id, array('visitorType'), array('visitors', 'pageviews', 'visits', 'timeOnSite', 'avgtimeOnsite', 'percentNewVisits', 'newVisits', 'pageviewsPerVisit', 'uniquePageviews'), '', '', $data_ricerca_settimana, date("Y-m-d"), 1, 1000); echo "<div class=\"gacounter\">"; echo "<h3>Periodo osservazione: ultimi 7 giorni</h3>"; echo "<p> dal " . date("d-m-Y", time() - 86400 * $giorni_settimana) . " al " . date("d-m-Y") . "</p>"; echo "<ul><li>visite totali: " . $ga->getVisits() . "</li>"; echo "<li class=\"alternato\">visitatori totali: " . $ga->getVisitors() . "</li>"; echo "<li>nuovi visitatori: " . $ga->getnewVisits() . "</li>";
<?php // ajax_ga_mobile.php require_once 'includes/global.inc.php'; if (isset($_SESSION['user'])) { $broker = unserialize($_SESSION["user"]); $broker_id = $broker->m_BrokerID; // initialize the google analytics object $ga = new gapi($ga_email, $ga_password); $dimensions = array('browser', 'operatingSystem', 'source', 'segment=gaid::-11'); $metrics = array('visits', 'pageviews', 'timeOnSite'); $segments = ""; //$filters = "country==India && pagePath==/broker_info.php?id=".$broker_id; $filters = "country==India"; $ga->requestReportData($ga_profile, $dimensions, $metrics, '-visits', $filters, '2012-03-01', '2012-04-10', 1, 50); $html = "<table class='table table-striped table-bordered'>"; $html .= "<tr><th>Pageviews</th><th>Visits</th><th>Time On Page</th></tr>"; foreach ($ga->getResults() as $result) { $html .= print_r($result); /*$html .= "<tr>"; $html .= "<td>".$result->getPageviews()."</td>"; $html .= "<td>".$result->getVisits()."</td>"; $html .= "<td>".$result->getTimeOnPage()."</td>"; $html .= "</tr>";*/ } $html .= "</table>"; $html .= '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>'; echo $html; }