use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation 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());
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation in project googleads-java-lib by googleads.
the class AddHtml5Ad 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.
* @throws IOException if unable to get media data from the URL.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create the template ad.
TemplateAd html5Ad = new TemplateAd();
html5Ad.setName("Ad for HTML5");
html5Ad.setTemplateId(419L);
html5Ad.setFinalUrls(new String[] { "http://example.com/html5" });
html5Ad.setDisplayUrl("example.com/html5");
Dimensions dimensions = new Dimensions();
dimensions.setWidth(300);
dimensions.setHeight(250);
html5Ad.setDimensions(dimensions);
// The HTML5 zip file contains all the HTML, CSS, and images needed for the
// HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
// Designer (https://www.google.com/webdesigner/).
byte[] html5Zip = com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl("https://goo.gl/9Y7qI2");
// Create a media bundle containing the zip file with all the HTML5 components.
// NOTE: You may also upload an HTML5 zip using MediaService.upload()
// and simply set the mediaId field below. See UploadMediaBundle.java for an example.
MediaBundle mediaBundle = new MediaBundle();
mediaBundle.setData(html5Zip);
mediaBundle.setEntryPoint("carousel/index.html");
mediaBundle.setType(MediaMediaType.MEDIA_BUNDLE);
// Create the template elements for the ad. You can refer to
// https://developers.google.com/adwords/api/docs/appendix/templateads
// for the list of available template fields.
TemplateElementField media = new TemplateElementField();
media.setName("Custom_layout");
media.setFieldMedia(mediaBundle);
media.setType(TemplateElementFieldType.MEDIA_BUNDLE);
TemplateElementField layout = new TemplateElementField();
layout.setName("layout");
layout.setFieldText("Custom");
layout.setType(TemplateElementFieldType.ENUM);
TemplateElement adData = new TemplateElement();
adData.setUniqueName("adData");
adData.setFields(new TemplateElementField[] { media, layout });
html5Ad.setTemplateElements(new TemplateElement[] { adData });
// Create the AdGroupAd.
AdGroupAd html5AdGroupAd = new AdGroupAd();
html5AdGroupAd.setAdGroupId(adGroupId);
html5AdGroupAd.setAd(html5Ad);
// Optional: Set the status.
html5AdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperator(Operator.ADD);
operation.setOperand(html5AdGroupAd);
// Create the ads.
AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { operation });
for (AdGroupAd adGroupAd : result.getValue()) {
System.out.printf("New HTML5 ad with ID %d and display url '%s' was created.%n", adGroupAd.getAd().getId(), adGroupAd.getAd().getDisplayUrl());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation in project googleads-java-lib by googleads.
the class AddResponsiveDisplayAd 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.
* @throws IOException if uploading an image failed.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
// Get the MediaService.
MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create a responsive display ad.
ResponsiveDisplayAd responsiveDisplayAd = new ResponsiveDisplayAd();
// This ad format does not allow the creation of an image using the
// Image.data field. An image must first be created using the MediaService,
// and Image.mediaId must be populated when creating the ad.
long marketingImageMediaId = uploadImage(mediaService, "https://goo.gl/3b9Wfh");
Image marketingImage = new Image();
marketingImage.setMediaId(marketingImageMediaId);
responsiveDisplayAd.setMarketingImage(marketingImage);
responsiveDisplayAd.setShortHeadline("Travel");
responsiveDisplayAd.setLongHeadline("Travel the World");
responsiveDisplayAd.setDescription("Take to the air!");
responsiveDisplayAd.setBusinessName("Interplanetary Cruises");
responsiveDisplayAd.setFinalUrls(new String[] { "http://www.example.com/" });
// Optional: Create a square marketing image using MediaService, and set it
// to the ad.
long squareMarketingImageMediaId = uploadImage(mediaService, "https://goo.gl/mtt54n");
Image squareMarketingImage = new Image();
squareMarketingImage.setMediaId(squareMarketingImageMediaId);
responsiveDisplayAd.setSquareMarketingImage(squareMarketingImage);
// Optional: set call to action text.
responsiveDisplayAd.setCallToActionText("Shop Now");
// Optional: Set dynamic display ad settings, composed of landscape logo
// image, promotion text, and price prefix.
DynamicSettings dynamicDisplayAdSettings = createDynamicDisplayAdSettings(mediaService);
responsiveDisplayAd.setDynamicDisplayAdSettings(dynamicDisplayAdSettings);
// Whitelisted accounts only: Set color settings using hexadecimal values.
// Set allowFlexibleColor to false if you want your ads to render by always
// using your colors strictly.
/*
responsiveDisplayAd.setMainColor("#0000ff");
responsiveDisplayAd.setAccentColor("#ffff00");
responsiveDisplayAd.setAllowFlexibleColor(false);
*/
// Whitelisted accounts only: Set the format setting that the ad will be
// served in.
/*
responsiveDisplayAd.setFormatSetting(
com.google.api.ads.adwords.axis.v201809.cm.DisplayAdFormatSetting.NON_NATIVE);
*/
// Create ad group ad for the responsive display ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(responsiveDisplayAd);
// Optional: set the status.
adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(adGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
// Make the mutate request.
AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
// Display ads.
Arrays.stream(result.getValue()).map(adGroupAdResult -> (ResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Responsive display ad with ID %d and short headline '%s' was added.%n", newAd.getId(), newAd.getShortHeadline()));
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation 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());
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation 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());
}
}
Aggregations