use of com.google.api.ads.adwords.axis.v201809.cm.ApiException 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.ApiException 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.ApiException in project googleads-java-lib by googleads.
the class AddShoppingDynamicRemarketingCampaign method main.
public static void main(String[] args) {
final AdWordsServicesInterface services = AdWordsServices.getInstance();
AdWordsSession session;
try {
Credential oAuth2Credential;
oAuth2Credential = new OfflineCredentials.Builder().forApi(OfflineCredentials.Api.ADWORDS).fromFile().build().generateCredential();
session = new Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
} catch (ConfigurationLoadException cle) {
System.err.printf("Failed to load configuration from the %s file%n", DEFAULT_CONFIGURATION_FILENAME);
return;
} catch (ValidationException ve) {
System.err.printf("Invalid configuration in the %s file%n", DEFAULT_CONFIGURATION_FILENAME);
return;
} catch (OAuthException oe) {
System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file%n", DEFAULT_CONFIGURATION_FILENAME);
return;
}
final AddShoppingDynamicRemarketingCampaignParams params = new AddShoppingDynamicRemarketingCampaignParams();
if (!params.parseArguments(args)) {
params.merchantId = Long.parseLong("INSERT_MERCHANT_ID");
params.budgetId = Long.parseLong("INSERT_BUDGET_ID");
params.userListId = Long.parseLong("INSERT_USER_LIST_ID");
}
try {
runExample(services, session, params.merchantId, params.budgetId, params.userListId);
} catch (ApiException apiException) {
// ApiException is the base class for most exceptions thrown by an API request. Instances
// of this exception have a message and a collection of ApiErrors that indicate the
// type and underlying cause of the exception. Every exception object in the adwords.axis
// packages will return a meaningful value from toString.
//
// ApiException extends RemoteException, so this catch block must appear before the
// catch block for RemoteException.
System.err.print("Request failed due to ApiException. Underlying ApiErrors:");
if (apiException.getErrors() != null) {
int i = 0;
for (ApiError apiError : apiException.getErrors()) {
System.err.printf(" Error %d: %s%n", i++, apiError);
}
}
} catch (RemoteException re) {
System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
} catch (IOException e) {
System.err.printf("Request failed unexpectedly due to IOException: %s%n", e);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class CreateAndAttachSharedKeywordSet method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param campaignId the ID of the campaign that will use the shared set.
* @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 campaignId) throws RemoteException {
// Get the SharedSetService.
SharedSetServiceInterface sharedSetService = adWordsServices.get(session, SharedSetServiceInterface.class);
// Keywords to include in the shared set.
List<String> keywords = Arrays.asList("mars cruise", "mars hotels");
// Create the shared negative keyword set.
SharedSet sharedSet = new SharedSet();
sharedSet.setName("Negative keyword list #" + System.currentTimeMillis());
sharedSet.setType(SharedSetType.NEGATIVE_KEYWORDS);
SharedSetOperation sharedSetOperation = new SharedSetOperation();
sharedSetOperation.setOperator(Operator.ADD);
sharedSetOperation.setOperand(sharedSet);
// Add the shared set.
sharedSet = sharedSetService.mutate(new SharedSetOperation[] { sharedSetOperation }).getValue(0);
System.out.printf("Shared set with ID %d and name '%s' was successfully added.%n", sharedSet.getSharedSetId(), sharedSet.getName());
// Add negative keywords to the shared set.
List<SharedCriterionOperation> sharedCriterionOperations = new ArrayList<>();
for (String keyword : keywords) {
Keyword keywordCriterion = new Keyword();
keywordCriterion.setText(keyword);
keywordCriterion.setMatchType(KeywordMatchType.BROAD);
SharedCriterion sharedCriterion = new SharedCriterion();
sharedCriterion.setCriterion(keywordCriterion);
sharedCriterion.setNegative(true);
sharedCriterion.setSharedSetId(sharedSet.getSharedSetId());
SharedCriterionOperation sharedCriterionOperation = new SharedCriterionOperation();
sharedCriterionOperation.setOperator(Operator.ADD);
sharedCriterionOperation.setOperand(sharedCriterion);
sharedCriterionOperations.add(sharedCriterionOperation);
}
// Get the SharedCriterionService.
SharedCriterionServiceInterface sharedCriterionService = adWordsServices.get(session, SharedCriterionServiceInterface.class);
SharedCriterionReturnValue sharedCriterionReturnValue = sharedCriterionService.mutate(sharedCriterionOperations.toArray(new SharedCriterionOperation[sharedCriterionOperations.size()]));
for (SharedCriterion addedCriterion : sharedCriterionReturnValue.getValue()) {
System.out.printf("Added shared criterion ID %d with text '%s' to shared set with ID %d.%n", addedCriterion.getCriterion().getId(), ((Keyword) addedCriterion.getCriterion()).getText(), addedCriterion.getSharedSetId());
}
// Attach the negative keyword shared set to the campaign.
CampaignSharedSetServiceInterface campaignSharedSetService = adWordsServices.get(session, CampaignSharedSetServiceInterface.class);
CampaignSharedSet campaignSharedSet = new CampaignSharedSet();
campaignSharedSet.setCampaignId(campaignId);
campaignSharedSet.setSharedSetId(sharedSet.getSharedSetId());
CampaignSharedSetOperation campaignSharedSetOperation = new CampaignSharedSetOperation();
campaignSharedSetOperation.setOperator(Operator.ADD);
campaignSharedSetOperation.setOperand(campaignSharedSet);
campaignSharedSet = campaignSharedSetService.mutate(new CampaignSharedSetOperation[] { campaignSharedSetOperation }).getValue(0);
System.out.printf("Shared set ID %d was attached to campaign ID %d.%n", campaignSharedSet.getSharedSetId(), campaignSharedSet.getCampaignId());
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class GetAdGroupBidModifier method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @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) throws RemoteException {
// Get the AdGroupBidModifierService.
AdGroupBidModifierServiceInterface adGroupBidModifierService = adWordsServices.get(session, AdGroupBidModifierServiceInterface.class);
// Create selector.
Selector selector = new SelectorBuilder().fields(AdGroupBidModifierField.CampaignId, AdGroupBidModifierField.AdGroupId, AdGroupBidModifierField.BidModifier, AdGroupBidModifierField.Id).offset(0).limit(PAGE_SIZE).build();
AdGroupBidModifierPage page = adGroupBidModifierService.get(selector);
if (page.getEntries() != null) {
for (AdGroupBidModifier bidModifierResult : page.getEntries()) {
String bidModifierValue = bidModifierResult.getBidModifier() != null ? bidModifierResult.getBidModifier().toString() : "unset";
System.out.printf("Campaign ID %d, AdGroup ID %d, Criterion ID %d, " + "has ad group level modifier: %s%n", bidModifierResult.getCampaignId(), bidModifierResult.getAdGroupId(), bidModifierResult.getCriterion().getId(), bidModifierValue);
}
} else {
System.out.println("No ad group level bid modifiers were found.");
}
}
Aggregations