Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌分析和Python_Python_Google Analytics_Analytics_Google Analytics Api - Fatal编程技术网

谷歌分析和Python

谷歌分析和Python,python,google-analytics,analytics,google-analytics-api,Python,Google Analytics,Analytics,Google Analytics Api,我是Python的新手,我正在尝试编写一个应用程序的扩展,将GA信息导入并解析到MySQL中。关于这个话题的信息少得可怜。谷歌文档似乎只有JS和Java的例子 …我的用户可以使用SubAuth对GA进行身份验证。代码如下: import gdata.service import gdata.analytics from django import http from django import shortcuts from django.shortcuts import render_to_

我是Python的新手,我正在尝试编写一个应用程序的扩展,将GA信息导入并解析到MySQL中。关于这个话题的信息少得可怜。谷歌文档似乎只有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是令人讨厌的) 然而,我发现很难导航。看起来我应该是gdata.analytics.AnalyticsDataEntry.getDataEntry(),但我不确定它要求我传递什么

我希望能朝着正确的方向努力。我觉得我已经用尽谷歌寻找一个工作的例子

谢谢

编辑:我已经走得更远了,但我的问题仍然没有解决。下面的方法返回数据(我相信)。。。。我得到的错误是:“'str'对象没有属性'\u 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)

也许我能帮上忙。似乎还没有特定于分析的绑定,所以您正在使用通用gdata。

我已经使用GA一年多了,大约从2009年4月开始,我使用了由Clint Ecker等人提供的名为python googleanalytics的包中提供的python绑定。到目前为止,它工作得相当好

在这里可以找到它:

按常规方式安装

要使用它:首先,为了不必在每次访问API时手动传递登录凭据,请将它们放入如下配置文件:

[Credentials]
google_account_email = youraccount@gmail.com
google_account_password = yourpassword
将此文件命名为“.pythongooglenalytics”,并将其放在主目录中

并从交互式提示类型:

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'])
来自googleanalytics导入连接
导入日期时间
connection=connection()#将id和pw作为字符串传入**如果**不在配置文件中
account=connection.get_account()
开始日期=日期时间日期(2009年12月1日)
end_data=datetime.date(2009,12,13)
#account对象执行此操作,请指定所需的数据w/
#“指标”和“维度”;有关示例,请参见“USAGE.md”文件
获取数据(开始日期=开始日期,结束日期=结束日期,指标=['访问])

“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
Example 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>
导入google\u auth
service=googleauth.initialize_service()
def总就诊次数(自我):
object=AnalyticsData.objects.get(utm\u source=self.utm\u source)
试用=查询(object.date.startdate、object.date.enddate、object.utm_源、ga:sessions)
result=service.data().ga().get(id='ga:',start_date=trial.startdate,end_date=trial.enddate,filters=trial.filter,metrics=trial.metrics)。execute()
总访问量=结果。获取('行')