Trying to update the budget programmatically using v13 Gads API

74 views
Skip to first unread message

Mrinal Pant

unread,
Feb 2, 2024, 2:48:46 AM2/2/24
to Google Ads API and AdWords API Forum
Can anyone out there can give me a good reference cuz, i cant find proper resources for the same.

1. I tried to retrieve the campaign id first to test things out.
from google.ads.googleads.client import GoogleAdsClient

from dotenv import dotenv_values


def get_campaign_id(campaign_name):
config = dotenv_values(".env")

credentials = {
"developer_token": config["developer_token"],
"refresh_token": config["refresh_token"],
"client_id": config["client_id"],
"client_secret": config["client_secret"],
"use_proto_plus"True
}

googleads_client = GoogleAdsClient.load_from_dict(credentials)

customer_id = config.get("account_id")
print(customer_id)

if not customer_id:
print("Google Ads customer ID not provided.")
return None
ga_service = googleads_client.get_service("GoogleAdsService")

query = f"""
SELECT
campaign.id
FROM
campaign
WHERE
campaign.name = '{campaign_name}'
"""

response = ga_service.search(customer_id=customer_id, query=query)

for row in response:
campaign_id = row.campaign.id
print(f"Campaign ID for '{campaign_name}': {campaign_id}")
return campaign_id

# Example usage
campaign_name_to_find = 'wowdare_united-kingdom_eng'
get_campaign_id(campaign_name_to_find)


I got the output in the console, so it worked well.


2. When i tried to update the budget using another function then it shows the error where i was trying to get the existing budget using  CampaignBudgetOperation. 

Is there any other way to update the existing budget for a specific campaign.

AttributeError: Unknown field for CampaignBudgetOperation: get

def campaign_budget_update(campaign_id, budget):
config = dotenv_values(".env")

credentials = {
"developer_token": config["developer_token"],
"refresh_token": config["refresh_token"],
"client_id": config["client_id"],
"client_secret": config["client_secret"],
"use_proto_plus": True
}

googleads_client = GoogleAdsClient.load_from_dict(credentials)

customer_id = config.get("account_id")

if not customer_id:
print("Google Ads customer ID not provided.")
return None

campaign_budget_service = googleads_client.get_type("CampaignBudgetOperation")

# Construct the campaign budget resource name
campaign_budget_resource_name = campaign_id
#print(campaign_budget_resource_name)

# Get the existing campaign budget to modify
existing_campaign_budget = campaign_budget_service.get(campaign_budget_resource_name)

# Update the budget amount
existing_campaign_budget.amount_micros = budget * 1_000_000 # Convert budget to micros

# Create a mutate operation to update the campaign budget
operation = googleads_client.types.CampaignBudgetOperation(update=existing_campaign_budget)

# Execute the mutate operation
response = googleads_client.service.campaign_budget.mutate_campaign_budgets(customer_id=customer_id,
operations=[operation])

# Print the response
print(f"Budget updated for Campaign ID {campaign_id}. Response: {response}")

Google Ads API Forum Advisor

unread,
Feb 2, 2024, 8:13:06 AM2/2/24
to pantmr...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for contacting the Google Ads API support team.

By reviewing your concern, I understand that you are trying to update the existing budget for a campaign. As per the API documentation, to replace the budget of an existing campaign, in a CampaignOperation.update, set the campaign_budget field of the Campaign object to the resource name of an existing budget (along with any other campaign fields that you might want to set). Note that this will replace the existing budget assigned to the campaign with the one specified by the campaign_budget field, since a campaign can be associated with only one budget at a time.

Kindly try with the above guidelines, and if the issue still persists, provide us with the complete API logs (request and response with request-id and request header) generated at your end to assist you further.

If you are using a client library and haven't enabled the logging yet, I would request you to enable logging for the specific client library that you are using. You can refer to the guides Java, .Net, PHP, Python, Ruby or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.

 
This message is in relation to case "ref:!00D1U01174p.!5004Q02rylw7:ref"

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages