1
votes

Sadly, there are no examples of this (and I've been digging for some time.)

I would like to be able to programmatically modify a campaign's budget. I can see that BudgetService exists, but I don't know how (or where to go to find out how) to get the id of a budget or the name of a budget.

I surmise that one has to query the campaign for its budget, derive from that the budgetId and then use that in a BudgetService request. Is that the case? If not, what's the go?

3

3 Answers

5
votes

I don't know what language / client library you are using, but the following should work for Ruby, assuming that you already have your configuration and authorization handled. I'd imagine it would be very similar in any of the other client libraries.

First, establish an API connection.

adwords = AdwordsApi::Api.new

Use CampaignService to get the budget's id. You can also learn the budget's current amount and period (DAILY, MONTHLY, etc).

campaign_srv = adwords.service(:CampaignService, API_VERSION)

selector = {
    fields: ['Id', 'Name', 'BudgetId', 'BudgetName', 'Amount', 'Period'], 
    predicates: [
        {field: 'Id', operator: 'EQUALS', values: [CAMPAIGN_ID]}
    ]
}

campaign_response = campaign_srv.get(selector)

Extract the budget's id from the response, and use BudgetService to alter the amount.

budget_id = response[:entries][0][:budget][:budget_id]
budget_srv = adwords.service(:BudgetService, API_VERSION)

operation = {
    operator: 'SET',
    operand: {
        id: budget_id,
        amount: DESIRED_AMOUNT_IN_LOCAL_CURRENCY_FOR_ACCOUNT
    }
}
budget_response = budget_srv.mutate([operation])
3
votes

Most of Google's documentation is about Shared Budgets and not about setting an individual campaign budget. To set an individual campaign budget, you still need to use the BudgetService and create a BudgetId, but set isExplicitlyShared to false. Setting the shared setting to false will not create a shared budget, but rather create a budget object which will set an individual campaign budget. Python example below:

from googleads import adwords

client = adwords.AdWordsClient.LoadFromStorage('<PATH_TO_YOUR_SERVICE_ACCOUNT>')
client.SetClientCustomerId('<YOUR_ADWORDS_ACCOUNT_ID>')

campaign_id = '<YOUR_CAMPAIGN_ID>'


def main():
    # Initialize both campaign and budget services
    campaign_service = client.GetService('CampaignService', version='v201809')
    budget_service = client.GetService('BudgetService', version='v201809')

    # Create a budget ID with no name, specify that it is not a shared budget
    # Establish budget as a micro amount
    budget = {
        'name': None,
        'isExplicitlyShared': 'false',
        'amount': {
            'microAmount': 80000000
            }
        }

    budget_operations = [{
        'operator': 'ADD',
        'operand': budget
        }]

    budget_id = budget_service.mutate(budget_operations)['value'][0]['budgetId']


    # Construct operations and update campaign.
    operations = [{
        'operator': 'SET',
        'operand': {
        'id': campaign_id,
        'budget': {
            'budgetId': budget_id
            }
        }
    }]

    campaigns = campaign_service.mutate(operations)
0
votes

A php function to update the budget, GoogleApiAdsAdWords, v201609

function GetCampaignAndUpdateBudgetExample(AdWordsUser $user, $campaignId)
{
    // Get the service, which loads the required classes.
    $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);

    // Create selector.
    $selector = new Selector();
    $selector->fields = array('Id', 'Name', 'BudgetId', 'BudgetName', 'Amount');
    $selector->predicates[] = new Predicate('CampaignId', 'EQUALS', array($campaignId));
    $selector->ordering[] = new OrderBy('Name', 'ASCENDING');

    // Create paging controls.
    $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);

    // Make the get request.
    $page = $campaignService->get($selector);

    // Display results.
    if (isset($page->entries[0]))
    {
        $campaign = $page->entries[0];
        printf("Campaign with name '%s' and ID '%s' and budget ID '%s' and budget Name '%s' was found.\n", $campaign->name, $campaign->id, $campaign->budget->budgetId, $campaign->budget->name);


        // Get the BudgetService, which loads the required classes.
        $budget_service = $user->GetService('BudgetService', ADWORDS_VERSION);
        // Create the shared budget (required).
        $budget = new Budget();
        $budget->budgetId = $campaign->budget->budgetId;
        $budget->amount = new Money(20000000);
        $budget->deliveryMethod = 'STANDARD';
        // Create operation.
        $budet_operation = new BudgetOperation();
        $budet_operation->operand = $budget;
        $budet_operation->operator = 'SET';
        // Make the mutate request.
        $budget_service_result = $budget_service->mutate([$budet_operation]);
        $budget_result = $budget_service_result->value[0];

        printf("Budget with name '%s' and ID '%s' updated.\n", $budget_result->name, $budget_result->budgetId);
    }
    else
    {
        print "No campaigns were found.\n";
    }
}