use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class EstimateKeywordTraffic 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 TrafficEstimatorService.
TrafficEstimatorServiceInterface trafficEstimatorService = adWordsServices.get(session, TrafficEstimatorServiceInterface.class);
// Create keywords. Refer to the TrafficEstimatorService documentation for the maximum
// number of keywords that can be passed in a single request.
// https://developers.google.com/adwords/api/docs/reference/latest/TrafficEstimatorService
List<Keyword> keywords = new ArrayList<Keyword>();
Keyword marsCruiseKeyword = new Keyword();
marsCruiseKeyword.setText("mars cruise");
marsCruiseKeyword.setMatchType(KeywordMatchType.BROAD);
keywords.add(marsCruiseKeyword);
Keyword cheapCruiseKeyword = new Keyword();
cheapCruiseKeyword.setText("cheap cruise");
cheapCruiseKeyword.setMatchType(KeywordMatchType.PHRASE);
keywords.add(cheapCruiseKeyword);
Keyword cruiseKeyword = new Keyword();
cruiseKeyword.setText("cruise");
cruiseKeyword.setMatchType(KeywordMatchType.EXACT);
keywords.add(cruiseKeyword);
// Create a keyword estimate request for each keyword.
List<KeywordEstimateRequest> keywordEstimateRequests = keywords.stream().map(keyword -> {
KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
keywordEstimateRequest.setKeyword(keyword);
return keywordEstimateRequest;
}).collect(Collectors.toList());
// Add a negative keyword to the traffic estimate.
KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();
negativeKeywordEstimateRequest.setKeyword(new Keyword(null, null, null, "hiking tour", KeywordMatchType.BROAD));
negativeKeywordEstimateRequest.setIsNegative(true);
keywordEstimateRequests.add(negativeKeywordEstimateRequest);
// Create ad group estimate requests.
List<AdGroupEstimateRequest> adGroupEstimateRequests = new ArrayList<AdGroupEstimateRequest>();
AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();
adGroupEstimateRequest.setKeywordEstimateRequests(keywordEstimateRequests.toArray(new KeywordEstimateRequest[] {}));
adGroupEstimateRequest.setMaxCpc(new Money(null, 1000000L));
adGroupEstimateRequests.add(adGroupEstimateRequest);
// Create campaign estimate requests.
List<CampaignEstimateRequest> campaignEstimateRequests = new ArrayList<CampaignEstimateRequest>();
CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();
campaignEstimateRequest.setAdGroupEstimateRequests(adGroupEstimateRequests.toArray(new AdGroupEstimateRequest[] {}));
Location unitedStates = new Location();
unitedStates.setId(2840L);
Language english = new Language();
english.setId(1000L);
campaignEstimateRequest.setCriteria(new Criterion[] { unitedStates, english });
campaignEstimateRequests.add(campaignEstimateRequest);
// Create selector.
TrafficEstimatorSelector selector = new TrafficEstimatorSelector();
selector.setCampaignEstimateRequests(campaignEstimateRequests.toArray(new CampaignEstimateRequest[] {}));
// Optional: Request a list of campaign level estimates segmented by platform.
selector.setPlatformEstimateRequested(true);
// Get traffic estimates.
TrafficEstimatorResult result = trafficEstimatorService.get(selector);
// Display traffic estimates.
if (result != null && result.getCampaignEstimates() != null && result.getCampaignEstimates().length > 0) {
CampaignEstimate campaignEstimate = result.getCampaignEstimates()[0];
// Display the campaign level estimates segmented by platform.
if (campaignEstimate.getPlatformEstimates() != null) {
for (PlatformCampaignEstimate platformEstimate : campaignEstimate.getPlatformEstimates()) {
String platformMessage = String.format("Results for the platform with ID %d and name '%s':%n", platformEstimate.getPlatform().getId(), platformEstimate.getPlatform().getPlatformName());
displayMeanEstimates(platformMessage, platformEstimate.getMinEstimate(), platformEstimate.getMaxEstimate());
}
}
// Display the keyword estimates.
KeywordEstimate[] keywordEstimates = campaignEstimate.getAdGroupEstimates()[0].getKeywordEstimates();
for (int i = 0; i < keywordEstimates.length; i++) {
if (Boolean.TRUE.equals(keywordEstimateRequests.get(i).getIsNegative())) {
continue;
}
Keyword keyword = keywordEstimateRequests.get(i).getKeyword();
KeywordEstimate keywordEstimate = keywordEstimates[i];
String keywordMessage = String.format("Results for the keyword with text '%s' and match type '%s':%n", keyword.getText(), keyword.getMatchType());
displayMeanEstimates(keywordMessage, keywordEstimate.getMin(), keywordEstimate.getMax());
}
} else {
System.out.println("No traffic estimates were returned.");
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad 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());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class RemoveKeyword method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group for the keyword criterion.
* @param criterionId the ID of the keyword criterion 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 criterionId) throws RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Create base class criterion to avoid setting keyword specific fields.
Criterion criterion = new Criterion();
criterion.setId(criterionId);
// Create ad group criterion.
AdGroupCriterion adGroupCriterion = new AdGroupCriterion();
adGroupCriterion.setAdGroupId(adGroupId);
adGroupCriterion.setCriterion(criterion);
// Create operations.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
operation.setOperand(adGroupCriterion);
operation.setOperator(Operator.REMOVE);
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { operation };
// Remove ad group criteria.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
// Display ad group criteria.
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
System.out.printf("Ad group criterion with ad group ID %d, criterion ID %d, and type " + "'%s' was removed.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), adGroupCriterionResult.getCriterion().getCriterionType());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class UpdateKeyword method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group for the criterion.
* @param keywordId the ID of the criterion to update.
* @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 keywordId) throws RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Create ad group criterion with updated bid.
Criterion criterion = new Criterion();
criterion.setId(keywordId);
BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
biddableAdGroupCriterion.setAdGroupId(adGroupId);
biddableAdGroupCriterion.setCriterion(criterion);
// Create bids.
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
CpcBid bid = new CpcBid();
bid.setBid(new Money(null, 10000000L));
biddingStrategyConfiguration.setBids(new Bids[] { bid });
biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
// Create operations.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
operation.setOperand(biddableAdGroupCriterion);
operation.setOperator(Operator.SET);
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { operation };
// Update ad group criteria.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
// Display ad group criteria.
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
if (adGroupCriterionResult instanceof BiddableAdGroupCriterion) {
biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
CpcBid criterionCpcBid = null;
// Find the criterion-level CpcBid among the keyword's bids.
for (Bids bids : biddableAdGroupCriterion.getBiddingStrategyConfiguration().getBids()) {
if (bids instanceof CpcBid) {
CpcBid cpcBid = (CpcBid) bids;
if (BidSource.CRITERION.equals(cpcBid.getCpcBidSource())) {
criterionCpcBid = cpcBid;
}
}
}
System.out.printf("Ad group criterion with ad group ID %d, criterion ID %d, type " + "'%s', and bid %d was updated.%n", biddableAdGroupCriterion.getAdGroupId(), biddableAdGroupCriterion.getCriterion().getId(), biddableAdGroupCriterion.getCriterion().getCriterionType(), criterionCpcBid.getBid().getMicroAmount());
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class SetAdParameters method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group of the keyword.
* @param keywordId the ID of the keyword.
* @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 keywordId) throws RemoteException {
// Get the AdParamService.
AdParamServiceInterface adParamService = adWordsServices.get(session, AdParamServiceInterface.class);
// Create ad params.
AdParam adParam1 = new AdParam();
adParam1.setAdGroupId(adGroupId);
adParam1.setCriterionId(keywordId);
adParam1.setInsertionText("100");
adParam1.setParamIndex(1);
AdParam adParam2 = new AdParam();
adParam2.setAdGroupId(adGroupId);
adParam2.setCriterionId(keywordId);
adParam2.setInsertionText("$40");
adParam2.setParamIndex(2);
// Create operations.
AdParamOperation adParamOperation1 = new AdParamOperation();
adParamOperation1.setOperand(adParam1);
adParamOperation1.setOperator(Operator.SET);
AdParamOperation adParamOperation2 = new AdParamOperation();
adParamOperation2.setOperand(adParam2);
adParamOperation2.setOperator(Operator.SET);
AdParamOperation[] operations = new AdParamOperation[] { adParamOperation1, adParamOperation2 };
// Set ad parameters.
AdParam[] adParams = adParamService.mutate(operations);
// Display ad parameters.
for (AdParam adParam : adParams) {
System.out.printf("Ad parameter with ad group ID %d, criterion ID %d, insertion text " + "'%s', and parameter index %d was set.%n", adParam.getAdGroupId(), adParam.getCriterionId(), adParam.getInsertionText(), adParam.getParamIndex());
}
}
Aggregations