2009-10-27 144 views
5

我是Python新品牌,我正在尝试为导入GA信息并将其解析为MySQL的应用程序编写扩展。关于这个话题的信息非常稀少。该谷歌文档似乎只在JS和Java例子...谷歌分析和Python

...我得到的地步,我的用户可以到GA使用SubAuth验证。代码在这里:

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) 

所以,下一步是获取数据。我找到了这个库:(小心,UI很冒犯)http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.analytics.html 但是,我发现很难浏览。看来我应该是gdata.analytics.AnalyticsDataEntry.getDataEntry(),但我不确定它是什么要求我通过它。

我很乐意推动正确的方向。我觉得我已经耗尽谷歌寻找一个工作的例子。

谢谢!

编辑:我得到了更远,但我的问题仍然没有解决。下面的方法返回数据(我相信)....我得到的错误是:“'str'对象没有属性'_BecomeChildElement'”我相信我正在返回一个饲料?但是,我不知道如何钻取它。有没有办法让我检查这个对象?

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) 

回答

3

也许this post可以帮忙。似乎还没有Google Analytics特定的绑定,所以您正在使用通用的gdata。

2

我一直在使用GA一年多一点,并且从2009年4月开始,我已经使用了由Clint Ecker等人在一个名为python-googleanalytics的软件包中提供的python绑定。到目前为止,它工作得很好。

这里是在哪里得到它:http://github.com/clintecker/python-googleanalytics

以通常的方式安装它。

要使用它:首先,让你不必在你的登录凭据每次访问API时手动传球,把它们放在一个配置文件,如下所示:

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

命名此文件'.pythongoogleanalytics'并将其放入您的主目录。

而从一个交互式提示类型:

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']) 

在“get_account”方法将返回一个蟒列表(在上面情况下,绑定到变量“帐户”),其中包含的数据。

+0

这似乎不再适用 – Eli 2013-03-22 14:10:12

0

你需要在应用程序内的3个文件。 client_secrets.json,analytics.dat和google_auth.py。

在应用内创建模块Query.py:

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 

实施例的模型。py:#有以下功能

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>