API Google Adwords: получение количества ключевых слов в аккаунте

Есть пример получения ключевых слов в группе объявлений, но я хочу найти количество всех ключевых слов в учетной записи.

Как я могу сделать это в java?


person biniam    schedule 06.10.2016    source источник


Ответы (1)


Вот как я изменил код API AdWords, чтобы он мог получать количество ключевых слов в учетной записи (указанной как clientCustomerId в файле ads.properties).

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
person biniam    schedule 06.10.2016