Search in sources :

Example 6 with ExpandedTextAd

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

the class AddExpandedTextAdWithUpgradedUrls 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);
    // Create expanded text ad with a tracking template and custom parameters.
    ExpandedTextAd expandedTextAd = new ExpandedTextAd();
    expandedTextAd.setHeadlinePart1("Luxury Cruise to Mars");
    expandedTextAd.setHeadlinePart2("Visit the Red Planet in style.");
    expandedTextAd.setDescription("Low-gravity fun for everyone!");
    // Specify a tracking url for 3rd party tracking provider. You may
    // specify one at customer, campaign, ad group, ad, criterion or
    // feed item levels.
    expandedTextAd.setTrackingUrlTemplate("http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}");
    // Since your tracking url has two custom parameters, provide their
    // values too. This can be provided at campaign, ad group, ad, criterion
    // or feed item levels.
    CustomParameter seasonParameter = new CustomParameter();
    seasonParameter.setKey("season");
    seasonParameter.setValue("christmas");
    CustomParameter promoCodeParameter = new CustomParameter();
    promoCodeParameter.setKey("promocode");
    promoCodeParameter.setValue("NYC123");
    CustomParameters trackingUrlParameters = new CustomParameters();
    trackingUrlParameters.setParameters(new CustomParameter[] { seasonParameter, promoCodeParameter });
    expandedTextAd.setUrlCustomParameters(trackingUrlParameters);
    // Specify a list of final urls. This field cannot be set if url field is
    // set. This may be specified at ad, criterion, and feed item levels.
    expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/cruise/space/", "http://www.example.com/locations/mars/" });
    // Specify a list of final mobile urls. This field cannot be set if url field is
    // set or finalUrls is not set. This may be specified at ad, criterion, and feed
    // item levels.
    expandedTextAd.setFinalMobileUrls(new String[] { "http://mobile.example.com/cruise/space/", "http://mobile.example.com/locations/mars/" });
    // Create ad group ad.
    AdGroupAd textAdGroupAd = new AdGroupAd();
    textAdGroupAd.setAdGroupId(adGroupId);
    textAdGroupAd.setAd(expandedTextAd);
    // Optional: Set status.
    textAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create operation.
    AdGroupAdOperation textAdGroupAdOperation = new AdGroupAdOperation();
    textAdGroupAdOperation.setOperand(textAdGroupAd);
    textAdGroupAdOperation.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation };
    // Add ad.
    AdGroupAd adGroupAdResult = adGroupAdService.mutate(operations).getValue(0);
    // Display ad.
    System.out.printf("Ad with ID %d and tracking URL template '%s' was added.", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getTrackingUrlTemplate());
}
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) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)

Example 7 with ExpandedTextAd

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

ExpandedTextAd (com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd)7 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)6 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)5 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)5 ArrayList (java.util.ArrayList)3 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)2 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)2 Parameter (com.beust.jcommander.Parameter)1 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)1 AdGroupAdStatus (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus)1 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)1 AdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdOperation)1 AdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdServiceInterface)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 CustomParameter (com.google.api.ads.adwords.axis.v201809.cm.CustomParameter)1 CustomParameters (com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)1 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)1 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)1