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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php header("Content-type: text/plain"); $email = "xxxxxx@xxx.xx"; $password = "xxxxxxx"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch,CURLOPT_HEADER,true); curl_setopt($ch, CURLOPT_POSTFIELDS,"accountType=GOOGLE&Email=$email&Passwd=$password&service=analytics&source=feronatoblog"); curl_exec($ch); curl_close($ch); ?> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | HTTP/1.1 200 OK Content-Type: text/plain Cache-control: no-cache, no-store Pragma: no-cache Expires: Mon, 01-Jan-1990 00:00:00 GMT Date: Sat, 25 Apr 2009 19:02:11 GMT X-Content-Type-Options: nosniff Content-Length: 563 Server: GFE/2.0 SID=DQAAA... LSID=DQAAA... Auth=DQAAA... |
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
5 Responses to “Playing with Google Analytics API”
Leave a Reply
Trackbacks
-
NEXTブレイクã¯Google Analytics APIã§æ±ºã¾ã‚Šï¼ | ã‚«ã‚°ã‚¢ï¼Google Analytics 活用塾:äº‹ä¾‹ã‚„ä½¿ã„æ–¹ on
June 3rd, 2009 1:03 am
[...]   Playing with Google Analytics API : Emanuele Feronato  (èªè¨¼ã‚’試ã¿ã¦ã¾ã™ï¼‰ [...]
-
2009?4?29??????? ?? ???? | ????Google Analytics ???:?????? on
June 22nd, 2009 6:40 am
[...] ?Playing with Google Analytics API : Emanuele Feronato ??Google Analytics API?????????????????????????? [...]
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)





Why not try piwik? It’s an open source project and similar to analytics.
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
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.