use of com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion 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);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapter method createCriterionForAdd.
/**
* Returns a new AdGroupCriterion configured for an ADD operation.
*
* @param node the node whose criterion should be added
* @param adGroupId the ad group ID of the criterion
* @param biddingConfig the bidding strategy configuration of the criterion
*/
static AdGroupCriterion createCriterionForAdd(ProductPartitionNode node, long adGroupId, BiddingStrategyConfiguration biddingConfig) {
Preconditions.checkNotNull(node, "Null node");
Preconditions.checkNotNull(biddingConfig, "Null bidding configuration");
AdGroupCriterion adGroupCriterion;
if (node.isExcludedUnit()) {
adGroupCriterion = new NegativeAdGroupCriterion();
} else if (node.isBiddableUnit()) {
BiddableAdGroupCriterion biddableCriterion = new BiddableAdGroupCriterion();
if (node.getBid() != null) {
Money bidMoney = new Money();
bidMoney.setMicroAmount(node.getBid());
CpcBid cpcBid = new CpcBid();
cpcBid.setBid(bidMoney);
cpcBid.setCpcBidSource(BidSource.CRITERION);
biddingConfig.setBids(new Bids[] { cpcBid });
biddableCriterion.setBiddingStrategyConfiguration(biddingConfig);
}
if (node.getTrackingUrlTemplate() != null) {
biddableCriterion.setTrackingUrlTemplate(node.getTrackingUrlTemplate());
}
biddableCriterion.setUrlCustomParameters(createCustomParameters(node));
adGroupCriterion = biddableCriterion;
} else {
adGroupCriterion = new BiddableAdGroupCriterion();
}
adGroupCriterion.setAdGroupId(adGroupId);
ProductPartition partition = new ProductPartition();
partition.setId(node.getProductPartitionId());
if (node.getParent() != null) {
partition.setParentCriterionId(node.getParent().getProductPartitionId());
}
partition.setCaseValue(node.getDimension());
partition.setPartitionType(node.isUnit() ? ProductPartitionType.UNIT : ProductPartitionType.SUBDIVISION);
adGroupCriterion.setCriterion(partition);
return adGroupCriterion;
}
use of com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion in project googleads-java-lib by googleads.
the class AddDemographicTargetingCriteria method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group where demographic targeting will be modified.
* @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);
// https://developers.google.com/adwords/api/docs/appendix/genders
Gender male = new Gender();
male.setId(10L);
BiddableAdGroupCriterion genderBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
genderBiddableAdGroupCriterion.setAdGroupId(adGroupId);
genderBiddableAdGroupCriterion.setCriterion(male);
// https://developers.google.com/adwords/api/docs/appendix/ages
AgeRange undetermined = new AgeRange();
undetermined.setId(503999L);
NegativeAdGroupCriterion ageRangeNegativeAdGroupCriterion = new NegativeAdGroupCriterion();
ageRangeNegativeAdGroupCriterion.setAdGroupId(adGroupId);
ageRangeNegativeAdGroupCriterion.setCriterion(undetermined);
AdGroupCriterionOperation genderAdGroupCriterionOperation = new AdGroupCriterionOperation();
genderAdGroupCriterionOperation.setOperand(genderBiddableAdGroupCriterion);
genderAdGroupCriterionOperation.setOperator(Operator.ADD);
AdGroupCriterionOperation ageRangeNegativeAdGroupCriterionOperation = new AdGroupCriterionOperation();
ageRangeNegativeAdGroupCriterionOperation.setOperand(ageRangeNegativeAdGroupCriterion);
ageRangeNegativeAdGroupCriterionOperation.setOperator(Operator.ADD);
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(new AdGroupCriterionOperation[] { genderAdGroupCriterionOperation, ageRangeNegativeAdGroupCriterionOperation });
// Display campaigns.
for (AdGroupCriterion adGroupCriterion : result.getValue()) {
System.out.printf("AdGroup criterion with adGroup ID %d, criterion ID %d, " + "and type '%s' was added.%n", adGroupCriterion.getAdGroupId(), adGroupCriterion.getCriterion().getId(), adGroupCriterion.getCriterion().getCriterionType());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion in project googleads-java-lib by googleads.
the class AddKeywords method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group where the keywords 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 UnsupportedEncodingException if encoding the final URL failed.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws RemoteException, UnsupportedEncodingException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Create keywords.
Keyword keyword1 = new Keyword();
keyword1.setText("mars cruise");
keyword1.setMatchType(KeywordMatchType.BROAD);
Keyword keyword2 = new Keyword();
keyword2.setText("space hotel");
keyword2.setMatchType(KeywordMatchType.EXACT);
// Create biddable ad group criterion.
BiddableAdGroupCriterion keywordBiddableAdGroupCriterion1 = new BiddableAdGroupCriterion();
keywordBiddableAdGroupCriterion1.setAdGroupId(adGroupId);
keywordBiddableAdGroupCriterion1.setCriterion(keyword1);
// You can optionally provide these field(s).
keywordBiddableAdGroupCriterion1.setUserStatus(UserStatus.PAUSED);
String encodedFinalUrl = String.format("http://example.com/mars/cruise/?kw=%s", URLEncoder.encode(keyword1.getText(), UTF_8.name()));
keywordBiddableAdGroupCriterion1.setFinalUrls(new UrlList(new String[] { encodedFinalUrl }));
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
CpcBid bid = new CpcBid();
bid.setBid(new Money(null, 10000000L));
biddingStrategyConfiguration.setBids(new Bids[] { bid });
keywordBiddableAdGroupCriterion1.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
NegativeAdGroupCriterion keywordNegativeAdGroupCriterion2 = new NegativeAdGroupCriterion();
keywordNegativeAdGroupCriterion2.setAdGroupId(adGroupId);
keywordNegativeAdGroupCriterion2.setCriterion(keyword2);
// Create operations.
AdGroupCriterionOperation keywordAdGroupCriterionOperation1 = new AdGroupCriterionOperation();
keywordAdGroupCriterionOperation1.setOperand(keywordBiddableAdGroupCriterion1);
keywordAdGroupCriterionOperation1.setOperator(Operator.ADD);
AdGroupCriterionOperation keywordAdGroupCriterionOperation2 = new AdGroupCriterionOperation();
keywordAdGroupCriterionOperation2.setOperand(keywordNegativeAdGroupCriterion2);
keywordAdGroupCriterionOperation2.setOperator(Operator.ADD);
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { keywordAdGroupCriterionOperation1, keywordAdGroupCriterionOperation2 };
// Add keywords.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
// Display results.
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
System.out.printf("Keyword ad group criterion with ad group ID %d, criterion ID %d, " + "text '%s', and match type '%s' was added.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), ((Keyword) adGroupCriterionResult.getCriterion()).getText(), ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType());
}
}
Aggregations