Search in sources :

Example 16 with AdGroupAd

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

the class AddShoppingCampaign method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param budgetId the budget ID to use for the new campaign.
 * @param merchantId the Merchant Center ID for the new campaign.
 * @param createDefaultPartition if true, a default product partition for all products will be
 *     created.
 * @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 budgetId, Long merchantId, boolean createDefaultPartition) throws RemoteException {
    // Get the CampaignService
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    // Create campaign.
    Campaign campaign = new Campaign();
    campaign.setName("Shopping campaign #" + System.currentTimeMillis());
    // The advertisingChannelType is what makes this a Shopping campaign
    campaign.setAdvertisingChannelType(AdvertisingChannelType.SHOPPING);
    // Recommendation: Set the campaign to PAUSED when creating it to prevent
    // the ads from immediately serving. Set to ENABLED once you've added
    // targeting and the ads are ready to serve.
    campaign.setStatus(CampaignStatus.PAUSED);
    // Set shared budget (required).
    Budget budget = new Budget();
    budget.setBudgetId(budgetId);
    campaign.setBudget(budget);
    // Set bidding strategy (required).
    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
    biddingStrategyConfiguration.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
    campaign.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
    // All Shopping campaigns need a ShoppingSetting.
    ShoppingSetting shoppingSetting = new ShoppingSetting();
    shoppingSetting.setSalesCountry("US");
    shoppingSetting.setCampaignPriority(0);
    shoppingSetting.setMerchantId(merchantId);
    // Set to 'true' to enable Local Inventory Ads in your campaign.
    shoppingSetting.setEnableLocal(true);
    campaign.setSettings(new Setting[] { shoppingSetting });
    // Create operation.
    CampaignOperation campaignOperation = new CampaignOperation();
    campaignOperation.setOperand(campaign);
    campaignOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    CampaignReturnValue campaignAddResult = campaignService.mutate(new CampaignOperation[] { campaignOperation });
    // Display result.
    campaign = campaignAddResult.getValue(0);
    System.out.printf("Campaign with name '%s' and ID %d was added.%n", campaign.getName(), campaign.getId());
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
    // Create ad group.
    AdGroup adGroup = new AdGroup();
    adGroup.setCampaignId(campaign.getId());
    adGroup.setName("Ad Group #" + System.currentTimeMillis());
    // Create operation.
    AdGroupOperation adGroupOperation = new AdGroupOperation();
    adGroupOperation.setOperand(adGroup);
    adGroupOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupReturnValue adGroupAddResult = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });
    // Display result.
    adGroup = adGroupAddResult.getValue(0);
    System.out.printf("Ad group with name '%s' and ID %d was added.%n", adGroup.getName(), adGroup.getId());
    // Create product ad.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    ProductAd productAd = new ProductAd();
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroup.getId());
    adGroupAd.setAd(productAd);
    // Create operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupAdReturnValue adGroupAdAddResult = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
    // Display result.
    adGroupAd = adGroupAdAddResult.getValue(0);
    System.out.printf("Product ad with ID %d was added.%n", adGroupAd.getAd().getId());
    if (createDefaultPartition) {
        // Create an ad group criterion for 'All products' using the ProductPartitionTree utility.
        ProductPartitionTree productPartitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroup.getId());
        productPartitionTree.getRoot().asBiddableUnit().setBid(500000L);
        List<AdGroupCriterionOperation> mutateOperations = productPartitionTree.getMutateOperations();
        // Make the mutate request.
        AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
        AdGroupCriterionReturnValue adGroupCriterionResult = adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
        // Display result.
        for (AdGroupCriterion adGroupCriterion : adGroupCriterionResult.getValue()) {
            System.out.printf("Ad group criterion with ID %d in ad group with ID %d was added.%n", adGroupCriterion.getCriterion().getId(), adGroupCriterion.getAdGroupId());
        }
    }
}
Also used : ShoppingSetting(com.google.api.ads.adwords.axis.v201809.cm.ShoppingSetting) AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) ProductPartitionTree(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionTree) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) BiddingStrategyConfiguration(com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) CampaignReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignReturnValue) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) ProductAd(com.google.api.ads.adwords.axis.v201809.cm.ProductAd) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget) AdGroupReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Example 17 with AdGroupAd

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

the class AddShoppingDynamicRemarketingCampaign method runExample.

/**
 * Runs the example.
 *
 * @param services the services factory.
 * @param session the session.
 * @param merchantId the ID of the merchant center account from which to source product feed data.
 * @param budgetId the ID of a shared budget to associate with the campaign.
 * @param userListId the ID of a user list to target.
 * @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 IOException if the ad images failed to load.
 */
private static void runExample(AdWordsServicesInterface services, AdWordsSession session, long merchantId, long budgetId, long userListId) throws IOException {
    Campaign campaign = createCampaign(services, session, merchantId, budgetId);
    System.out.printf("Campaign with name '%s' and ID %d was added.%n", campaign.getName(), campaign.getId());
    AdGroup adGroup = createAdGroup(services, session, campaign);
    System.out.printf("Ad group with name '%s' and ID %d was added.%n", adGroup.getName(), adGroup.getId());
    AdGroupAd adGroupAd = createAd(services, session, adGroup);
    System.out.printf("Responsive display ad with ID %d was added.%n", adGroupAd.getAd().getId());
    attachUserList(services, session, adGroup, userListId);
    System.out.printf("User list with ID %d was attached to ad group with ID %d.%n", userListId, adGroup.getId());
}
Also used : Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup)

Example 18 with AdGroupAd

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

the class GetResponsiveSearchAds method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group to use to find expanded text ads.
 * @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 adGroupId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    int offset = 0;
    boolean morePages = true;
    // Create selector to get all of the ads for the ad group.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.Status, AdGroupAdField.ResponsiveSearchAdHeadlines, AdGroupAdField.ResponsiveSearchAdDescriptions).orderAscBy(AdGroupAdField.Id).offset(offset).limit(PAGE_SIZE).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).in(AdGroupAdField.Status, "ENABLED", "PAUSED").equals("AdType", AdType.RESPONSIVE_SEARCH_AD.getValue()).build();
    int totalEntries = 0;
    while (morePages) {
        // Get all ads.
        AdGroupAdPage page = adGroupAdService.get(selector);
        // Display ads.
        if (page.getEntries() != null && page.getEntries().length > 0) {
            totalEntries = page.getTotalNumEntries();
            for (AdGroupAd adGroupAd : page.getEntries()) {
                ResponsiveSearchAd responsiveSearchAd = (ResponsiveSearchAd) adGroupAd.getAd();
                System.out.printf("Responsive search ad with ID %d, status '%s' was found.%n", adGroupAd.getAd().getId(), adGroupAd.getStatus());
                System.out.println("Headlines:");
                for (AssetLink headline : responsiveSearchAd.getHeadlines()) {
                    ServedAssetFieldType pinning = headline.getPinnedField();
                    System.out.printf("    %s%n", ((TextAsset) headline.getAsset()).getAssetText());
                    if (pinning != null) {
                        System.out.printf("    (pinned to %s)%n", pinning);
                    }
                }
                System.out.println("Descriptions:");
                for (AssetLink description : responsiveSearchAd.getDescriptions()) {
                    ServedAssetFieldType pinning = description.getPinnedField();
                    System.out.printf("    %s%n", ((TextAsset) description.getAsset()).getAssetText());
                    if (pinning != null) {
                        System.out.printf("    (pinned to %s)%n", pinning);
                    }
                }
            }
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
    System.out.printf("Ad group ID %d has %d responsive search ads.%n", adGroupId, totalEntries);
}
Also used : AdGroupAdPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) ServedAssetFieldType(com.google.api.ads.adwords.axis.v201809.cm.ServedAssetFieldType) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ResponsiveSearchAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveSearchAd) AssetLink(com.google.api.ads.adwords.axis.v201809.cm.AssetLink) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 19 with AdGroupAd

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

the class PauseAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group for the ad.
 * @param adId the ID of the ad to pause.
 * @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 adGroupId, Long adId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create ad with updated status.
    Ad ad = new Ad();
    ad.setId(adId);
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create operations.
    AdGroupAdOperation operation = new AdGroupAdOperation();
    operation.setOperand(adGroupAd);
    operation.setOperator(Operator.SET);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { operation };
    // Update ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
    // Display ads.
    for (AdGroupAd adGroupAdResult : result.getValue()) {
        System.out.printf("Ad with ID %d, type '%s', and status '%s' was updated.%n", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getAdType(), adGroupAdResult.getStatus());
    }
}
Also used : Ad(com.google.api.ads.adwords.axis.v201809.cm.Ad) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)

Example 20 with AdGroupAd

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

the class ValidateTextAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group for the ad.
 * @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 adGroupId) throws RemoteException {
    // Enable validation.
    session.setValidateOnly(true);
    // Get the validation AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdValidationService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create text ad.
    ExpandedTextAd textAd1 = new ExpandedTextAd();
    textAd1.setHeadlinePart1("Luxury Cruise to Mars");
    textAd1.setHeadlinePart2("Visit the Red Planet in style.");
    textAd1.setDescription("Low-gravity fun for everyone!");
    textAd1.setFinalUrls(new String[] { "http://www.example.com" });
    // Create ad group ad.
    AdGroupAd textAdGroupAd1 = new AdGroupAd();
    textAdGroupAd1.setAdGroupId(adGroupId);
    textAdGroupAd1.setAd(textAd1);
    // Create operations.
    AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
    textAdGroupAdOperation1.setOperand(textAdGroupAd1);
    textAdGroupAdOperation1.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation1 };
    // Add ads.
    adGroupAdValidationService.mutate(operations);
    // No error means the request is valid.
    // Now let's check an invalid ad using a very long line to trigger an error.
    textAd1.setDescription("Low-gravity fun for all astronauts in orbit.");
    try {
        adGroupAdValidationService.mutate(operations);
    } catch (ApiException e) {
        System.err.printf("Validation failed for the following reason: %s%n", e.getMessage());
    }
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Aggregations

AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)21 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)19 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)16 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)12 ExpandedTextAd (com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd)6 ArrayList (java.util.ArrayList)5 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)4 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)4 Parameter (com.beust.jcommander.Parameter)3 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)3 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)3 AdGroupAdStatus (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus)3 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)3 AssetLink (com.google.api.ads.adwords.axis.v201809.cm.AssetLink)3 Image (com.google.api.ads.adwords.axis.v201809.cm.Image)3 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)3 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)3 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)3 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)3 ArgumentNames (com.google.api.ads.adwords.lib.utils.examples.ArgumentNames)3