Google AnalyticsとPython
-
06-07-2019 - |
質問
私はPythonが初めてで、GA情報をインポートしてMySQLに解析するアプリの拡張機能を作成しようとしています。このトピックに関する情報は、わずかにまばらにしかありません。 Googleドキュメントには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)
解決
おそらくこの投稿が役に立ちます。アナリティクス固有のバインディングはまだないようですので、汎用gdataで作業しています。
他のヒント
私は1年以上GAを使用していますが、2009年4月頃から、Clint Ecker et alによる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>