Mac Mac - 4 days ago 5
PHP Question

Google Analytics, Dimensions & Metrics - How to?

I've finally figured out how i connected to the Google Analytics, correct - and I'm now able to access data to some point. I'm using the google-api-php-client.

I can work with metrics just fine fx, by doing

return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions,ga:pageviews,ga:sessionDuration');


which will return me number of sessions, pageviews, and session duration. But now lets say I am interested in using some of the dimensions as well - Maybe i want the query return site usage data for all traffic by search engine, sorted by pageviews in descending order.

dimensions=ga:source
metrics=ga:pageviews,ga:sessionDuration,ga:exits
filters=ga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppc
sort=-ga:pageviews


the data_ga->get function calls for the following parameters: $ids, $startDate, $endDate, $metrics, $optParams = array()

I tried supplying the dimensions and filters in a array, but it returns me the following errors


Warning: Illegal string offset 'type' in
xxxxxxxxx/src/Google/Service/Resource.php on line 269

Warning: Illegal string offset 'location' in
xxxxxxxxx/src/Google/Service/Resource.php on line 272

Warning: Illegal string offset 'location' in
xxxxxxxxx/src/Google/Service/Resource.php on line 274

Answer

Dimensions are not required so there for they are part of option parameters.

//Adding Dimensions
$params = array('dimensions' => 'ga:userType'); 
// requesting the data  
$data = $service->data_ga->get("ga:89798036", "2014-12-14", "2014-12-  14", "ga:users,ga:sessions", $params );  

Filters and sort can also be added to the $parms array

Comments