php - Get function in Google Analytics API -
i'm trying fetch data using analytics api, example have this:
function getresults(&$analytics, $profileid) { // calls core reporting api , queries number of sessions // last 7 days. return $analytics->data_ga->get( 'ga:' . $profileid, '7daysago', 'today', 'ga:sessions'); }
and function in analytics.php file is:
public function get($ids, $metrics, $optparams = array()) { $params = array('ids' => $ids, 'metrics' => $metrics); $params = array_merge($params, $optparams); return $this->call('get', array($params), "google_service_analytics_realtimedata"); } }
how adapt example return dimensions along sessions, example, pagepath?
thanks
so question little unclear, first part of question correct, example works , way data google analytics api. not need touch or modify analytics.php however.
here code should like:
$ga_profile_id = xxxxxxx; // insert yours $from = date('y-m-d', time()-2*24*60*60); // last 2 days $to = date('y-m-d'); // today $metrics = 'ga:visits,ga:visitors,ga:pageviews'; $dimensions = 'ga:date'; $sort = "-ga:visits"; $data = $service->data_ga->get('ga:'.$ga_profile_id, $from, $to, $metrics, array('dimensions' => $dimensions,'sort'=>$sort));
these basic elements need started. visit https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries list of common query recipes. replace metrics, dimensions , sort parameters in example above ones listed there run common report scenarios cover.
the analytics api query explorer (https://ga-dev-tools.appspot.com/query-explorer/) great play around , discover metric , dimension names. example, you'll find that dimension page path is: ga:pagepath.
so then, example, if want visits , pageviews page path, insert correct parameters in code, , looks this:
$ga_profile_id = xxxxxx; //insert yours here $from = date('y-m-d', time()-2*24*60*60); // last 2 days $to = date('y-m-d'); // today $metrics = 'ga:visits,ga:pageviews'; $dimensions = 'ga:pagepath'; $sort = "-ga:visits"; $data = $service->data_ga->get('ga:'.$ga_profile_id, $from, $to, $metrics, array('dimensions' => $dimensions,'sort'=>$sort));
which means: metrics visits , pageviews, using page path dimension, , sort visits - last 2 days! makes sense.
Comments
Post a Comment