首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google :如何发送批处理请求?

Google :如何发送批处理请求?
EN

Stack Overflow用户
提问于 2022-11-07 15:22:47
回答 1查看 62关注 0票数 1

我使用Google v11上传转换和调整转换。我每天发送数百个转换,并希望开始发送批处理请求。

我一直跟踪谷歌的文档,我上传/调整转换的方式与他们所说的完全一样。https://developers.google.com/google-ads/api/docs/conversions/upload-clicks https://developers.google.com/google-ads/api/docs/conversions/upload-adjustments

我找不到任何关于如何发送批处理请求的很好的解释或例子:https://developers.google.com/google-ads/api/reference/rpc/v11/BatchJobService

下面是我的代码,一个如何调整数百个转换的例子。如果能解释如何处理批处理请求,将不胜感激。

代码语言:javascript
复制
# Adjust the conversion value of an existing conversion, via Google Ads API
def adjust_offline_conversion(
    client,
    customer_id,
    conversion_action_id,
    gclid,
    conversion_date_time,
    adjustment_date_time,
    restatement_value,
    adjustment_type='RESTATEMENT'):

    # Check that gclid is valid string else exit the function
    if type(gclid) is not str:
        return None
    # Check if datetime or string, if string make as datetime
    if type(conversion_date_time) is str:
        conversion_date_time = datetime.strptime(conversion_date_time, '%Y-%m-%d %H:%M:%S')
    # Add 1 day forward to conversion time to avoid this error (as explained by Google: "The Offline Conversion cannot happen before the ad click. Add 1-2 days to your conversion time in your upload, or check that the time zone is properly set.")
    to_datetime_plus_one = conversion_date_time + timedelta(days=1)
    # If time is bigger than now, set as now (it will be enough to avoid the original google error, but to avoid a new error since google does not support future dates that are bigger than now)
    to_datetime_plus_one = to_datetime_plus_one if to_datetime_plus_one < datetime.utcnow() else datetime.utcnow()
    # We must convert datetime back to string + add time zone suffix (+00:00 or -00:00 this is utc) **in order to work with google ads api**
    adjusted_string_date = to_datetime_plus_one.strftime('%Y-%m-%d %H:%M:%S') + "+00:00"

    conversion_adjustment_type_enum = client.enums.ConversionAdjustmentTypeEnum
    # Determine the adjustment type.
    conversion_adjustment_type = conversion_adjustment_type_enum[adjustment_type].value

    # Associates conversion adjustments with the existing conversion action.
    # The GCLID should have been uploaded before with a conversion
    conversion_adjustment = client.get_type("ConversionAdjustment")
    conversion_action_service = client.get_service("ConversionActionService")
    conversion_adjustment.conversion_action = (
        conversion_action_service.conversion_action_path(
            customer_id, conversion_action_id
        )
    )
    conversion_adjustment.adjustment_type = conversion_adjustment_type
    conversion_adjustment.adjustment_date_time = adjustment_date_time.strftime('%Y-%m-%d %H:%M:%S') + "+00:00"

    # Set the Gclid Date
    conversion_adjustment.gclid_date_time_pair.gclid = gclid
    conversion_adjustment.gclid_date_time_pair.conversion_date_time = adjusted_string_date

    # Sets adjusted value for adjustment type RESTATEMENT.
    if conversion_adjustment_type == conversion_adjustment_type_enum.RESTATEMENT.value:
        conversion_adjustment.restatement_value.adjusted_value = float(restatement_value)

    conversion_adjustment_upload_service = client.get_service("ConversionAdjustmentUploadService")
    request = client.get_type("UploadConversionAdjustmentsRequest")
    request.customer_id = customer_id
    request.conversion_adjustments = [conversion_adjustment]
    request.partial_failure = True
    response = (
        conversion_adjustment_upload_service.upload_conversion_adjustments(
            request=request,
        )
    )
    conversion_adjustment_result = response.results[0]
    print(
        f"Uploaded conversion that occurred at "
        f'"{conversion_adjustment_result.adjustment_date_time}" '
        f"from Gclid "
        f'"{conversion_adjustment_result.gclid_date_time_pair.gclid}"'
        f' to "{conversion_adjustment_result.conversion_action}"'
    )



# Iterate every row (subscriber) and call the "adjust conversion" function for it
df.apply(lambda row: adjust_offline_conversion(client=client
                                                   , customer_id=customer_id
                                                   , conversion_action_id='xxxxxxx'
                                                   , gclid=row['click_id']
                                                   , conversion_date_time=row['subscription_time']
                                                   , adjustment_date_time=datetime.utcnow()
                                                   , restatement_value=row['revenue'])
                                                   , axis=1)
EN

回答 1

Stack Overflow用户

发布于 2022-11-11 17:01:23

我设法用以下方式解决了这个问题:

在批处理中不支持转换上载和调整,因为它们没有列出这里

但是,可以在一个请求中上载多个转换,因为conversions[]字段(list)可以填充多个转换,而不仅仅是我错误地认为的单个转换。

因此,如果您正在上传转换/调整转换,您可以通过这种方式批量上传它们:

而不是上传一个转换:

代码语言:javascript
复制
request.conversions = [conversion]

上传几个:

代码语言:javascript
复制
request.conversions = [conversion_1, conversion_2, conversion_3...]

以同样的方式进行转换调整上传:

代码语言:javascript
复制
request.conversion_adjustments = [conversion_adjustment_1, conversion_adjustment_2, conversion_adjustment_3...]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74348944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档