function getAccounts() { $db =& JFactory::getDBO(); $app =& JFactory::getApplication(); $config = $app->getuserState('rsseoConfig'); try { $ga = new gapi($config['analytics.username'], $config['analytics.password']); $token = $ga->getAuthToken(); $db->setQuery("UPDATE #__rsseo_config SET ConfigValue = '" . $db->getEscaped($token) . "' WHERE ConfigName = 'ga.token' "); $db->query(); $ga->requestAccountData(); return $ga->getResults(); } catch (Exception $e) { $app->redirect('index.php?option=com_rsseo', $e->getMessage(), 'error'); } }
/** * 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 save_settings() { // Get all settings $settings = $this->get_settings(TRUE); // Get current site $site = $this->EE->config->item('site_id'); // print_r($settings); exit(); // If we're posting a username and password, // check if they authenticate, and store them if they do. // If not, discard and throw the authentication error flag if (isset($_POST['user']) && isset($_POST['password'])) { require_once PATH_THIRD . 'cp_analytics/libraries/gapi.class.php'; $ga_user = $_POST['user']; $ga_password = $_POST['password']; $ga = new gapi($ga_user, $ga_password); if ($ga->getAuthToken() != FALSE) { $settings[$site]['user'] = $_POST['user']; $settings[$site]['password'] = base64_encode($_POST['password']); $settings[$site]['authenticated'] = 'y'; } else { // The credentials don't authenticate, so zero us out $settings[$site]['user'] = ''; $settings[$site]['password'] = ''; $settings[$site]['profile'] = ''; $settings[$site]['authenticated'] = 'n'; } } if (isset($_POST['profile'])) { $settings[$site]['profile'] = $_POST['profile']; $settings[$site]['hourly_cache'] = ''; $settings[$site]['daily_cache'] = ''; } $this->EE->db->where('class', ucfirst(get_class($this))); $this->EE->db->update('extensions', array('settings' => serialize($settings))); $this->EE->session->set_flashdata('message_success', $this->EE->lang->line('preferences_updated')); $this->EE->functions->redirect(BASE . AMP . 'C=addons_extensions' . AMP . 'M=extension_settings' . AMP . 'file=' . $this->slug); exit; }
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; }
<?php define('ga_email', '*****@*****.**'); define('ga_password', 'your password'); require 'gapi.class.php'; $ga = new gapi(ga_email, ga_password, isset($_SESSION['ga_auth_token']) ? $_SESSION['ga_auth_token'] : null); $_SESSION['ga_auth_token'] = $ga->getAuthToken(); echo 'Token: ' . $_SESSION['ga_auth_token'];
<?php if (!defined('_ROOT')) { exit('Access Denied'); } $ga = new gapi($cfg['ga_email'], $cfg['ga_pasw'], isset($_SESSION['ga']) ? $_SESSION['ga'] : NULL); if ($_SESSION['ga']['token'] = $ga->getAuthToken()) { $_SESSION['ga']['ga_emai'] = $cfg['ga_email']; $_SESSION['ga']['ga_pasw'] = $cfg['ga_pasw']; echo '1'; } else { echo '0'; } exit;