How can i change existing campaign budget?

591 views
Skip to first unread message

ma...@sumer.me

unread,
Apr 26, 2022, 7:31:10 PM4/26/22
to Google Ads API and AdWords API Forum
Hi,

I am using Ads API v10 with PHP. And i can't update campaign budget with update sample.

Do I always have to create a new budget and add it to that campaign to update the budget? Like this:

$budget = new CampaignBudget([
            'name' => $budgetId.time(),
            'amount_micros' => $amount
        ]);

        $campaignBudgetOperation = new CampaignBudgetOperation();
        $campaignBudgetOperation->setCreate($budget);

        $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
        $response = $campaignBudgetServiceClient->mutateCampaignBudgets(
            $customerId,
            [$campaignBudgetOperation]
        );
       
        $addedBudget = $response->getResults()[0];
       
        $budgetResourceName = $addedBudget->getResourceName();
       
        $campaign = new Campaign([
            'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
            'campaign_budget' => $budgetResourceName
        ]);
       
        $campaignOperation = new CampaignOperation();
        $campaignOperation->setUpdate($campaign);
        $campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
       
        $campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
        $response = $campaignServiceClient->mutateCampaigns(
            $customerId,
            [$campaignOperation]
        );

        $updatedCampaign = $response->getResults()[0];

Is there no way to update the existing budget with the budget id?

Google Ads API Forum Advisor

unread,
Apr 27, 2022, 10:43:33 PM4/27/22
to ma...@sumer.me, adwor...@googlegroups.com
Hi Mahir,

Thank you for raising this concern to the Google Ads API team.

Moving forward to your concern, it seems that you want to update the existing budget with the budget id. That said, you can use this CampaignBudgetService which allows you to manage your campaign budgets with available operations [create,update,remove]. Unfortunately, the said service is utilizing the resource name of the campaign budget instead of the campaign budget ID.

Regards,
Google Logo
Carmela
Google Ads API Team
 


ref:_00D1U1174p._5004Q2aNwfX:ref

jindong o

unread,
May 18, 2022, 3:01:45 PM5/18/22
to Google Ads API and AdWords API Forum

this is my working code sample.
you need  budgetResourceName or budgetId of existing budget to update campaign budget.
($budget_resource_name=ResourceNames::forCampaignBudget($cam_infos['customer_id'], $budgetId);)
.......

$budgetResourceName = self::updateCampaignBudget($googleAdsClient, $cam_infos['customer_id'], $cam_infos['campaign_budget'],$cam_infos['budget_resource_name']);
........

private static function updateCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId,int $campaign_budget,string $budget_resource_name){
        // Creates a campaign budget.
        $budget = new CampaignBudget([
            'resource_name' => $budget_resource_name,
            //'delivery_method' => BudgetDeliveryMethod::STANDARD,
            'amount_micros' => $campaign_budget
            //'explicitly_shared' => false
        ]);

        // Creates a campaign budget operation.
        $campaignBudgetOperation = new CampaignBudgetOperation();
        $campaignBudgetOperation->setUpdate($budget);
        $campaignBudgetOperation->setUpdateMask(FieldMasks::allSetFieldsOf($budget));
        // Issues a mutate request.

        $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
        $response = $campaignBudgetServiceClient->mutateCampaignBudgets(
            $customerId,
            [$campaignBudgetOperation]
        );

        /** @var CampaignBudget $addedBudget */
        $updatedBudget = $response->getResults()[0];
        //printf("Added budget named '%s'%s", $addedBudget->getResourceName(), PHP_EOL);

        return $updatedBudget->getResourceName();
Reply all
Reply to author
Forward
0 new messages