Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Retrieving Data from Google Analytics API using PHP

DZone's Guide to

Retrieving Data from Google Analytics API using PHP

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Recently I started playing with the google analytics API, looking at ways to bring analytics onto dashboards and generate simple reports from the data in there. Very shortly after I started to look at the API, I had working data retrieval, so I thought I'd share my experiences (and code!).
I am using OAuth for authentication, and I blogged about using Google and OAuth previously so feel free to pop over there to find out more about that. I used the pecl_oauth extension to make all the requests to analytics, but even for another technology choice, the concepts apply so keep reading :)

Analytics Parameters


The data format for analytics is fairly comprehensive and easy to understand - see their data feed reference document for a clear outline of the parameters that can be passed and what format they can take. I am using the ids parameter (which is the tableId I showed retrieval of in the earlier blog post), the date parameters, metrics, dimensions and sort - so my code looks something like this, with the parameters set in $query_data.
        $oauth = new OAuth($oauth_consumer_key, $oauth_consumer_secret);
$oauth->setToken($token, $token_secret);

$oauth->fetch('https://www.google.com/analytics/feeds/data', $query_data);
$result = $oauth->getLastResponse();

Combining Parameters


The biggest problem I'm encountering with the analytics is understanding how to combine the dimension and metrics parameters to get useful data. It is pretty easy to report on visitors by day/week/month, over a given date range, but getting more meaningful and useful statistics is still a work in progress! There is some documentation, which is exhaustive, but doesn't link the available data to the sorts of questions users actually want to ask. If anyone has any suggestions on good combinations to use, I'd absolutely love to hear them - leave me a comment and I'll post again if/when have some better data patterns!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}