use of com.google.api.ads.adwords.axis.v201809.cm.Ad 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.Ad in project googleads-java-lib by googleads.
the class AddProductPartitionTree method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group.
* @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 {
// Build a new ProductPartitionTree using the ad group's current set of criteria.
ProductPartitionTree partitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroupId);
System.out.printf("Original tree:%n%s%n", partitionTree);
// Clear out any existing criteria.
ProductPartitionNode rootNode = partitionTree.getRoot().removeAllChildren();
// Make the root node a subdivision.
rootNode = rootNode.asSubdivision();
// Add a unit node for condition = NEW.
rootNode.addChild(ProductDimensions.createCanonicalCondition(ProductCanonicalConditionCondition.NEW)).asBiddableUnit().setBid(200000L);
// Add a unit node for condition = USED.
rootNode.addChild(ProductDimensions.createCanonicalCondition(ProductCanonicalConditionCondition.USED)).asBiddableUnit().setBid(100000L);
// Add a subdivision node for condition = null (everything else).
ProductPartitionNode otherConditionNode = rootNode.addChild(ProductDimensions.createCanonicalCondition(null)).asSubdivision();
// Add a unit node under condition = null for brand = "CoolBrand".
otherConditionNode.addChild(ProductDimensions.createBrand("CoolBrand")).asBiddableUnit().setBid(900000L);
// Add a unit node under condition = null for brand = "CheapBrand".
otherConditionNode.addChild(ProductDimensions.createBrand("CheapBrand")).asBiddableUnit().setBid(10000L);
// Add a subdivision node under condition = null for brand = null (everything else).
ProductPartitionNode otherBrandNode = otherConditionNode.addChild(ProductDimensions.createBrand(null)).asSubdivision();
// Add unit nodes under condition = null/brand = null.
// The value for each bidding category is a fixed ID for a specific
// category. You can retrieve IDs for categories from the ConstantDataService.
// See the 'GetProductCategoryTaxonomy' example for more details.
// Add a unit node under condition = null/brand = null for product type
// level 1 = 'Luggage & Bags'.
otherBrandNode.addChild(ProductDimensions.createBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1, -5914235892932915235L)).asBiddableUnit().setBid(750000L);
// Add a unit node under condition = null/brand = null for product type
// level 1 = null (everything else).
otherBrandNode.addChild(ProductDimensions.createBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1, null)).asBiddableUnit().setBid(110000L);
// Get the ad group criterion service.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Make the mutate request, using the operations returned by the ProductPartitionTree.
List<AdGroupCriterionOperation> mutateOperations = partitionTree.getMutateOperations();
if (mutateOperations.isEmpty()) {
System.out.println("Skipping the mutate call because the original tree and the current tree " + "are logically identical.");
} else {
adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
}
// The request was successful, so create a new ProductPartitionTree based on the updated state
// of the ad group.
partitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroupId);
// Show the tree
System.out.printf("Updated tree:%n%s%n", partitionTree);
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class AddShoppingCampaign method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param budgetId the budget ID to use for the new campaign.
* @param merchantId the Merchant Center ID for the new campaign.
* @param createDefaultPartition if true, a default product partition for all products 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 budgetId, Long merchantId, boolean createDefaultPartition) throws RemoteException {
// Get the CampaignService
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
// Create campaign.
Campaign campaign = new Campaign();
campaign.setName("Shopping campaign #" + System.currentTimeMillis());
// The advertisingChannelType is what makes this a Shopping campaign
campaign.setAdvertisingChannelType(AdvertisingChannelType.SHOPPING);
// Recommendation: Set the campaign to PAUSED when creating it to prevent
// the ads from immediately serving. Set to ENABLED once you've added
// targeting and the ads are ready to serve.
campaign.setStatus(CampaignStatus.PAUSED);
// Set shared budget (required).
Budget budget = new Budget();
budget.setBudgetId(budgetId);
campaign.setBudget(budget);
// Set bidding strategy (required).
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
biddingStrategyConfiguration.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
campaign.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
// All Shopping campaigns need a ShoppingSetting.
ShoppingSetting shoppingSetting = new ShoppingSetting();
shoppingSetting.setSalesCountry("US");
shoppingSetting.setCampaignPriority(0);
shoppingSetting.setMerchantId(merchantId);
// Set to 'true' to enable Local Inventory Ads in your campaign.
shoppingSetting.setEnableLocal(true);
campaign.setSettings(new Setting[] { shoppingSetting });
// Create operation.
CampaignOperation campaignOperation = new CampaignOperation();
campaignOperation.setOperand(campaign);
campaignOperation.setOperator(Operator.ADD);
// Make the mutate request.
CampaignReturnValue campaignAddResult = campaignService.mutate(new CampaignOperation[] { campaignOperation });
// Display result.
campaign = campaignAddResult.getValue(0);
System.out.printf("Campaign with name '%s' and ID %d was added.%n", campaign.getName(), campaign.getId());
// Get the AdGroupService.
AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
// Create ad group.
AdGroup adGroup = new AdGroup();
adGroup.setCampaignId(campaign.getId());
adGroup.setName("Ad Group #" + System.currentTimeMillis());
// Create operation.
AdGroupOperation adGroupOperation = new AdGroupOperation();
adGroupOperation.setOperand(adGroup);
adGroupOperation.setOperator(Operator.ADD);
// Make the mutate request.
AdGroupReturnValue adGroupAddResult = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });
// Display result.
adGroup = adGroupAddResult.getValue(0);
System.out.printf("Ad group with name '%s' and ID %d was added.%n", adGroup.getName(), adGroup.getId());
// Create product ad.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
ProductAd productAd = new ProductAd();
// Create ad group ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroup.getId());
adGroupAd.setAd(productAd);
// Create operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(adGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
// Make the mutate request.
AdGroupAdReturnValue adGroupAdAddResult = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
// Display result.
adGroupAd = adGroupAdAddResult.getValue(0);
System.out.printf("Product ad with ID %d was added.%n", adGroupAd.getAd().getId());
if (createDefaultPartition) {
// Create an ad group criterion for 'All products' using the ProductPartitionTree utility.
ProductPartitionTree productPartitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroup.getId());
productPartitionTree.getRoot().asBiddableUnit().setBid(500000L);
List<AdGroupCriterionOperation> mutateOperations = productPartitionTree.getMutateOperations();
// Make the mutate request.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
AdGroupCriterionReturnValue adGroupCriterionResult = adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
// Display result.
for (AdGroupCriterion adGroupCriterion : adGroupCriterionResult.getValue()) {
System.out.printf("Ad group criterion with ID %d in ad group with ID %d was added.%n", adGroupCriterion.getCriterion().getId(), adGroupCriterion.getAdGroupId());
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class ServiceQueryTest method testNextPageWithSpecifiedAdGroupBidLandscapePage.
/**
* Tests using {@link ServiceQuery#nextPage(AdGroupBidLandscapePage)} ] to get a next page of the
* result by specifying the previous ad group bid landscape page.
*/
@Test
public void testNextPageWithSpecifiedAdGroupBidLandscapePage() {
// Make ad group bid landscape page contain 400 bid landscape points (200 per ad group bid
// landscape point).
int numBidLandscapePoints = 200;
BidLandscapeLandscapePoint bidLandscapeLandscapePoint = new BidLandscapeLandscapePoint();
List<BidLandscapeLandscapePoint> bidLandscapeLandscapePoints = new ArrayList<>();
bidLandscapeLandscapePoints.addAll(Collections.nCopies(numBidLandscapePoints, bidLandscapeLandscapePoint));
AdGroupBidLandscape bidLandscape = mock(AdGroupBidLandscape.class);
when(bidLandscape.getLandscapePoints()).thenReturn(bidLandscapeLandscapePoints.toArray(new BidLandscapeLandscapePoint[0]));
AdGroupBidLandscapePage page = mock(AdGroupBidLandscapePage.class);
when(page.getEntries()).thenReturn(new AdGroupBidLandscape[] { bidLandscape, bidLandscape });
expectedAwql = "SELECT Id, Name WHERE Status = \"ENABLED\" ORDER BY Name DESC LIMIT 400,100";
serviceQuery.nextPage(page);
assertEquals(expectedAwql, serviceQuery.toString());
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method getAdGroupBiddingStrategyConfiguration.
/**
* Retrieves the {@link BiddingStrategyConfiguration} of an ad group.
*
* @param services the AdWordsServices
* @param session the session to use for the request
* @param adGroupId the ad group ID
* @return the non-null BiddingStrategyConfiguration of the ad group
*/
private static BiddingStrategyConfiguration getAdGroupBiddingStrategyConfiguration(AdWordsServicesInterface services, AdWordsSession session, Long adGroupId) throws ApiException, RemoteException {
AdGroupServiceInterface adGroupService = services.get(session, AdGroupServiceInterface.class);
Selector selector = new SelectorBuilder().fields(AdGroupField.Id, AdGroupField.BiddingStrategyType, AdGroupField.BiddingStrategyId, AdGroupField.BiddingStrategyName).equalsId(adGroupId).build();
AdGroupPage adGroupPage = adGroupService.get(selector);
if (adGroupPage.getEntries() == null || adGroupPage.getEntries().length == 0) {
throw new IllegalArgumentException("No ad group found with ID " + adGroupId);
}
AdGroup adGroup = adGroupPage.getEntries(0);
Preconditions.checkState(adGroup.getBiddingStrategyConfiguration() != null, "Unexpected state - ad group ID %s has a null BiddingStrategyConfiguration", adGroupId);
return adGroup.getBiddingStrategyConfiguration();
}
Aggregations