廣告是移動(dòng)應(yīng)用非常好的變現(xiàn)模式酒繁,作為開(kāi)發(fā)者經(jīng)常會(huì)接很多家的廣告平臺(tái)滓彰,每個(gè)廣告平臺(tái)都有自己的報(bào)表系統(tǒng),就會(huì)有一個(gè)各個(gè)平臺(tái)數(shù)據(jù)匯總分析的需求州袒,首先第一步就需要從各個(gè)廣告平臺(tái)獲取數(shù)據(jù)揭绑,除了在web頁(yè)面提供基本的數(shù)據(jù)導(dǎo)出功能,基本上各個(gè)平臺(tái)都會(huì)提供 api 來(lái)方便數(shù)據(jù)拉取的自動(dòng)化郎哭。
admob 是 google 的移動(dòng)開(kāi)發(fā)者廣告平臺(tái)他匪,同樣也提供了完備的 api 來(lái)獲取報(bào)表數(shù)據(jù)
創(chuàng)建 API 憑證
報(bào)表 api 使用 oauth 2.0 授權(quán),需要先開(kāi)通云平臺(tái)賬號(hào)夸研,需要填一下賬單信息邦蜜。開(kāi)通賬號(hào)后,在平臺(tái)上創(chuàng)建一個(gè) oauth 2.0 的憑證
創(chuàng)建完成后亥至,下載JSON
秘鑰文件悼沈,保存為 client_secrets.json
使用 google api 的 python 庫(kù)
sudo pip3 install google-api-python-client
sudo pip3 install oauth2client
初始化服務(wù)
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
)
調(diào)用服務(wù) api
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 平臺(tái)的首頁(yè)上 https://apps.admob.com/v2/home 點(diǎn)擊自己的頭像, 發(fā)布商 ID
就是 accountId
metric
: 指標(biāo)項(xiàng)姐扮,點(diǎn)擊絮供、展示、收入等
dimension
: 維度茶敏,日期壤靶、app、廣告位惊搏、國(guó)家等
startDate
: 開(kāi)始日期贮乳,yyyy-mm-dd 格式
endDate
: 結(jié)束時(shí)間忧换,yyyy-mm-dd 格式
useTimezoneReporting
: 使用賬戶所在的時(shí)區(qū)
參考代碼
代碼依賴了一個(gè) client_secrets.json
的授權(quán)文件
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:
# 數(shù)據(jù)轉(zhuǎn)化
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()
參考鏈接
- google 廣告平臺(tái)報(bào)表 api: https://developers.google.com/adsense/management/v1.4/reference/accounts/reports/generate
- google 廣告平臺(tái)指標(biāo)和維度:
https://developers.google.com/adsense/management/metrics-dimensions - google oauth 授權(quán): https://developers.google.com/adwords/api/docs/guides/authentication
- How to use Google Adsense API to download Adsense data: https://009co.com/?p=389
- google admob 平臺(tái): https://apps.admob.com/v2/home
- google 云平臺(tái): https://console.cloud.google.com/home/dashboard
- adsense 平臺(tái)憑據(jù): https://console.cloud.google.com/apis/credentials
轉(zhuǎn)載請(qǐng)注明出處
本文鏈接:http://www.hatlonely.com/2018/11/01/admob-%E5%B9%BF%E5%91%8A%E5%BC%80%E5%8F%91%E8%80%85%E6%8A%A5%E8%A1%A8-api/