2009-10-27 135 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>