5

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)
4

3 回答 3

3

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

于 2009-10-28T02:05:56.387 回答
2

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 = youraccount@gmail.com
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.

于 2009-12-13T21:37:02.347 回答
0

You need 3 files within the app. client_secrets.json, analytics.dat and google_auth.py.

Create a module Query.py within the app:

class Query(object):
    def __init__(self, startdate, enddate, filter, metrics):
        self.startdate = startdate.strftime('%Y-%m-%d')
        self.enddate = enddate.strftime('%Y-%m-%d')
        self.filter = "ga:medium=" + filter  
        self.metrics = metrics

Example models.py: #has the following function

import google_auth
service = googleauth.initialize_service()
def total_visit(self):
    object = AnalyticsData.objects.get(utm_source=self.utm_source)
    trial = Query(object.date.startdate, object.date.enddate, object.utm_source, ga:sessions")
    result = service.data().ga().get(ids = 'ga:<your-profile-id>', start_date =   trial.startdate, end_date = trial.enddate, filters= trial.filter, metrics = trial.metrics).execute()
    total_visit = result.get('rows')
    <yr save command, ColumnName.object.create(data=total_visit) goes here>
于 2014-05-26T16:24:56.847 回答