使用Python从Google广告中收回转换

2024-06-14 14:08:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是收回我先前从谷歌分析导入谷歌广告的几个转换。我正在使用来自documentation of the Google Ads API的代码示例“上传转换调整”运行此命令时,没有异常,但出现“部分故障”。

这是我的代码(基本上是示例代码,但略为缩短):

from google.ads.googleads.client import GoogleAdsClient


customer_id = '1234567890'  # changed
conversion_action_id = '1234567890'
gclid = 'ABCabc123-456_xyz'  # changed
adjustment_type = 'RETRACTION'
conversion_date_time = '2021-05-19 10:31:22+02:00'
adjustment_date_time = '2021-05-19 10:31:22+02:00'
restatement_value = ''

credentials = {
    "developer_token": "Xyz-123",  # changed
    "refresh_token": "1//23-abc.apps.googleusercontent.com",  # changed
    "client_secret": "XYZ1234",  # changed
    "login_customer_id": "9876543210"}  # changed

client = GoogleAdsClient.load_from_dict(credentials)


# following def_main of the example code:

conversion_adjustment_type_enum = client.get_type(
    "ConversionAdjustmentTypeEnum"
)
# Determine the adjustment type.
conversion_adjustment_type = (
    conversion_adjustment_type_enum._pb.ConversionAdjustmentType.Value(
        adjustment_type
    )
)

# 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

# Set the Gclid Date
conversion_adjustment.gclid_date_time_pair.gclid = gclid
conversion_adjustment.gclid_date_time_pair.conversion_date_time = (
    conversion_date_time
)

# I obviously don't need that for RETRACTION:    
# Sets adjusted value for adjustment type RESTATEMENT.
# if (
#     restatement_value
#     and conversion_adjustment_type
#     == conversion_adjustment_type_enum.ConversionAdjustmentType.RESTATEMENT
# ):
#     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}"'
)

结果是:

Uploaded conversion that occurred at "" from Gclid "" to ""

因此返回的变量似乎是空的。我想那是不对的

我添加了以下代码,其灵感来自于我在documentation of "Partial Failure"中找到的示例代码(在“上传转换调整”文档的末尾提到):

partial_failure = getattr(response, "partial_failure_error", None)
code = getattr(partial_failure, "code", None)
message = getattr(partial_failure, "message", None)
print(code, message)

结果是:

3 This customer does not have a conversion action of a supported ConversionActionType that matches the conversion action provided., at conversion_adjustments[0].conversion_action

“部分故障”文档指向https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto,其中说明:

  // The client specified an invalid argument.  Note that this differs
  // from `FAILED_PRECONDITION`.  `INVALID_ARGUMENT` indicates arguments
  // that are problematic regardless of the state of the system
  // (e.g., a malformed file name).
  //
  // HTTP Mapping: 400 Bad Request
  INVALID_ARGUMENT = 3;

参数adjustment_date_time可能是错误的,并且不应该与conversion_date_time相同,但稍后会出现吗?如果我改变这一点,结果仍然是一样的

这仍然有什么问题,我如何实际检查相关转换是否仍然存在或已经撤回?


Tags: oftheclientiddatetimevaluetype