use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class RemoveAd 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 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 adId) throws RemoteException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create base class ad to avoid setting type specific fields.
Ad ad = new Ad();
ad.setId(adId);
// Create ad group ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(ad);
// Create operations.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperand(adGroupAd);
operation.setOperator(Operator.REMOVE);
AdGroupAdOperation[] operations = new AdGroupAdOperation[] { operation };
// Remove ad.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
// Display ads.
for (AdGroupAd adGroupAdResult : result.getValue()) {
System.out.printf("Ad with ID %d and type '%s' was removed.%n", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getAdType());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class UpdateExpandedTextAd method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adId the ID of the ad 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 adId) throws RemoteException {
// Get the AdService.
AdServiceInterface adService = adWordsServices.get(session, AdServiceInterface.class);
// Creates an expanded text ad using the provided ad ID.
ExpandedTextAd expandedTextAd = new ExpandedTextAd();
expandedTextAd.setId(adId);
// Updates some properties of the expanded text ad.
expandedTextAd.setHeadlinePart1("Cruise to Pluto #" + System.currentTimeMillis());
expandedTextAd.setHeadlinePart2("Tickets on sale now");
expandedTextAd.setDescription("Best space cruise ever.");
expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/" });
expandedTextAd.setFinalMobileUrls(new String[] { "http://www.example.com/mobile" });
// Creates ad group ad operation and adds it to the list.
AdOperation operation = new AdOperation();
operation.setOperator(Operator.SET);
operation.setOperand(expandedTextAd);
// Updates the ad on the server.
ExpandedTextAd updatedAd = (ExpandedTextAd) adService.mutate(new AdOperation[] { operation }).getValue(0);
// Prints out some information.
System.out.printf("Expanded text ad with ID %d was updated.%n", updatedAd.getId());
System.out.printf("Headline part 1 is '%s'.%nHeadline part 2 is '%s'.%nDescription is '%s'.%n", updatedAd.getHeadlinePart1(), updatedAd.getHeadlinePart2(), updatedAd.getDescription());
System.out.printf("Final URL is '%s'.%nFinal mobile URL is '%s'.%n", updatedAd.getFinalUrls()[0], updatedAd.getFinalMobileUrls()[0]);
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class GetKeywords method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group to use to find keywords.
* @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 AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupCriterionField.Id, AdGroupCriterionField.CriteriaType, AdGroupCriterionField.KeywordMatchType, AdGroupCriterionField.KeywordText).orderAscBy(AdGroupCriterionField.KeywordText).offset(offset).limit(PAGE_SIZE).in(AdGroupCriterionField.AdGroupId, adGroupId.toString()).in(AdGroupCriterionField.CriteriaType, "KEYWORD").build();
while (morePages) {
// Get all ad group criteria.
AdGroupCriterionPage page = adGroupCriterionService.get(selector);
// Display ad group criteria.
if (page.getEntries() != null && page.getEntries().length > 0) {
// Display results.
Arrays.stream(page.getEntries()).map(adGroupCriterionResult -> (Keyword) adGroupCriterionResult.getCriterion()).forEach(keyword -> System.out.printf("Keyword with text '%s', match type '%s', criteria type '%s'," + " and ID %d was found.%n", keyword.getText(), keyword.getMatchType(), keyword.getType(), keyword.getId()));
} else {
System.out.println("No ad group criteria were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class RemoveAdGroup method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group 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) throws RemoteException {
// Get the AdGroupService.
AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
// Create ad group with REMOVED status.
AdGroup adGroup = new AdGroup();
adGroup.setId(adGroupId);
adGroup.setStatus(AdGroupStatus.REMOVED);
// Create operations.
AdGroupOperation operation = new AdGroupOperation();
operation.setOperand(adGroup);
operation.setOperator(Operator.SET);
AdGroupOperation[] operations = new AdGroupOperation[] { operation };
// Remove ad group.
AdGroupReturnValue result = adGroupService.mutate(operations);
// Display ad groups.
for (AdGroup adGroupResult : result.getValue()) {
System.out.printf("Ad group with name '%s' and ID %d was removed.%n", adGroupResult.getName(), adGroupResult.getId());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapterTest method testCommonAttributes.
/**
* Asserts that the attributes of {@code adGroupCriterion} match expectations.
*
* @param node the node from which the criterion was built
* @param adGroupCriterion the criterion created by {@link ProductPartitionNodeAdapter}
* @param isForRemove if true, this method will only check the attributes required for a REMOVE
* operation
*/
private void testCommonAttributes(ProductPartitionNode node, AdGroupCriterion adGroupCriterion, boolean isForRemove) {
assertEquals("Ad group ID is incorrect", adGroupId, adGroupCriterion.getAdGroupId());
Criterion criterion = adGroupCriterion.getCriterion();
assertTrue("Criterion should be a ProductPartition", criterion instanceof ProductPartition);
assertEquals("Partition ID is incorrect", node.getProductPartitionId(), criterion.getId());
if (isForRemove) {
assertEquals("Type of AdGroupCriterion for REMOVE should be the base class", AdGroupCriterion.class, adGroupCriterion.getClass());
// The above checks suffice for REMOVE operations.
return;
}
ProductPartition partition = (ProductPartition) criterion;
assertEquals("The caseValue of the partition does not match the dimension of the node", 0, new ProductDimensionComparator().compare(partition.getCaseValue(), node.getDimension()));
if (node.getParent() == null) {
assertNull("Parent ID should be null", partition.getParentCriterionId());
} else {
assertEquals("Parent ID does not match ID of parent node", node.getParent().getProductPartitionId(), partition.getParentCriterionId());
}
if (node.isBiddableUnit()) {
assertTrue("Biddable node should be translated into a BiddableAdGroupCriterion", adGroupCriterion instanceof BiddableAdGroupCriterion);
BiddableAdGroupCriterion biddableCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
BiddingStrategyConfiguration biddingStrategyConfig = biddableCriterion.getBiddingStrategyConfiguration();
if (node.getBid() == null) {
assertArrayEquals(new Bids[0], biddingStrategyConfig.getBids());
} else {
Bids bid = biddingStrategyConfig.getBids(0);
assertTrue("Bid should be a CpcBid", bid instanceof CpcBid);
CpcBid cpcBid = (CpcBid) bid;
assertEquals("Bid amount is incorrect", node.getBid(), cpcBid.getBid().getMicroAmount());
assertEquals("Partition is not a UNIT partition", ProductPartitionType.UNIT, partition.getPartitionType());
}
assertEquals("tracking URL template is incorrect", node.getTrackingUrlTemplate(), biddableCriterion.getTrackingUrlTemplate());
// The adapter should always have a CustomParameters object, even if the node had no params.
// This ensures that the parameters will be cleared (via doReplace=true) if all params were
// removed from the node.
CustomParameters customParameters = biddableCriterion.getUrlCustomParameters();
assertNotNull("Biddable criterion does not have custom parameters", customParameters);
assertEquals("doReplace for custom parameters should always be true", true, customParameters.getDoReplace());
// Convert the BiddableAdGroupCriterion's custom parameters to a map to simplify comparison
// against the node's custom parameter map.
Map<String, String> actualCustomParameters = new HashMap<>();
for (CustomParameter customParameter : customParameters.getParameters()) {
actualCustomParameters.put(customParameter.getKey(), customParameter.getValue());
}
assertEquals("node and criterion do not have the same custom parameters", node.getCustomParameters(), actualCustomParameters);
} else {
assertTrue("Excluded node should be translated into a NegativeAdGroupCriterion", adGroupCriterion instanceof NegativeAdGroupCriterion);
}
}
Aggregations