首页 > 解决方案 > 如何使用 Google API 和 Python 从 Google Ads 广告系列中删除位置条件

问题描述

我可以使用以下相同的代码将位置条件添加到活动中,方法是替换campaign_criterion = campaign_criterion_operation.removecampaign_criterion = campaign_criterion_operation.create

但是,我无法使用以下代码根据位置 ID 删除位置。我浏览了文档,但找不到任何有用的东西。任何帮助将不胜感激。

def main(client, customer_id, campaign_id, location_id):
    campaign_criterion_service = client.get_service("CampaignCriterionService")

    operations = [
        _create_location_op(client, customer_id, campaign_id, location_id)
    ]





    print(operations)

    campaign_criterion_response = (
        campaign_criterion_service.mutate_campaign_criteria(
            customer_id=customer_id, operations=operations
        )
    )

    for result in campaign_criterion_response.results:
        print(f'Added campaign criterion "{result.resource_name}".')


def _create_location_op(client, customer_id, campaign_id, location_id):
    campaign_service = client.get_service("CampaignService")
    geo_target_constant_service = client.get_service("GeoTargetConstantService")


    # campaign criterion.
    campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
    campaign_criterion = campaign_criterion_operation.remove
    campaign_criterion.campaign = campaign_service.campaign_path(
        customer_id, campaign_id
    )

    # Besides using location_id, you can also search by location names from
    # GeoTargetConstantService.suggest_geo_target_constants() and directly
    # apply GeoTargetConstant.resource_name here. An example can be found
    # in get_geo_target_constant_by_names.py.

    campaign_criterion.location.geo_target_constant = (
        geo_target_constant_service.geo_target_constant_path(location_id)
    )

    return campaign_criterion_operation

def otherMain():
    # GoogleAdsClient will read the google-ads.yaml configuration file in the
    # home directory if none is specified.
    googleads_client = GoogleAdsClient.load_from_storage(path='XXX')
    campaign_id = XX
    customer_id = XX
    location_id = XX


    try:
        main(googleads_client, customer_id, campaign_id , location_id)
    except GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{ex.error.code().name}" and includes the following errors:'
        )
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

我得到的错误是:

    campaign_criterion.campaign = campaign_service.campaign_path(
AttributeError: 'str' object has no attribute 'campaign'

标签: pythongoogle-api

解决方案


推荐阅读