use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.
the class AddGmailAd 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 {
// 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 logoImageId = uploadImage(adWordsServices, session, "https://goo.gl/mtt54n");
Image logoImage = new Image();
logoImage.setMediaId(logoImageId);
long marketingImageId = uploadImage(adWordsServices, session, "http://goo.gl/3b9Wfh");
Image adImage = new Image();
adImage.setMediaId(marketingImageId);
GmailTeaser teaser = new GmailTeaser();
teaser.setHeadline("Dream");
teaser.setDescription("Create your own adventure");
teaser.setBusinessName("Interplanetary Ships");
teaser.setLogoImage(logoImage);
// Create the Gmail ad.
GmailAd gmailAd = new GmailAd();
gmailAd.setTeaser(teaser);
gmailAd.setMarketingImage(adImage);
gmailAd.setMarketingImageHeadline("Travel");
gmailAd.setMarketingImageDescription("Take to the skies!");
gmailAd.setFinalUrls(new String[] { "http://www.example.com" });
// Create ad group ad for the Gmail ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(gmailAd);
// Additional properties (non-required).
adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create operation.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperator(Operator.ADD);
operation.setOperand(adGroupAd);
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Add Gmail ad.
AdGroupAdReturnValue returnValue = adGroupAdService.mutate(new AdGroupAdOperation[] { operation });
if (returnValue.getValue() != null) {
for (AdGroupAd newAdGroupAd : returnValue.getValue()) {
System.out.printf("New Gmail ad with ID %d and headline '%s' was added.%n", newAdGroupAd.getAd().getId(), ((GmailAd) newAdGroupAd.getAd()).getTeaser().getHeadline());
}
} else {
System.out.println("No Gmail ads were added.");
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.
the class AddMultiAssetResponsiveDisplayAd 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 retrieve an image from a URL.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
List<AdGroupAdOperation> operations = new ArrayList<>();
MultiAssetResponsiveDisplayAd ad = new MultiAssetResponsiveDisplayAd();
List<AssetLink> headlines = new ArrayList<>();
headlines.add(createAssetLinkForText("Travel to Mars"));
headlines.add(createAssetLinkForText("Travel to Jupiter"));
headlines.add(createAssetLinkForText("Travel to Pluto"));
headlines.add(createAssetLinkForText("Experience the Stars"));
ad.setHeadlines(headlines.toArray(new AssetLink[0]));
List<AssetLink> descriptions = new ArrayList<>();
descriptions.add(createAssetLinkForText("Visit the planet in a luxury spaceship."));
descriptions.add(createAssetLinkForText("See the planet in style."));
ad.setDescriptions(descriptions.toArray(new AssetLink[0]));
ad.setBusinessName("Galactic Luxury Cruises");
ad.setLongHeadline(createAssetLinkForText("Visit the planet in a luxury spaceship."));
// This ad format does not allow the creation of an image asset by setting the asset.imageData
// field. An image asset must first be created using the AssetService, and asset.assetId must be
// populated when creating the ad.
ad.setMarketingImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/3b9Wfh")) });
ad.setSquareMarketingImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/mtt54n")) });
ad.setFinalUrls(new String[] { "http://www.example.com" });
// Optional: set call to action text.
ad.setCallToActionText("Shop Now");
// Set color settings using hexadecimal values. Set allowFlexibleColor to false if you want
// your ads to render by always using your colors strictly.
ad.setMainColor("#0000ff");
ad.setAccentColor("#ffff00");
ad.setAllowFlexibleColor(false);
// Set the format setting that the ad will be served in.
ad.setFormatSetting(DisplayAdFormatSetting.NON_NATIVE);
// Optional: Set dynamic display ad settings, composed of landscape logo image, promotion text,
// and price prefix.
ad.setDynamicSettingsPricePrefix("as low as");
ad.setDynamicSettingsPromoText("Free shipping!");
ad.setLogoImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/mtt54n")) });
// Create ad group ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(ad);
// Optional: set the status.
adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(adGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
operations.add(adGroupAdOperation);
// Add ad.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[operations.size()]));
Arrays.stream(result.getValue()).map(adGroupAdResult -> (MultiAssetResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("New responsive display ad with ID %d and long headline '%s' was added.%n", newAd.getId(), ((TextAsset) newAd.getLongHeadline().getAsset()).getAssetText()));
}
use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.
the class AddMultiAssetResponsiveDisplayAd method uploadImageAsset.
/**
* Creates and uploads an {@link ImageAsset} for the specified URL.
*
* @return the ID of the {@link ImageAsset}.
* @throws IOException if unable to read the image from the specified URL.
*/
private static long uploadImageAsset(AdWordsServicesInterface adWordsServices, AdWordsSession session, String url) throws IOException {
AssetServiceInterface assetService = adWordsServices.get(session, AssetServiceInterface.class);
// Create the image asset.
ImageAsset image = new ImageAsset();
// Optional: Provide a unique friendly name to identify your asset. If you specify the assetName
// field, then both the asset name and the image being uploaded should be unique, and should not
// match another ACTIVE asset in this customer account.
// image.setAssetName("Image asset #" + System.currentTimeMillis());
image.setImageData(com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl(url));
// Create the operation.
AssetOperation operation = new AssetOperation();
operation.setOperator(Operator.ADD);
operation.setOperand(image);
// Create the asset and return the ID.
return assetService.mutate(new AssetOperation[] { operation }).getValue(0).getAssetId();
}
use of com.google.api.ads.adwords.axis.v201809.cm.Operation 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())));
}
use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.
the class MigrateToExtensionSettings method createExtensionSetting.
/**
* Creates the extension setting for a list of feed items.
*
* @param adWordsServices the AdWordsServices
* @param session the AdWordsSession
* @param feedItems the list of all feed items
* @param campaignFeed the original campaign feed
* @param feedItemIds IDs of the feed items for which extension settings should be created
* @param platformRestrictions the platform restrictions for the new campaign extension setting
*/
private static void createExtensionSetting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Map<Long, SiteLinkFromFeed> feedItems, CampaignFeed campaignFeed, Set<Long> feedItemIds, ExtensionSettingPlatform platformRestrictions) throws RemoteException {
// Get the CampaignExtensionSettingService.
CampaignExtensionSettingServiceInterface campaignExtensionSettingService = adWordsServices.get(session, CampaignExtensionSettingServiceInterface.class);
CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting();
campaignExtensionSetting.setCampaignId(campaignFeed.getCampaignId());
campaignExtensionSetting.setExtensionType(FeedType.SITELINK);
ExtensionSetting extensionSetting = new ExtensionSetting();
List<ExtensionFeedItem> extensionFeedItems = new ArrayList<>();
for (Long feedItemId : feedItemIds) {
SiteLinkFromFeed siteLinkFromFeed = feedItems.get(feedItemId);
SitelinkFeedItem siteLinkFeedItem = new SitelinkFeedItem();
siteLinkFeedItem.setSitelinkText(siteLinkFromFeed.text);
if (siteLinkFromFeed.finalUrls != null && siteLinkFromFeed.finalUrls.length > 0) {
siteLinkFeedItem.setSitelinkFinalUrls(new UrlList(siteLinkFromFeed.finalUrls));
if (siteLinkFromFeed.finalMobileUrls != null && siteLinkFromFeed.finalMobileUrls.length > 0) {
siteLinkFeedItem.setSitelinkFinalMobileUrls(new UrlList(siteLinkFromFeed.finalMobileUrls));
}
siteLinkFeedItem.setSitelinkTrackingUrlTemplate(siteLinkFromFeed.trackingUrlTemplate);
} else {
siteLinkFeedItem.setSitelinkUrl(siteLinkFromFeed.url);
}
siteLinkFeedItem.setSitelinkLine2(siteLinkFromFeed.line2);
siteLinkFeedItem.setSitelinkLine3(siteLinkFromFeed.line3);
extensionFeedItems.add(siteLinkFeedItem);
}
extensionSetting.setExtensions(extensionFeedItems.toArray(new ExtensionFeedItem[extensionFeedItems.size()]));
extensionSetting.setPlatformRestrictions(platformRestrictions);
campaignExtensionSetting.setExtensionSetting(extensionSetting);
CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation();
operation.setOperand(campaignExtensionSetting);
operation.setOperator(Operator.ADD);
campaignExtensionSettingService.mutate(new CampaignExtensionSettingOperation[] { operation });
}
Aggregations