Search in sources :

Example 6 with CampaignPage

use of com.google.api.ads.adwords.axis.v201809.cm.CampaignPage in project googleads-java-lib by googleads.

the class GetCampaignsByLabel method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param labelId the ID of the label.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long labelId) throws RemoteException {
    // Get the CampaignService.
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    int offset = 0;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(CampaignField.Id, CampaignField.Name, CampaignField.Labels).containsAny(CampaignField.Labels, labelId.toString()).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
    CampaignPage page = null;
    do {
        // Get all campaigns.
        page = campaignService.get(selector);
        // Display campaigns.
        if (page.getEntries() != null) {
            for (Campaign campaign : page.getEntries()) {
                String labels = Joiner.on(", ").join(Lists.transform(Lists.newArrayList(campaign.getLabels()), label -> String.format("%d/%s", label.getId(), label.getName())));
                System.out.printf("Campaign found with name '%s' and ID %d and labels: %s.%n", campaign.getName(), campaign.getId(), labels);
            }
        } else {
            System.out.println("No campaigns were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());
}
Also used : OAuthException(com.google.api.ads.common.lib.exception.OAuthException) Parameter(com.beust.jcommander.Parameter) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) Lists(com.google.common.collect.Lists) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) CampaignField(com.google.api.ads.adwords.lib.selectorfields.v201809.cm.CampaignField) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) Joiner(com.google.common.base.Joiner) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Aggregations

CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)6 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)6 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)5 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)5 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)4 Parameter (com.beust.jcommander.Parameter)1 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 ServiceQuery (com.google.api.ads.adwords.axis.utils.v201809.ServiceQuery)1 AdGroupChangeData (com.google.api.ads.adwords.axis.v201809.ch.AdGroupChangeData)1 CampaignChangeData (com.google.api.ads.adwords.axis.v201809.ch.CampaignChangeData)1 CustomerChangeData (com.google.api.ads.adwords.axis.v201809.ch.CustomerChangeData)1 CustomerSyncSelector (com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector)1 CustomerSyncServiceInterface (com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncServiceInterface)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)1 DateTimeRange (com.google.api.ads.adwords.axis.v201809.cm.DateTimeRange)1 DynamicSearchAdsSetting (com.google.api.ads.adwords.axis.v201809.cm.DynamicSearchAdsSetting)1 PageFeed (com.google.api.ads.adwords.axis.v201809.cm.PageFeed)1 Paging (com.google.api.ads.adwords.axis.v201809.cm.Paging)1