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')); } }
// change to if(true) to echo the example if (false) { $data = $api->data($id, 'ga:browser,ga:browserVersion', 'ga:visits,ga:pageviews', false, false, false, 100); //print_r($data); // you can then access the metrics for a specific dimension vs metric like e.g. echo $data['Internet Explorer']['8.0']['ga:pageviews'], "\n"; // or loop through the data foreach ($data as $dimension1 => $array) { foreach ($array as $dimension2 => $metrics) { echo "{$dimension1} {$dimension2} visits: {$metrics['ga:visits']} pageviews: {$metrics['ga:pageviews']}\n"; } } } // get a summary for the selected profile just for yesterday if (false) { $data = $api->get_summary($id, 'yesterday'); print_r($data); } // get a summary for all profiles just for yesterday if (false) { $data = $api->get_summaries('yesterday'); print_r($data); } if (false) { // get data filtered by canada, using a string as the filters parameter $data = $api->data($id, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, 'ga:country%3d%3dCanada'); print_r($data); // get data filtered by canada and firefox browser, using a string as the filters parameter $data = $api->data($id, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, 'ga:country%3d%3dCanada;ga:browser%3d@Firefox'); print_r($data); // same as the second example above but using the filtering class