How to set campaign targeting_setting with python google-ads library

230 views
Skip to first unread message

Shawn Hsiao

unread,
Jan 24, 2024, 3:06:45 AM1/24/24
to Google Ads API and AdWords API Forum
Hello,

I was using the below code to try to modify campaign.targeting_setting but failed,
and I could not find examples of using google-ads python library to achieve this. 
Could you suggest valid ways to achieve this? Thanks in advance.

error messages:

[2024-01-24 15:32:03,881 - INFO] Request
-------
Method: /google.ads.googleads.v14.services.CampaignService/MutateCampaigns
Host: googleads.googleapis.com
Headers: {
  "developer-token": "REDACTED",
  "login-customer-id": "7852354852",
  "x-goog-api-client": "gl-python/3.8.13 grpc/1.60.0 gax/2.15.0 gccl/23.0.0 pb/4.25.2",
  "x-goog-request-params": "customer_id=5692452448"
}
Request: customer_id: "5692452448"
operations {
  create {
    name: "Campaign-test-c920b589a770452b81097b38c8341799"
    status: PAUSED
    advertising_channel_type: DISPLAY
    targeting_setting {
      target_restrictions {
        targeting_dimension: TOPIC
        bid_only: true
      }
    }
    campaign_budget: "customers/5692452448/campaignBudgets/13318508559"
    manual_cpc {
      enhanced_cpc_enabled: true
    }
  }
}


Response
-------
Headers: {
  "google.ads.googleads.v14.errors.googleadsfailure-bin": "\n\u0002\n\u0003\u0003\u0005\u0012\u0001The supplied TargetingSetting contains an invalid CriterionTypeGroup. See CriterionTypeGroup documentation for CriterionTypeGroups allowed in Campaign or AdGroup TargetingSettings.\u001a\n*\bVERTICAL\"_\u0012\u000e\n\noperations\u0018\u0000\u0012\b\n\u0006create\u0012\u0013\n\u0011targeting_setting\u0012\u0017\n\u0013target_restrictions\u0018\u0000\u0012\u0015\n\u0013targeting_dimension\u0012\u0016GFK9JfBHlKKgx814MqWfOQ",
  "grpc-status-details-bin": "\b\u0003\u0012%Request contains an invalid argument.\u001a\u0003\nDtype.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure\u0012\u0002\n\u0002\n\u0003\u0003\u0005\u0012\u0001The supplied TargetingSetting contains an invalid CriterionTypeGroup. See CriterionTypeGroup documentation for CriterionTypeGroups allowed in Campaign or AdGroup TargetingSettings.\u001a\n*\bVERTICAL\"_\u0012\u000e\n\noperations\u0018\u0000\u0012\b\n\u0006create\u0012\u0013\n\u0011targeting_setting\u0012\u0017\n\u0013target_restrictions\u0018\u0000\u0012\u0015\n\u0013targeting_dimension\u0012\u0016GFK9JfBHlKKgx814MqWfOQ",
  "request-id": "GFK9JfBHlKKgx814MqWfOQ"
}
Fault: errors {
  error_code {
    setting_error: TARGETING_SETTING_CONTAINS_INVALID_CRITERION_TYPE_GROUP
  }
  message: "The supplied TargetingSetting contains an invalid CriterionTypeGroup. See CriterionTypeGroup documentation for CriterionTypeGroups allowed in Campaign or AdGroup TargetingSettings."
  trigger {
    string_value: "VERTICAL"
  }
  location {
    field_path_elements {
      field_name: "operations"
      index: 0
    }
    field_path_elements {
      field_name: "create"
    }
    field_path_elements {
      field_name: "targeting_setting"
    }
    field_path_elements {
      field_name: "target_restrictions"
      index: 0
    }
    field_path_elements {
      field_name: "targeting_dimension"
    }
  }
}
request_id: "GFK9JfBHlKKgx814MqWfOQ"

=====

my code:

def create_campaign(client, customer_id: str, campaign_budget: str, targeting_setting: str) -> dict:
"""given customer id, campagin_budget (resource_name), create a campaign and return campaign info as dict"""

campaign_service = client.get_service("CampaignService")

campaign_operation = client.get_type("CampaignOperation")
campaign = campaign_operation.create
# create a campaign with Google GDN
campaign.name = f"Campaign-test-{uuid4().hex}"
campaign.advertising_channel_type = client.enums.AdvertisingChannelTypeEnum.DISPLAY
campaign.status = client.enums.CampaignStatusEnum.PAUSED
campaign.manual_cpc.enhanced_cpc_enabled = True
campaign.campaign_budget = campaign_budget

# set targeting setting
targeting_setting = client.get_type("TargetingSetting")
restriction = client.get_type("TargetRestriction")
restriction.bid_only = True

restriction.targeting_dimension = client.enums.TargetingDimensionEnum.TOPIC
restriction.targeting_dimension.path = "Pets & Animals"
restriction.targeting_dimension.topic_constant = "/Pets & Animals/Animal Products & Services/Pet Food & Pet Care Supplies" # ref: https://developers.google.com/google-ads/api/data/verticals
targeting_setting.target_restrictions.append(restriction)

campaign.targeting_setting = targeting_setting
campaign_response = campaign_service.mutate_campaigns(customer_id=customer_id, operations=[campaign_operation])

return {
"customer_id": customer_id,
"campaign_id": campaign_response.results[0].resource_name.split("/")[-1],
"campaign_name": campaign.name,
"campaign_status": campaign.status,
"campaign_budget": campaign_budget,
"campagin_targeting_setting": targeting_setting,
}




Google Ads API Forum Advisor

unread,
Jan 24, 2024, 6:49:41 AM1/24/24
to shawn...@koodata.com.tw, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

By reviewing your concern, I understand that you have a query on how to set campaign targeting_setting with python google-ads library.

I would like to inform you that when you update the targeting settings, you must specify an exhaustive list of all targeting settings you want; there is no way to just specify a single new targeting setting. To do this, you must first fetch the existing list of targeting settings before you attempt your update, and make sure you include all the existing ones along with the new one you want to add. Kindly refer to the sample example TargetingSettings to get more information. 
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02rYNrx:ref"

Thanks,
 
Google Logo Google Ads API Team


Shawn Hsiao

unread,
Jan 24, 2024, 10:03:30 PM1/24/24
to Google Ads API and AdWords API Forum
Hi,

Thanks for the response.

I would like to clarify what I want to achieve is to create campaign and setup targeting_setting simultaneously. The official docs state that target_setting should be a valid column of campaign.
In that case, there should be no existing targeting settings, does it means that I could not setup targeting_setting upon creation?

Since the reference docs you provided I believe is more likely for updating existing target settings, could you provide code example for creation as a reference.
Thanks in advance.

Google Ads API Forum Advisor

unread,
Jan 25, 2024, 1:13:42 AM1/25/24
to shawn...@koodata.com.tw, adwor...@googlegroups.com
Hi,

Thank you for getting back to us. 

By reviewing your concern, I understand that you are encountering a "TARGETING_SETTING_CONTAINS_INVALID_CRITERION_TYPE_GROUP" error. Kindly note that the supplied TargetingSetting contains an invalid CriterionTypeGroup. Also, note that you can update your targetting_setting even after the campaign creation. I would recommend you kindly refer to the "Add Campaign Targeting Criteria" documentation as it describes how to create a campaign along with setting the target simultaneously. 
Reply all
Reply to author
Forward
0 new messages