代码之家  ›  专栏  ›  技术社区  ›  Sara Chipps

谷歌分析和python

  •  5
  • Sara Chipps  · 技术社区  · 15 年前

    我在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)
    

    所以,下一步就是获取数据。我找到了这个库:(注意,用户界面很冒犯) http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.analytics.html 但是,我发现很难导航。看起来我应该是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)
    
    3 回复  |  直到 10 年前
        1
  •  3
  •   John Paulett    15 年前

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

        2
  •  2
  •   doug    14 年前

    我已经使用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
    

    将此文件命名为“.pythongogleanalanalytics”,并将其放在主目录中。

    从交互式提示类型:

    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
  •  0
  •   maQbex    10 年前

    应用程序中需要3个文件。客户机\u 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>