Search in sources :

Example 6 with AdGroupAdServiceInterface

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

the class AddExpandedTextAds method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad 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 adGroupId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    List<AdGroupAdOperation> operations = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_ADS; i++) {
        // Create expanded text ad.
        ExpandedTextAd expandedTextAd = new ExpandedTextAd();
        expandedTextAd.setHeadlinePart1(String.format("Cruise #%d to Mars", i));
        expandedTextAd.setHeadlinePart2("Best Space Cruise Line");
        expandedTextAd.setHeadlinePart3("For Your Loved Ones");
        expandedTextAd.setDescription("Buy your tickets now!");
        expandedTextAd.setDescription2("Discount ends soon");
        expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/" + i });
        // Create ad group ad.
        AdGroupAd expandedTextAdGroupAd = new AdGroupAd();
        expandedTextAdGroupAd.setAdGroupId(adGroupId);
        expandedTextAdGroupAd.setAd(expandedTextAd);
        // Optional: set the status.
        expandedTextAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
        // Create the operation.
        AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
        adGroupAdOperation.setOperand(expandedTextAdGroupAd);
        adGroupAdOperation.setOperator(Operator.ADD);
        operations.add(adGroupAdOperation);
    }
    // Add ads.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[operations.size()]));
    // Display ads.
    Arrays.stream(result.getValue()).map(adGroupAdResult -> (ExpandedTextAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Expanded text ad with ID %d and headline '%s | %s%s' was added.%n", newAd.getId(), newAd.getHeadlinePart1(), newAd.getHeadlinePart2(), newAd.getHeadlinePart3() == null ? "" : String.format(" | %s", newAd.getHeadlinePart3())));
}
Also used : Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) ArrayList(java.util.ArrayList) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdStatus(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) 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) ArrayList(java.util.ArrayList) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)

Example 7 with AdGroupAdServiceInterface

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

the class AddResponsiveSearchAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad 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 adGroupId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    List<AdGroupAdOperation> operations = new ArrayList<>();
    ResponsiveSearchAd responsiveSearchAd = new ResponsiveSearchAd();
    List<AssetLink> headlines = new ArrayList<>();
    AssetLink pinnedHeadline = createAssetLinkForText("Cruise to Mars #" + System.currentTimeMillis());
    // Set a pinning to always choose this asset for HEADLINE_1. Pinning is optional; if no pinning
    // is set, then headlines and descriptions will be rotated and the ones that perform best will
    // be used more often.
    pinnedHeadline.setPinnedField(ServedAssetFieldType.HEADLINE_1);
    headlines.add(pinnedHeadline);
    headlines.add(createAssetLinkForText("Best Space Cruise Line"));
    headlines.add(createAssetLinkForText("Experience the Stars"));
    responsiveSearchAd.setHeadlines(headlines.toArray(new AssetLink[0]));
    List<AssetLink> descriptions = new ArrayList<>();
    descriptions.add(createAssetLinkForText("Buy your tickets now"));
    descriptions.add(createAssetLinkForText("Visit the Red Planet"));
    responsiveSearchAd.setDescriptions(descriptions.toArray(new AssetLink[0]));
    responsiveSearchAd.setFinalUrls(new String[] { "http://www.example.com/cruise" });
    responsiveSearchAd.setPath1("all-inclusive");
    responsiveSearchAd.setPath2("deals");
    // Create ad group ad.
    AdGroupAd responsiveSearchAdGroupAd = new AdGroupAd();
    responsiveSearchAdGroupAd.setAdGroupId(adGroupId);
    responsiveSearchAdGroupAd.setAd(responsiveSearchAd);
    // Optional: set the status.
    responsiveSearchAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create the operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(responsiveSearchAdGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    operations.add(adGroupAdOperation);
    // Add ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[0]));
    for (AdGroupAd adGroupAdResult : result.getValue()) {
        ResponsiveSearchAd newAd = (ResponsiveSearchAd) adGroupAdResult.getAd();
        System.out.printf("New responsive search ad with ID %d was added.%n", adGroupAdResult.getAd().getId());
        System.out.println("Headlines:");
        for (AssetLink headline : newAd.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 : newAd.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);
            }
        }
    }
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) ServedAssetFieldType(com.google.api.ads.adwords.axis.v201809.cm.ServedAssetFieldType) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ArrayList(java.util.ArrayList) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) ResponsiveSearchAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveSearchAd) AssetLink(com.google.api.ads.adwords.axis.v201809.cm.AssetLink)

Example 8 with AdGroupAdServiceInterface

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

the class GetExpandedTextAds 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.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.Status, AdGroupAdField.HeadlinePart1, AdGroupAdField.HeadlinePart2, AdGroupAdField.Description).orderAscBy(AdGroupAdField.Id).offset(offset).limit(PAGE_SIZE).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).in(AdGroupAdField.Status, "ENABLED", "PAUSED").equals("AdType", "EXPANDED_TEXT_AD").build();
    while (morePages) {
        // Get all ads.
        AdGroupAdPage page = adGroupAdService.get(selector);
        // Display ads.
        if (page.getEntries() != null && page.getEntries().length > 0) {
            for (AdGroupAd adGroupAd : page.getEntries()) {
                ExpandedTextAd expandedTextAd = (ExpandedTextAd) adGroupAd.getAd();
                System.out.printf("Expanded text ad with ID %d, status '%s', and headline '%s - %s' was found.%n", adGroupAd.getAd().getId(), adGroupAd.getStatus(), expandedTextAd.getHeadlinePart1(), expandedTextAd.getHeadlinePart2());
            }
        } else {
            System.out.println("No expanded text ads were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
}
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) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 9 with AdGroupAdServiceInterface

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

the class RemoveAd 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 remove.
 * @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 base class ad to avoid setting type specific fields.
    Ad ad = new Ad();
    ad.setId(adId);
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    // Create operations.
    AdGroupAdOperation operation = new AdGroupAdOperation();
    operation.setOperand(adGroupAd);
    operation.setOperator(Operator.REMOVE);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { operation };
    // Remove ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
    // Display ads.
    for (AdGroupAd adGroupAdResult : result.getValue()) {
        System.out.printf("Ad with ID %d and type '%s' was removed.%n", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getAdType());
    }
}
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 10 with AdGroupAdServiceInterface

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

the class AddSmartShoppingAd method createSmartShoppingAd.

/**
 * Creates a Smart Shopping ad.
 */
private static void createSmartShoppingAd(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws RemoteException {
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create a Smart Shopping ad (Goal-optimized Shopping ad).
    GoalOptimizedShoppingAd smartShoppingAd = new GoalOptimizedShoppingAd();
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(smartShoppingAd);
    // 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("Smart Shopping ad with ID %d was added.%n", adGroupAd.getAd().getId());
}
Also used : 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) GoalOptimizedShoppingAd(com.google.api.ads.adwords.axis.v201809.cm.GoalOptimizedShoppingAd)

Aggregations

AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)19 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)19 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)15 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)12 ExpandedTextAd (com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd)5 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)4 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)4 ArrayList (java.util.ArrayList)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