views:

807

answers:

2

I'm brand new at Python and I'm trying to write an extension to an app that imports GA information and parses it into MySQL. There is a shamfully sparse amount of infomation on the topic. The Google Docs only seem to have examples in JS and Java...

...I have gotten to the point where my user can authenticate into GA using SubAuth. That code is here:

import gdata.service
import gdata.analytics  
from django import http
from django import shortcuts
from django.shortcuts import render_to_response

def authorize(request):
    next = 'http://localhost:8000/authconfirm'
    scope = 'https://www.google.com/analytics/feeds'
    secure = False  # set secure=True to request secure AuthSub tokens
    session = False
    auth_sub_url = gdata.service.GenerateAuthSubRequestUrl(next, scope, secure=secure, session=session)
    return http.HttpResponseRedirect(auth_sub_url)

So, step next is getting at the data. I have found this library: (beware, UI is offensive) http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.analytics.html However, I have found it difficult to navigate. It seems like I should be gdata.analytics.AnalyticsDataEntry.getDataEntry(), but I'm not sure what it is asking me to pass it.

I would love a push in the right direction. I feel I've exhausted google looking for a working example.

Thank you!!

EDIT: I have gotten farther, but my problem still isn't solved. The below method returns data (I believe).... the error I get is: "'str' object has no attribute '_BecomeChildElement'" I believe I am returning a feed? However, I don't know how to drill into it. Is there a way for me to inspect this object?

def auth_confirm(request):
    gdata_service = gdata.service.GDataService('iSample_acctSample_v1.0')
    feedUri='https://www.google.com/analytics/feeds/accounts/default?max-results=50'
    # request feed
    feed = gdata.analytics.AnalyticsDataFeed(feedUri)
    print str(feed)
+1  A: 

Maybe this post can help out. Seems like there are not Analytics specific bindings yet, so you are working with the generic gdata.

John Paulett
A: 

I've been using GA for a little over a year now and since about April 2009, i have used python bindings supplied in a package called python-googleanalytics by Clint Ecker et al. So far, it works quite well.

Here's where to get it: http://github.com/clintecker/python-googleanalytics.

Install it the usual way.

To use it: First, so that you don't have to manually pass in your login credentials each time you access the API, put them in a config file like so:

[Credentials]
google_account_email = [email protected]
google_account_password = yourpassword

Name this file '.pythongoogleanalytics' and put it in your home directory.

And from an interactive prompt type:

from googleanalytics import Connection
import datetime
connection = Connection()     # pass in id & pw as strings **if** not in config file
account = connection.get_account(<*your GA profile ID goes here*>)
start_date = datetime.date(2009, 12, 01)
end_data = datetime.date(2009, 12, 13)
# account object does the work, specify what data you want w/ 
# 'metrics' & 'dimensions'; see 'USAGE.md' file for examples
account.get_data(start_date=start_date, end_date=end_date, metrics=['visits'])

The 'get_account' method will return a python list (in above instance, bound to the variable 'account'), which contains your data.

doug