【问题标题】:Google Adwords API: Retrieve count of keywords in accountGoogle Adwords API:检索帐户中的关键字计数
【发布时间】:2016-10-06 13:22:46
【问题描述】:

有一个获取广告组中关键字的示例,但我想查找帐户中所有关键字的计数。

如何在 java 中做到这一点?

【问题讨论】:

    标签: google-ads-api


    【解决方案1】:

    以下是我如何修改 adwords api 代码以使其能够检索帐户中的关键字计数(在 ads.properties 文件中指定为 clientCustomerId)。

    package adwords.axis.v201609.basicoperations;
    
    import com.google.api.ads.adwords.axis.factory.AdWordsServices;
    import com.google.api.ads.adwords.axis.utils.v201609.SelectorBuilder;
    import com.google.api.ads.adwords.axis.v201609.cm.AdGroupCriterionPage;
    import com.google.api.ads.adwords.axis.v201609.cm.AdGroupCriterionServiceInterface;
    import com.google.api.ads.adwords.axis.v201609.cm.Paging;
    import com.google.api.ads.adwords.axis.v201609.cm.Selector;
    import com.google.api.ads.adwords.lib.client.AdWordsSession;
    import com.google.api.ads.adwords.lib.selectorfields.v201609.cm.AdGroupCriterionField;
    import com.google.api.ads.common.lib.auth.OfflineCredentials;
    import com.google.api.client.auth.oauth2.Credential;
    
    import java.text.NumberFormat;
    
    /**
     * This example gets the count of all ad group criteria in an account.
     *
     * @modified by Biniam Asnake.
     */
    public class GetCountOfKeywordsInAccount {
    
        public static void main(String[] args) throws Exception {
    
            // execution duration counter
            long started = System.currentTimeMillis();
    
            // Generate a refreshable OAuth2 credential.
            Credential oAuth2Credential = new OfflineCredentials.Builder()
                    .forApi(OfflineCredentials.Api.ADWORDS)
                    .fromFile()
                    .build()
                    .generateCredential();
    
            // Construct an AdWordsSession.
            AdWordsSession session = new AdWordsSession.Builder()
                    .fromFile()
                    .withOAuth2Credential(oAuth2Credential)
                    .build();
    
            AdWordsServices adWordsServices = new AdWordsServices();
    
            runExample(adWordsServices, session);
    
            System.out.println("Execution took: " + ((System.currentTimeMillis() - started) / 1000) + " seconds.");
        }
    
        public static void runExample(AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
    
            // Get the AdGroupCriterionService.
            AdGroupCriterionServiceInterface adGroupCriterionService =
                    adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    
            // Create selector.
            SelectorBuilder builder = new SelectorBuilder();
            Selector selector = builder
                    .fields(AdGroupCriterionField.Id)
                    .in(AdGroupCriterionField.CriteriaType, "KEYWORD")
                    .in(AdGroupCriterionField.Status, "ENABLED")
                    .build();
    
            // Set selector paging = the most important change is to set numberResults to 0.
            Paging paging = new Paging();
            paging.setNumberResults(0);
            selector.setPaging(paging);
    
            // Get all ad group criteria.
            AdGroupCriterionPage page = adGroupCriterionService.get(selector);
    
            System.out.println("Count of Keywords = " + NumberFormat.getInstance().format(page.getTotalNumEntries()));
        }
    }
    

    确保您的类路径中有ads.properties 文件。

    # Credentials for accessing Google AdWords API
    api.adwords.refreshToken=SOME-THING
    api.adwords.clientId=SOME-THING
    api.adwords.clientSecret=SOME-THING
    api.adwords.userAgent=SOME-THING
    api.adwords.developerToken=SOME-THING
    api.adwords.isPartialFailure=true
    api.adwords.clientCustomerId=123456789
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2014-02-01
      相关资源
      最近更新 更多