广告是移动应用很是好的变现模式,做为开发者常常会接不少家的广告平台,每一个广告平台都有本身的报表系统,就会有一个各个平台数据汇总分析的需求,首先第一步就须要从各个广告平台获取数据,除了在web页面提供基本的数据导出功能,基本上各个平台都会提供 api 来方便数据拉取的自动化。python
admob 是 google 的移动开发者广告平台,一样也提供了完备的 api 来获取报表数据web
报表 api 使用 oauth 2.0 受权,须要先开通云平台帐号,须要填一下帐单信息。开通帐号后,在平台上建立一个 oauth 2.0 的凭证json
建立完成后,下载JSON
秘钥文件,保存为 client_secrets.json
api
sudo pip3 install google-api-python-client sudo pip3 install oauth2client
from apiclient import sample_tools scope = ['https://www.googleapis.com/auth/adsense.readonly'] client_secrets_file = 'client_secrets.json' service, _ = sample_tools.init( '', 'adsense', 'v1.4', __doc__, client_secrets_file, parents=[], scope=scope )
results = service.accounts().reports().generate( accountId='pub-131xxxxxxxxxxxxx', startDate=start.strftime('%Y-%m-%d'), endDate=end.strftime('%Y-%m-%d'), metric=metric, dimension=dimension, useTimezoneReporting=True, ).execute()
accountId
: 在 admob 平台的首页上 https://apps.admob.com/v2/home 点击本身的头像, 发布商 ID
就是 accountId
metric
: 指标项,点击、展现、收入等dimension
: 维度,日期、app、广告位、国家等startDate
: 开始日期,yyyy-mm-dd 格式endDate
: 结束时间,yyyy-mm-dd 格式useTimezoneReporting
: 使用帐户所在的时区app
代码依赖了一个 client_secrets.json
的受权文件ide
import json import datetime import argparse from apiclient import sample_tools def collect(start=None, end=None): if not start: start = datetime.datetime.now() - datetime.timedelta(days=1) if not end: end = start scope = ['https://www.googleapis.com/auth/adsense.readonly'] client_secrets_file = 'client_secrets.json' service, _ = sample_tools.init( '', 'adsense', 'v1.4', __doc__, client_secrets_file, parents=[], scope=scope ) dimension = [ "DATE", "APP_NAME", "APP_PLATFORM", "AD_UNIT_NAME", "AD_UNIT_ID", "COUNTRY_CODE" ] metric = [ "AD_REQUESTS", "CLICKS", "INDIVIDUAL_AD_IMPRESSIONS", "EARNINGS", "REACHED_AD_REQUESTS_SHOW_RATE" ] results = service.accounts().reports().generate( accountId='pub-131xxxxxxxxxxxxx', startDate=start.strftime('%Y-%m-%d'), endDate=end.strftime('%Y-%m-%d'), metric=metric, dimension=dimension, useTimezoneReporting=True, ).execute() headers = [i.lower() for i in dimension + metric] datas = [] for row in results.get('rows'): data = {} for i in range(len(row)): data[headers[i]] = row[i] datas.append(data) return datas def transform(datas): for data in datas: # 数据转化 pass return datas def serialize(datas): for data in datas: print(json.dumps(data)) def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description="""Example: python3 admob.py -s 20181025 -e 20181028 """ ) parser.add_argument( '-s', '--start', type=lambda d: datetime.datetime.strptime(d, '%Y%m%d'), help='start time', default=None ) parser.add_argument( '-e', '--end', type=lambda d: datetime.datetime.strptime(d, '%Y%m%d'), help='end time', default=None ) args = parser.parse_args() serialize(transform(collect(args.start, args.end))) if __name__ == '__main__': main()
转载请注明出处
本文连接: http://www.hatlonely.com/2018...