Search in sources :

Example 61 with Campaign

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

the class AddTrial method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param draftId the ID of the draft.
 * @param baseCampaignId the ID of the base campaign.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws InterruptedException
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long draftId, long baseCampaignId) throws RemoteException, InterruptedException {
    // Get the TrialService.
    TrialServiceInterface trialService = adWordsServices.get(session, TrialServiceInterface.class);
    Trial trial = new Trial();
    trial.setDraftId(draftId);
    trial.setBaseCampaignId(baseCampaignId);
    trial.setName("Test Trial #" + System.currentTimeMillis());
    trial.setTrafficSplitPercent(50);
    trial.setTrafficSplitType(CampaignTrialTrafficSplitType.RANDOM_QUERY);
    TrialOperation trialOperation = new TrialOperation();
    trialOperation.setOperator(Operator.ADD);
    trialOperation.setOperand(trial);
    long trialId = trialService.mutate(new TrialOperation[] { trialOperation }).getValue(0).getId();
    // Since creating a trial is asynchronous, we have to poll it to wait for it to finish.
    Selector trialSelector = new SelectorBuilder().fields(TrialField.Id, TrialField.Status, TrialField.BaseCampaignId, TrialField.TrialCampaignId).equalsId(trialId).build();
    trial = null;
    boolean isPending = true;
    int pollAttempts = 0;
    do {
        long sleepSeconds = (long) Math.scalb(30d, pollAttempts);
        System.out.printf("Sleeping for %d seconds.%n", sleepSeconds);
        Thread.sleep(sleepSeconds * 1000);
        trial = trialService.get(trialSelector).getEntries(0);
        System.out.printf("Trial ID %d has status '%s'.%n", trial.getId(), trial.getStatus());
        pollAttempts++;
        isPending = TrialStatus.CREATING.equals(trial.getStatus());
    } while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
    if (TrialStatus.ACTIVE.equals(trial.getStatus())) {
        // The trial creation was successful.
        System.out.printf("Trial created with ID %d and trial campaign ID %d.%n", trial.getId(), trial.getTrialCampaignId());
    } else if (TrialStatus.CREATION_FAILED.equals(trial.getStatus())) {
        // The trial creation failed, and errors can be fetched from the TrialAsyncErrorService.
        Selector errorsSelector = new SelectorBuilder().fields(TrialAsyncErrorField.TrialId, TrialAsyncErrorField.AsyncError).equals(TrialAsyncErrorField.TrialId, trial.getId().toString()).build();
        TrialAsyncErrorServiceInterface trialAsyncErrorService = adWordsServices.get(session, TrialAsyncErrorServiceInterface.class);
        TrialAsyncErrorPage trialAsyncErrorPage = trialAsyncErrorService.get(errorsSelector);
        if (trialAsyncErrorPage.getEntries() == null || trialAsyncErrorPage.getEntries().length == 0) {
            System.out.printf("Could not retrieve errors for trial ID %d for draft ID %d.%n", trial.getId(), draftId);
        } else {
            System.out.printf("Could not create trial ID %d for draft ID %d due to the following errors:%n", trial.getId(), draftId);
            int i = 0;
            for (TrialAsyncError error : trialAsyncErrorPage.getEntries()) {
                ApiError asyncError = error.getAsyncError();
                System.out.printf("Error #%d: errorType='%s', errorString='%s', trigger='%s', fieldPath='%s'%n", i++, asyncError.getApiErrorType(), asyncError.getErrorString(), asyncError.getTrigger(), asyncError.getFieldPath());
            }
        }
    } else {
        // Most likely, the trial is still being created. You can continue polling,
        // but we have limited the number of attempts in the example.
        System.out.printf("Timed out waiting to create trial from draft ID %d with base campaign ID %d.%n", draftId, baseCampaignId);
    }
}
Also used : TrialServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.TrialServiceInterface) Trial(com.google.api.ads.adwords.axis.v201809.cm.Trial) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) TrialAsyncErrorPage(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncErrorPage) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) TrialAsyncErrorServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncErrorServiceInterface) TrialOperation(com.google.api.ads.adwords.axis.v201809.cm.TrialOperation) TrialAsyncError(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncError) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 62 with Campaign

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

the class GetCampaigns method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @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) 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).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
    CampaignPage page;
    do {
        // Get all campaigns.
        page = campaignService.get(selector);
        // Display campaigns.
        if (page.getEntries() != null) {
            for (Campaign campaign : page.getEntries()) {
                System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId());
            }
        } else {
            System.out.println("No campaigns were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());
}
Also used : 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)

Example 63 with Campaign

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

the class SetBidModifier method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign.
 * @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 campaignId) throws RemoteException {
    // Get the CampaignCriterionService.
    CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
    // Create mobile platform. The ID can be found in the documentation.
    // https://developers.google.com/adwords/api/docs/appendix/platforms
    Platform mobile = new Platform();
    mobile.setId(30001L);
    // Create criterion with modified bid.
    CampaignCriterion campaignCriterion = new CampaignCriterion();
    campaignCriterion.setCampaignId(campaignId);
    campaignCriterion.setCriterion(mobile);
    campaignCriterion.setBidModifier(BID_MODIFIER);
    // Create SET operation.
    CampaignCriterionOperation operation = new CampaignCriterionOperation();
    operation.setOperand(campaignCriterion);
    operation.setOperator(Operator.SET);
    // Update campaign criterion.
    CampaignCriterionReturnValue result = campaignCriterionService.mutate(new CampaignCriterionOperation[] { operation });
    for (CampaignCriterion campaignCriterionResult : result.getValue()) {
        System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was modified with bid %.4f.%n", campaignCriterionResult.getCampaignId(), campaignCriterionResult.getCriterion().getId(), campaignCriterionResult.getCriterion().getType(), campaignCriterionResult.getBidModifier());
    }
}
Also used : CampaignCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue) CampaignCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation) Platform(com.google.api.ads.adwords.axis.v201809.cm.Platform) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) CampaignCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)

Example 64 with Campaign

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

the class AddSiteLinks method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign where sitelinks will be added.
 * @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 campaignId) throws ApiException, RemoteException {
    // Get the CustomerService.
    CustomerServiceInterface customerService = adWordsServices.get(session, CustomerServiceInterface.class);
    // Find the matching customer and its time zone. The getCustomers method will return
    // a single Customer object corresponding to the session's clientCustomerId.
    Customer customer = customerService.getCustomers()[0];
    DateTimeZone customerTimeZone = DateTimeZone.forID(customer.getDateTimeZone());
    System.out.printf("Found customer ID %d with time zone '%s'.%n", customer.getCustomerId(), customer.getDateTimeZone());
    // Get the CampaignExtensionSettingService.
    CampaignExtensionSettingServiceInterface campaignExtensionSettingService = adWordsServices.get(session, CampaignExtensionSettingServiceInterface.class);
    // Create the sitelinks.
    SitelinkFeedItem sitelink1 = createSiteLinkFeedItem("Store Hours", "http://www.example.com/storehours");
    // Show the Thanksgiving specials link only from 20 - 27 Nov.
    SitelinkFeedItem sitelink2 = createSiteLinkFeedItem("Thanksgiving Specials", "http://www.example.com/thanksgiving");
    // The time zone of the start and end date/times must match the time zone of the customer.
    DateTime startTime = new DateTime(DateTime.now().getYear(), 11, 20, 0, 0, 0, customerTimeZone);
    if (startTime.isBeforeNow()) {
        // Move the startTime to next year if the current date is past November 20th.
        startTime = startTime.plusYears(1);
    }
    sitelink2.setStartTime(startTime.toString("yyyyMMdd HHmmss ZZZ"));
    // Use the same year as startTime when creating endTime.
    DateTime endTime = new DateTime(startTime.getYear(), 11, 27, 23, 59, 59, customerTimeZone);
    sitelink2.setEndTime(endTime.toString("yyyyMMdd HHmmss ZZZ"));
    // Target this sitelink for United States only. See
    // https://developers.google.com/adwords/api/docs/appendix/geotargeting
    // for valid geolocation codes.
    Location unitedStates = new Location();
    unitedStates.setId(2840L);
    sitelink2.setGeoTargeting(unitedStates);
    // Restrict targeting only to people physically within the United States.
    // Otherwise, this could also show to people interested in the United States
    // but not physically located there.
    FeedItemGeoRestriction geoTargetingRestriction = new FeedItemGeoRestriction();
    geoTargetingRestriction.setGeoRestriction(GeoRestriction.LOCATION_OF_PRESENCE);
    sitelink2.setGeoTargetingRestriction(geoTargetingRestriction);
    // Show the wifi details primarily for high end mobile users.
    SitelinkFeedItem sitelink3 = createSiteLinkFeedItem("Wifi available", "http://www.example.com/mobile/wifi");
    // See https://developers.google.com/adwords/api/docs/appendix/platforms for device criteria
    // IDs.
    FeedItemDevicePreference devicePreference = new FeedItemDevicePreference(30001L);
    sitelink3.setDevicePreference(devicePreference);
    // Target this sitelink for the keyword "free wifi".
    Keyword wifiKeyword = new Keyword();
    wifiKeyword.setText("free wifi");
    wifiKeyword.setMatchType(KeywordMatchType.BROAD);
    sitelink3.setKeywordTargeting(wifiKeyword);
    // Show the happy hours link only during Mon - Fri 6PM to 9PM.
    SitelinkFeedItem sitelink4 = createSiteLinkFeedItem("Happy hours", "http://www.example.com/happyhours");
    sitelink4.setScheduling(new FeedItemScheduling(new FeedItemSchedule[] { new FeedItemSchedule(DayOfWeek.MONDAY, 18, MinuteOfHour.ZERO, 21, MinuteOfHour.ZERO), new FeedItemSchedule(DayOfWeek.TUESDAY, 18, MinuteOfHour.ZERO, 21, MinuteOfHour.ZERO), new FeedItemSchedule(DayOfWeek.WEDNESDAY, 18, MinuteOfHour.ZERO, 21, MinuteOfHour.ZERO), new FeedItemSchedule(DayOfWeek.THURSDAY, 18, MinuteOfHour.ZERO, 21, MinuteOfHour.ZERO), new FeedItemSchedule(DayOfWeek.FRIDAY, 18, MinuteOfHour.ZERO, 21, MinuteOfHour.ZERO) }));
    // Create your campaign extension settings. This associates the sitelinks
    // to your campaign.
    CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting();
    campaignExtensionSetting.setCampaignId(campaignId);
    campaignExtensionSetting.setExtensionType(FeedType.SITELINK);
    ExtensionSetting extensionSetting = new ExtensionSetting();
    extensionSetting.setExtensions(new ExtensionFeedItem[] { sitelink1, sitelink2, sitelink3, sitelink4 });
    campaignExtensionSetting.setExtensionSetting(extensionSetting);
    CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation();
    operation.setOperand(campaignExtensionSetting);
    operation.setOperator(Operator.ADD);
    // Add the extensions.
    CampaignExtensionSettingReturnValue returnValue = campaignExtensionSettingService.mutate(new CampaignExtensionSettingOperation[] { operation });
    if (returnValue.getValue() != null && returnValue.getValue().length > 0) {
        CampaignExtensionSetting newExtensionSetting = returnValue.getValue(0);
        System.out.printf("Extension setting with type '%s' was added to campaign ID %d.%n", newExtensionSetting.getExtensionType().getValue(), newExtensionSetting.getCampaignId());
    } else {
        System.out.println("No extension settings were created.");
    }
}
Also used : ExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting) CampaignExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting) CampaignExtensionSettingOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingOperation) FeedItemSchedule(com.google.api.ads.adwords.axis.v201809.cm.FeedItemSchedule) FeedItemDevicePreference(com.google.api.ads.adwords.axis.v201809.cm.FeedItemDevicePreference) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) Customer(com.google.api.ads.adwords.axis.v201809.mcm.Customer) CampaignExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) CampaignExtensionSettingReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingReturnValue) CustomerServiceInterface(com.google.api.ads.adwords.axis.v201809.mcm.CustomerServiceInterface) SitelinkFeedItem(com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem) FeedItemGeoRestriction(com.google.api.ads.adwords.axis.v201809.cm.FeedItemGeoRestriction) FeedItemScheduling(com.google.api.ads.adwords.axis.v201809.cm.FeedItemScheduling) CampaignExtensionSettingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingServiceInterface) Location(com.google.api.ads.adwords.axis.v201809.cm.Location)

Example 65 with Campaign

use of com.google.api.ads.adwords.axis.v201809.cm.Campaign 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

Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)23 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)17 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)16 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)15 ArrayList (java.util.ArrayList)14 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)11 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)11 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)11 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)10 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)9 CampaignReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignReturnValue)9 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)6 CampaignCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation)6 CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)6 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)5 AdGroupServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface)5 BudgetOperation (com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation)5 CampaignCriterion (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion)5 CampaignCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)5 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)4