Google Ads API Creating Campaign Bid Modifiers Using CampaignCriterionService in Python

113 views
Skip to first unread message

Natalie Chun

unread,
Jun 6, 2019, 6:16:13 AM6/6/19
to AdWords API and Google Ads API Forum
Several questions
1)  Is there something wrong with the below approach in using CampaignCriterionService to create Campaign level bid modifiers (at device, location, other level) and what is the recommended best approach to generate these via the Google Ads API? 
2) Is there something wrong with the documentation on (CampaignCriterionStatusEnum and CampaignCriterion resource) as the following commands do not work (i.e. CampaignCriterionStatusEnum does not seem to exist and the CampaignCriterion resource does not have a status field as cited in the Google Ads API documentation:
criterion_status_enum = client.get_type('CampaignCriterionStatusEnum', version='v1')
op_c.status = getattr(criterion_status_enum, 'PAUSED')

Any advice/feedback on these issues would be greatly appreciated.  Thanks!
 
Logging Output
Response
-------
Headers: {
  "google.ads.googleads.v1.errors.googleadsfailure-bin": "\n^\n\u0002\b\u0005\u0012FMutate operations must have 'create', 'update', or 'remove' specified.\"\u0010\u0012\u000e\n\noperations\u0012\u0000",
  "grpc-status-details-bin": "\b\u0003\u0012%Request contains an invalid argument.\u001a\u0001\nCtype.googleapis.com/google.ads.googleads.v1.errors.GoogleAdsFailure\u0012`\n^\n\u0002\b\u0005\u0012FMutate operations must have 'create', 'update', or 'remove' specified.\"\u0010\u0012\u000e\n\noperations\u0012\u0000",
  "request-id": "WlST6n-W6Xb6ns6DiJR2Iw"
}
Fault: {
  "errors": [
    {
      "errorCode": {
        "requestError": "OPERATION_REQUIRED"
      },
      "message": "Mutate operations must have 'create', 'update', or 'remove' specified.",
      "location": {
        "fieldPathElements": [
          {
            "fieldName": "operations",
            "index": "0"
          }
        ]
      }
    }
  ]
}


Code for Generating Behavior

def campaign_criterion_service(customer_id, campaign_id, params):
    """Function to add/create modifiers (location, device etc.) for a given campaign."""
    
    logging.basicConfig(level=logging.INFO, format='[%(asctime)s - %(levelname)s] %(message).5000s')
    logging.getLogger('./google.ads.google_ads.client').setLevel(logging.INFO)
    client = google.ads.google_ads.client.GoogleAdsClient.load_from_storage()
    op_c = client.get_type('CampaignCriterionOperation', version='v1').create
    op_c.campaign.value = client.get_service('CampaignService', version='v1').campaign_path(customer_id, campaign_id)

    criterion_type_enum = client.get_type('CriterionTypeEnum', version='v1')
    # NOTE:  Two following lines error out where enum does not exist and status is not part of CampaignCriterion
    #criterion_status_enum = client.get_type('CampaignCriterionStatusEnum' , version='v1')
    #op_c.status = getattr(criterion_status_enum, 'PAUSED')
    op_c.type = getattr(criterion_type_enum, params['criterion_type'].upper())
    if params['criterion_type'] == 'device':
        device_enum = client.get_type('DeviceEnum', version='v1')
        op_c.criterion_id.value = getattr(device_enum, params['device_type'].upper())
        op_c.criterion.negative = False

    try:
        response = client.get_service('CampaignCriterionService', version='v1').mutate_campaign_criteria(customer_id, [client.get_type('CampaignCriterionOperation', version='v1')])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
            error_statement(ex)

    print('Created %s bid modifier: %s.'
              % (name, response.results[0].resource_name))

Google Ads API Forum Advisor Prod

unread,
Jun 6, 2019, 5:29:15 PM6/6/19
to adwor...@googlegroups.com
Hello Natalie, 

Could you please take a look at the AddCampaignBidModifier code sample in Python which illustrates in detail on how to set the campaign level bid modifier? Please give it a try and let us know if you have any issues.

Thanks,
Bharani, Google Ads API Team

ref:_00D1U1174p._5001UBloxU:ref
Reply all
Reply to author
Forward
0 new messages