/** * fetchData is just pulling in raw data like it is coming form the API and is supposed to be a data provider * for functions that return certain formats like GoogleDataStore::fetchPerformance() * * @param Query assoc Array containing all necessary query parameters that are not already set, @see GoogleDataStore::$query * @return Query Array containing raw data retrieved from the analytics_api.php **/ public function fetchData($query) { require_once '../googleanalytics/thirdparty/analytics_api.php'; // prep query params $query = array_merge($this->query, $query); // get cache object $q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"'; $hash = hash('md5', serialize(array_merge($this->setup, $query))); $cache = DataObject::get_one('GoogleCachedQuery', "{$q}GoogleCachedQuery{$q}.{$q}Hash{$q} = '{$hash}'"); if (!$cache) { $cache = new GoogleCachedQuery(array('Hash' => $hash)); } // poll fresh data if cached query is empty or outdated if (date('Y-m-d', strtotime($cache->LastEdited)) != date('Y-m-d')) { $api = new analytics_api(); if ($api->login($this->setup['email'], $this->setup['password'])) { // $xml = $api->call($this->setup['url']); $data = $api->data('ga:' . $this->setup['ids'], $query['dimensions'], $query['metrics'], $query['sort'], $query['start-date'], $query['end-date'], $query['max-results'], $query['start-index'], $query['filters']); $cache->Data = serialize($data); $cache->write(); } else { trigger_error('ERROR: failed to connect remote server.'); } } else { $data = unserialize($cache->Data); } return $data; }
public function indexAction($params) { // enter your login, password and id into the variables below to try it out $login = GAQ_USERNAME; $password = GAQ_PASSWORD; // NOTE: the id is in the form ga:12345 and not just 12345 // if you do e.g. 12345 then no data will be returned // read http://www.electrictoolbox.com/get-id-for-google-analytics-api/ for info about how to get this id from the GA web interface // or load the accounts (see below) and get it from there // if you don't specify an id here, then you'll get the "Badly formatted request to the Google Analytics API..." error message $id = GAQ_PROFILE_ID; $api = new analytics_api(); if($api->login($login, $password)) { parent::set('visitors', $api->data($id, '', 'ga:bounces,ga:newVisits,ga:visits,ga:pageviews,ga:uniquePageviews')); parent::set('visitorsToday', $api->get_summary($id, 'today')); } }
<div id="center-column"> <div class="top-bar"> <h1>Cpanel - Google Analytics</h1> <div class="breadcrumbs"><a href="index.php?act=gainfo" title="Google Analytics Info">Google Analytics Login Info</a></div> </div><br /> <div class="select-bar"> <label> <h3>Google Analytics Data</h3> </label> </div> <?php $api = new analytics_api(); if ($api->login($gaLogin['galogin'], $gaLogin['gapassword'])) { $api->load_accounts(); $tableId = $api->getAccountsTableIdFromName($gaLogin['gaurl']); $data = $api->data($tableId, 'ga:date', 'ga:visits', 'ga:date', date('Y-m-d', strtotime('1 month ago')), date('Y-m-d'), 33); $amounts = array(); $amounts2 = array(); $amounts3 = array(); foreach ($data as $i => $value) { $amounts[substr($i, 4, 2) . '/' . substr($i, 6, 2)] = $value['ga:visits']; } $_SESSION['amounts'] = serialize($amounts); $_SESSION['url'] = $gaLogin['gaurl']; unset($amounts); // output image ?> <div style="margin 0 auto 10px auto;"><img src="ga_graph2.php?pic=1" style="border:3px solid #000;margin-bottom:10px;"/></div> <?php $data = $api->data($tableId, '', 'ga:newVisits,ga:visits,ga:bounces,ga:pageviews,ga:uniquePageviews'); foreach ($data as $metric => $count) {
) ) the id you need to pass to ->data() is [tableId] for example, if you were wanting to get the profile id for www.electrictoolbox.com you could do this: $api->load_accounts(); $id = $api->accounts['www.electrictoolbox.com']['tableId']; be sure to also read http://www.electrictoolbox.com/google-analytics-api-php-class-qa-20090513/ in the update to the section "How do I get the account name with a profile_ID" because it would seem that not everyone sees the index and account name the same way i have in the example above */ } // get some account summary information without a dimension // change to if(true) to echo the example if (false) { $data = $api->data($id, '', 'ga:bounces,ga:newVisits,ga:visits,ga:pageviews,ga:uniquePageviews'); foreach ($data as $metric => $count) { echo "{$metric}: {$count}\n"; } } // get the pagePath vs pageviews & unique pageviews // change to if(true) to echo the example if (false) { $data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews'); //print_r($data); // how to loop through the data foreach ($data as $dimension => $metrics) { echo "{$dimension} pageviews: {$metrics['ga:pageviews']} unique pageviews: {$metrics['ga:uniquePageviews']}\n"; } } // get the browser vs visits & pageviews