Google Analytics和Python
-
06-07-2019 - |
题
我是Python的新手,我正在尝试为导入GA信息并将其解析为MySQL的应用编写扩展。有关该主题的信息量非常稀少。 Google Docs似乎只有JS和Java中的示例......
...我已经达到了我的用户可以使用SubAuth对GA进行身份验证的程度。那段代码在这里:
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)
解决方案
也许这篇文章可以提供帮助。似乎还没有特定于Analytics的绑定,因此您正在使用通用gdata。
其他提示
我已经使用GA一年多了一段时间,从2009年4月开始,我使用了Clint Ecker等人在python-googleanalytics包中提供的python绑定。到目前为止,它运作良好。
以下是获取它的地方: http://github.com/clintecker/python-googleanalytics一>。
按常规方式安装。
使用它:首先,每次访问API时都不必手动传入登录凭据,将它们放在配置文件中,如下所示:
[Credentials]
google_account_email = youraccount@gmail.com
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'方法将返回一个python 列表(在上面的例子中,绑定到变量'account'),其中包含您的数据。
您需要在应用中使用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
示例models.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>