Trying to update a campaign budget with python

428 views
Skip to first unread message

Andrew Kloos

unread,
Jun 17, 2022, 3:47:39 PM6/17/22
to Google Ads API and AdWords API Forum
So we're trying to update a campaign budget with python and we're getting a response which looks like it's succeeding but the budget doesn't change in google ads.  Here's our code....

           query = (f'''
                        SELECT
                          campaign_budget.id,
                          campaign_budget.amount_micros,
                          campaign_budget.delivery_method,
                          campaign_budget.period,
                          campaign_budget.name,
                          campaign_budget.status,
                          campaign.id,
                          campaign.status,
                          campaign_budget.resource_name,
                          campaign_budget.amount_micros
                        FROM campaign_budget
                        WHERE campaign.id = "proper id hard coded"
            ''')

            #fix  {index[0][3]}
            request = client.get_type("SearchGoogleAdsRequest")
            request.customer_id = google_id
            request.query = query
            request.page_size = page_size
            response = ga_service.search(request)

            # Retrieve Google Customer Service Info
            for row in response:
                campaign = row.campaign
                campaign_budget_resource_name = row.campaign_budget
                print('\n')
                print(campaign_budget_resource_name.amount_micros)
                print('\n')

            operations = []

            campaign_budget_operation = client.get_type("CampaignBudgetOperation")
            campaign_budget_service = client.get_service("CampaignBudgetService")
            campaign_service = client.get_service('CampaignService')

            campaign_budget = campaign_budget_operation.update
            campaign_budget.resource_name = campaign_budget_resource_name.resource_name
            campaign_budget.status = client.enums.CampaignStatusEnum.ENABLED # campaign.status

            campaign_budget.amount_micros = 8000000 #index_row[5]

            client.copy_from(campaign_budget_operation.update_mask, protobuf_helpers.field_mask(None, campaign._pb))
            operations.append(campaign_budget_operation)

            campaign_response = campaign_budget_service.mutate_campaign_budgets(
                    customer_id=google_id, operations=operations
                )


            print(operations)
            print(campaign_response)

here is our operations and response...

[update { resource_name: "customers/num/campaignBudgets/resource name" status: ENABLED amount_micros: 8000000 } update_mask { paths: "resource_name" paths: "id" paths: "status" } ] results { resource_name: "customers/num/campaignBudgets/resource name" }

Google Ads API Forum Advisor

unread,
Jun 20, 2022, 8:28:16 AM6/20/22
to akl...@waterbearmarketing.com, adwor...@googlegroups.com
Hello Andrew,

If you look at the log for your response, you will see that in the update_mask field the amount_micros is missing, which is the reason why the amount is not being changed.

I am not a Python expert, but I think the reason for this issue is in the line

client.copy_from(campaign_budget_operation.update_mask, protobuf_helpers.field_mask(None, campaign._pb))

Which is passing a campaign object to protobuf_helpers.field_mask.

Instead, I think it should be, in your code,

client.copy_from(campaign_budget_operation.update_mask, protobuf_helpers.field_mask(None, campaign_budget_resource_name._pb))

This should create the right field mask for the API request to indicate that the amount_micros field is being mutated, which in turn should result in the value being actually updated by your API request.

Hope this helps,

Mattia Tommasone
Google Ads API Team

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