我在Google Analytics API(Python)中使用批处理请求。链接到批处理:https://developers.google.com/api-client-library/python/guide/batch
当通过.add()的所有记录均正确(有效)时,批处理工作正常。当一个或多个值无效时,所有记录的批处理将失败。
我添加了一个回调函数来处理该错误,并且我看到批处理中的所有记录的BAtching请求都失败了(与仅无效记录相反)。有没有一种方法可以处理错误并跳过无效的行/记录,并继续处理批处理中的其余记录?
以下是我使用的示例代码和错误消息:
def add_user_callback(request_id, response, exception):
if exception:
print "error :",exception
else:
print "successful"
def main():
## code to set the account, property and other variables
batch.add(service.management().webpropertyUserLinks().insert(
accountId=account_id,
webPropertyId=property_at,
body={
'permissions': {
'local': [
'READ_AND_ANALYZE'
]
},
'userRef': {
'email': '[email protected]'
}
}))
batch.add(service.management().webpropertyUserLinks().insert(
accountId=account_id,
webPropertyId=property_at,
body={
'permissions': {
'local': [
'READ_AND_ANALYZE'
]
},
'userRef': {
'email': '[email protected]' ## i used a dummy id : [email protected]
}
}))
batch.execute()
#Error :
#error : <HttpError 400 when requesting https://www.googleapis.com/analytics/v3/management/accounts/62974313/webproperties/UA-62974313-35/entityUserLinks?alt=json returned "Value for field user.email = [email protected] is not valid.">
#error : <HttpError 400 when requesting https://www.googleapis.com/analytics/v3/management/accounts/62974313/webproperties/UA-62974313-11/entityUserLinks?alt=json returned "Value for field user.email = [email protected] is not valid.">
如果您需要更多信息,请告诉我。
参考方案
假设您有要添加到配置文件users
中的配置文件的用户列表。
您可以使用以下回调函数删除不良电子邮件:
def call_back(request_id, response, exception):
if exception is not None:
if isinstance(exception, HttpError):
message = json.loads(exception.content)['error']['message']
bad = 'Value for field user.email = (\S*) is not valid.'
match = re.match(bad, message)
if match:
bad_user = match.group(1)
if bad_user in users:
users.remove(bad_user)
else:
print response
在所有失败的呼叫返回之后,您可以通过遍历用户并构造新的批处理请求来再次尝试再次执行批处理调用:
batch = BatchHttpRequest(callback=call_back)
for user in users:
request = analytics.management().profileUserLinks().insert(
accountId=ACCOUNT_ID,
webPropertyId=PROFILE_ID,
profileId=profile,
body={
'permissions': {'local': ['READ_AND_ANALYZE']},
'userRef': {'email': user}
}
)
batch.add(request, request_id=PROFILE_ID + user)
batch.execute()
使用Google Analytics(分析)API及其客户端google-api-python-client创建一个Web应用程序? - python我正在浏览googles的api python-client-library和google analytics api。我能够执行官方文档中提到的所有步骤,但是随后我有了一些疑问。由于我以前从未做过此类事情,因此,我需要您的宝贵建议/提示。我的目标:想要使用Python(使用django / flask)和google-api-python-client设计…
Google Analytics API fromdate,todate。如何从开始跟踪时开始? - c#我是C#的新手,正在创建一个控制台应用程序,该应用程序使用API连接到Google Analytics(分析)。我知道了,因此它可以将控制台中捕获的数据写入并导出为CSV,但是我在这里指定了日期:static void Main(string[] args) { Authenticate(); **getVisits("2013-01-01…
Google Analytics API-不完整的报告批次 - php因此,我们有一个有效的API,可以在特定日期范围内获取报告。最近,我们尝试获取行数达到40,000个结果的报告。我们从API得到响应,并且能够提取前1000行。现在的问题是分页。查询设置为每批检索1,000行,因此总共40批(基于40,000行)。根据analytics api pagination,从api响应中检索nextPageToken并将其设置为下…
如果我的GAE应用程序应使用Analytics API,是否需要OAuth流程? - python我想在我的GAE网站上显示我的android应用程序的日常用户(使用Google Analytics(分析)进行跟踪),他们都在同一帐户下。我想为此使用Analytics API。我正在阅读Hello Analytics API tutorial,看起来我需要OAuth授权我的应用才能从Google Analytics(分析)获取数据。它看起来很复杂,有没有…
Google Analytics PHP API:找不到目标 - php在我公司,我们决定使用Google Analytics(分析)来获取有关访问者,进入渠道等的一些有趣指标...当访问者提交联系表单时,我创建了一个“触发”的目标,一切工作正常,我什至创建了一个细分来预览使用该表单的人与其他人之间的区别。使用PHP API,我有自己的仪表板表,其中逐个列出了每个会话的一些详细信息,其中包括:每个访问的URL约会时间如果来自Ad…