Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Playing with Google Analytics API

I think you all know Google Analytics.

A Google Analytics API has long been one of the most widely anticipated (and wanted from developers) features. On April 21st, 2009 the official blog announced that the Google Analytics Data Export API beta is now publicly available to all Analytics users.

The API will allow developers to extend Google Analytics in new and creative ways and integrate Google Analytics into their existing products and create standalone applications that they sell.

Read more at the official post.

So I think it’s time to play a bit with this new toy.

Today we’ll learn how to autenticate to the service.

I made the script you are about to see reading the information at this page.

Line 3: Setting the header to text/plain because I am going to output plain text instead of html

Lines 5-6: Assigning to two variables my Analytics email and password. Obviously replace the xxx with your correct Analytics email and password

Line 8: Initializing a cURL handle. I am using it to communicate to Google servers using HTTP protocol and pass variables in POST mode. More information about the cURL library at this link.

Lines 9-12: Setting some options for the cURL handle. CURLOPT_URL is the URL to fetch, CURLOPT_POST to perform a HTTP POST, CURLOPT_HEADER to include the header in the output and CURLOPT_POSTFIELDS contains the full data to post in a HTTP “POST” operation.

This way I am simulating an HTML form submission to https://www.google.com/accounts/ClientLogin using POST method.

Let’s take a look at the variables I am sending:

accountType: Type of account to be authenticated. The default is GOOGLE (I found it’s not case sentitive).

Email: The user’s email address.

Passwd: The user’s password.

service: The Analytics service name is analytics.

source: A string identifying your client application in the form companyName-applicationName-versionID (actually you can write whatever you want).

Lines 13 and 14 perform and close the cURL session.

This is the result if everything went fine:

The Auth code is what we need to retrieve account and report data. I’ll show you how to do it next time.

At the moment I am integrating the Analytics in a control panel we provide to some customers, in order to give them access to all information from a single page.

Do you have a creative use of this API in your mind? Look at some examples at this link and you will agree this is a great feature

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 1.00 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. Alexander Trust

    on April 25, 2009 at 11:04 pm

    Why not try piwik? It’s an open source project and similar to analytics.

  2. Emanuele Feronato

    on April 26, 2009 at 10:33 am

    at least two reasons:

    1) piwik writes on your mysql db and uses your php resources, analytics does not

    2) most customers already use analytics on their own so saying your software is 100% analytics compatible isn’t the same thing as saying is piwik comatible

  3. Scot

    on May 13, 2009 at 10:03 pm

    Thank you for the tutorial. I am looking to find out what to do next – I have the auth code and want to make another request. I personally feel that Google’s API sucks.

  4. NEXTブレイクはGoogle Analytics APIで決まり! | カグア!Google Analytics 活用塾:事例や使い方

    on June 3, 2009 at 1:03 am

    [...]   Playing with Google Analytics API : Emanuele Feronato  (認証を試みてます) [...]

  5. 2009?4?29??????? ?? ???? | ????Google Analytics ???:??????

    on June 22, 2009 at 6:40 am

    [...] ?Playing with Google Analytics API : Emanuele Feronato ??Google Analytics API?????????????????????????? [...]

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×