2014-02-28 38 views
0

不幸的是,沒有這方面的例子(我一直在挖掘一段時間)。在Google Adwords中,您如何以編程方式更改預算?

我希望能夠以編程方式修改廣告系列的預算。我可以看到BudgetService存在,但我不知道如何(或去哪裏瞭解如何)獲取預算ID或預算名稱。

我推測必須向廣告系列查詢預算,從budgetId派生出來,然後在BudgetService請求中使用它。是這樣嗎?如果沒有,最後的結果是什麼?

回答

4

我不知道您正在使用哪種語言/客戶端庫,但假設您已經處理了配置和授權,那麼以下內容適用於Ruby。我可以想象它在任何其他客戶端庫中都會非常相似。

首先,建立一個API連接。

adwords = AdwordsApi::Api.new 

使用CampaignService獲取預算的ID。您還可以瞭解預算的當前金額和期限(每日,每月等)。

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) 

從響應中提取預算的ID,並使用BudgetService改變量。

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]) 
0

一個PHP函數用於更新預算,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"; 
    } 
} 
相關問題