WUYUANS
Just for Sharing

使用google api获取analytics数据

2016年05月08日 分类:学习笔记网络Python

最近在使用google analytics统计网站访问,为了方便拉数据通过google api定时获取一些指标,在使用python sdk时出现ImportError: cannot import name SignedJwtAssertionCredentials的错误,后来改成ServiceAccountCredentials就好了,顺便记录一下google api访问google analytics的方法。

1. 准备

完整的教程官方文档里写的很详细:https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py

  1. 创建账户:https://console.developers.google.com//start/api?id=analytics&credential=client_key
  2. 创建凭据时选择服务账户密钥,P12类型,下载文件到本地。API 密钥需要定时验证,不适合在服务端用。
  3. 在google analytics账户中添加刚才生成的邮箱,阅读和分析权限。

2. 安装python客户端

用pip最简单

sudo pip install --upgrade google-api-python-client

3. 获取service

在文档里是用SignedJwtAssertionCredentials来构造service,跑不通,出现错误:

Traceback (most recent call last):
  File "/Users/qike/PycharmProjects/analytics/HelloAnalytics.py", line 4, in <module>
    from oauth2client.client import SignedJwtAssertionCredentials
ImportError: cannot import name SignedJwtAssertionCredentials

看起来像是oauth2client版本不对,SignedJwtAssertionCredentials方法没有,后来我试过修改版本,又引起了一大堆依赖冲突。。。后来使用ServiceAccountCredentials也可以构造出service。

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2

def get_service(api_name, api_version, scope, key_file_location,service_account_email):

  credentials = ServiceAccountCredentials.from_p12_keyfile(service_account_email=service_account_email,filename=key_file_location,scopes=scope)
  http = credentials.authorize(httplib2.Http())
  service = build(api_name, api_version, http=http)

  return service

4. 查询数据

通过service.data().ga().get().execute()查询数据,get的构造参考https://developers.google.com/analytics/devguides/reporting/core/v3/reference#data_request

比如我是查自定义指标1的访问量,profile_id从google analytics里查看。

service.data().ga().get(
      ids='ga:' + profile_id,
      start_date='3daysAgo',
      end_date='today',
      metrics='ga:hits',
      dimensions='ga:dimension1',
      sort='-ga:hits',
      max_results='10').execute()

5. 获取数据

上面接口execute后获得results字典,数据在rows里,参考https://developers.google.com/analytics/devguides/reporting/core/v3/reference#data_response

for row in results.get('rows'):
      print '%s:%s' % (row[0],row[1])
作者:wuyuan 本文来自Wuyuan's Blog 转载请注明,谢谢! 文章地址: https://www.wuyuans.com/blog/detail/128