2012-04-14(Sat)
Google Analyticsデータを取得する with Python (1)
Google Analyticsのデータを取得してみる。
まずはテーブルIDとかフィールドIDとかGoogleAnalyticsの情報を取得する。
クライアントログインはここを見ればだいたい理解できた。
Pythonコード
出力は以下の通り。
出力結果
次は、テーブルIDを使って実際にデータを取り出してみる。
参考:
ClientLogin in the Google Data Protocol Client Libraries
Googlecode Pydocs
ClientLogin for Installed Applications
knaka20blue様のまずは ids (tableId) を取得する
まずはテーブルIDとかフィールドIDとかGoogleAnalyticsの情報を取得する。
クライアントログインはここを見ればだいたい理解できた。
Pythonコード
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys_
import logging
email = 'hoge@gmail.com'
password = 'passcode'
application_name = 'get-gdata-info'
class GdataLogin():
def login_v1(self):
import gdata.analytics.service
client = gdata.analytics.service.AccountsService()
try:
logging.debug('login_v1 start')
auth_token = client.ClientLogin(email, password, source=application_name)
logging.info(' auth_token:%s', auth_token)
return client
except gdata.service.CaptchaRequired:
logging.error('Please visit %s', challenge.captcha_url)
answer = raw_input('Answer to the challenge? ')
auth_token = client.ClientLogin(email, password, source=application_name,
captcha_token=client.captcha_token,
captcha_response=answer)
logging.info(' auth_token:%s', auth_token)
return client
except gdata.service.BadAuthentication:
logging.error('Users credentials were unrecognized')
raise
except gdata.service.Error:
logging.error('Login Error')
raise
def info_view(self, client):
try:
l_account = client.GetAccountList()
for entry in l_account.entry:
logging.info('id:%s', entry.id.text)
for info in entry.tableId:
logging.info(' tableId:%s', info.text)
logging.info(' updated%s:', entry.updated.text)
logging.info(' accountId:%s', entry.accountId)
logging.info(' accountName:%s', entry.accountName)
logging.info(' profileId:%s', entry.profileId)
logging.info(' webPropertyId:%s', entry.webPropertyId)
logging.info(' currency:%s', entry.currency)
logging.info(' timezone:%s', entry.timezone)
logging.info(' account.title.text:%s', entry.title.text)
return
except:
logging.error('notice_mail %s', sys.exc_info())
raise
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
gl = GdataLogin()
client = gl.login_v1()
gl.info_view(client)
出力は以下の通り。
出力結果
INFO:root:id:http://www.google.com/analytics/feeds/accounts/ga:11111111
INFO:root: tableId:ga:11111111
INFO:root: updated2012-03-16T01:32:10.540-07:00:
INFO:root: accountId:22222222
INFO:root: accountName:テストサイト
INFO:root: profileId:33333333
INFO:root: webPropertyId:UA-44444444-1
INFO:root: currency:USD
INFO:root: timezone:America/Los_Angeles
INFO:root: account.title.text:テストサイト
次は、テーブルIDを使って実際にデータを取り出してみる。
参考:
ClientLogin in the Google Data Protocol Client Libraries
Googlecode Pydocs
ClientLogin for Installed Applications
knaka20blue様のまずは ids (tableId) を取得する
- 関連記事
-
- Google AnalyticでSocial Button Tracking
- Google Analyticsデータを取得する with Python (1)
スポンサーサイト