gpt4 book ai didi

python - 关于Google Analytics的一个误解示例(上传数据)

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:48 26 4
gpt4 key购买 nike

我对 this script example 有疑问。有一个代码片段:

daily_upload = analytics.management().uploads().uploadData(
accountId='123456',
webPropertyId='UA-123456-1',
customDataSourceId='9876654321',
media_body=media).execute()

我不知道我可以从哪个模块导入 analytics.management() 方法。

最佳答案

import sys
import logging
from pprint import pprint

from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
DISCOVERY_URI = 'https://analyticsreporting.googleapis.com/$discovery/rest'
CLIENT_SECRETS_PATH = 'client_id.json'
VIEW_ID = ''


logger = logging.getLogger(name)


def initialize_analyticsreporting():
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH, scope=SCOPES,
message=tools.message_if_missing(CLIENT_SECRETS_PATH))

storage = file.Storage('analyticsreporting.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage)
http = credentials.authorize(http=httplib2.Http())

analytics = build(
'analytics', 'v4',
http=http, discoveryServiceUrl=DISCOVERY_URI)

return analytics


def main():
analytics = initialize_analyticsreporting()
data_f = open("report_data.csv", "w")

page_token = "7000"
request = {
'reportRequests': [{
'viewId': VIEW_ID,
'dateRanges': [{
'startDate': '2017-01-01',
'endDate': '2018-02-12',
}],
'metrics': [{'expression': 'ga:users'}],
"dimensions": [
{"name": "ga:dimension2"},
{"name": "ga:dimension3"},
],

"pageToken": page_token,
}]
}

for n, f in enumerate(request['reportRequests'][0]['dimensions']):
data_f.write("%s\t" % f["name"])

data_f.write("pageToken\r\n")

while True:
response = analytics.reports().batchGet(body=request).execute()

rows = response['reports'][0]['data']['rows']
print("loaded %d rows..." % len(rows))

for row in rows:
str = ''
for val in row['dimensions']:
str += val + '\t'

data_f.write(str + page_token + '\r\n')

if "nextPageToken" not in response['reports'][0]:
break

page_token = response['reports'][0]['nextPageToken']
request['reportRequests'][0]['pageToken'] = page_token

data_f.close()


if name == 'main':
main()

关于python - 关于Google Analytics的一个误解示例(上传数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49134195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com