use of com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder in project googleads-java-lib by googleads.
the class GetProductCategoryTaxonomy 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 constant data service.
ConstantDataServiceInterface constantDataService = adWordsServices.get(session, ConstantDataServiceInterface.class);
Selector selector = new SelectorBuilder().equals(ConstantDataField.Country, "US").build();
ProductBiddingCategoryData[] results = constantDataService.getProductBiddingCategoryData(selector);
// List of top level category nodes.
List<CategoryNode> rootCategories = new ArrayList<>();
// Map of category ID to category node for all categories found in the results.
Map<Long, CategoryNode> biddingCategories = Maps.newHashMap();
for (ProductBiddingCategoryData productBiddingCategoryData : results) {
Long id = productBiddingCategoryData.getDimensionValue().getValue();
String name = productBiddingCategoryData.getDisplayValue(0).getValue();
CategoryNode node = biddingCategories.get(id);
if (node == null) {
node = new CategoryNode(id, name);
biddingCategories.put(id, node);
} else if (node.name == null) {
// Ensure that the name attribute for the node is set. Name will be null for nodes added
// to biddingCategories as a result of being a parentNode below.
node.name = name;
}
if (productBiddingCategoryData.getParentDimensionValue() != null && productBiddingCategoryData.getParentDimensionValue().getValue() != null) {
Long parentId = productBiddingCategoryData.getParentDimensionValue().getValue();
CategoryNode parentNode = biddingCategories.get(parentId);
if (parentNode == null) {
parentNode = new CategoryNode(parentId);
biddingCategories.put(parentId, parentNode);
}
parentNode.children.add(node);
} else {
rootCategories.add(node);
}
}
displayCategories(rootCategories, "");
}
use of com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder in project googleads-java-lib by googleads.
the class LookupLocation method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param locationNames the location names to use for the lookup.
* @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, String[] locationNames) throws RemoteException {
// Get the LocationCriterionService.
LocationCriterionServiceInterface locationCriterionService = adWordsServices.get(session, LocationCriterionServiceInterface.class);
Selector selector = new SelectorBuilder().fields("Id", "LocationName", "CanonicalName", "DisplayType", "ParentLocations", "Reach", "TargetingStatus").in("LocationName", locationNames).equals("Locale", "en").build();
// Make the get request.
LocationCriterion[] locationCriteria = locationCriterionService.get(selector);
// Display the resulting location criteria.
for (LocationCriterion locationCriterion : locationCriteria) {
String parentString = getParentLocationString(locationCriterion.getLocation().getParentLocations());
System.out.printf("The search term '%s' returned the location '%s (%d)' of type '%s' " + "with parent locations '%s' and reach %d (%s).%n", locationCriterion.getSearchTerm(), locationCriterion.getLocation().getLocationName(), locationCriterion.getLocation().getId(), locationCriterion.getLocation().getDisplayType(), parentString, locationCriterion.getReach(), locationCriterion.getLocation().getTargetingStatus());
}
}
use of com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder in project googleads-java-lib by googleads.
the class AddDynamicPageFeed method updateCampaignDsaSetting.
/**
* Updates the campaign DSA setting to add DSA pagefeeds.
*/
private static void updateCampaignDsaSetting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long campaignId, DSAFeedDetails feedDetails) throws ApiException, RemoteException {
// Get the CampaignService.
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
Selector selector = new SelectorBuilder().fields(CampaignField.Id, CampaignField.Settings).equalsId(campaignId).build();
CampaignPage campaignPage = campaignService.get(selector);
if (campaignPage.getEntries() == null || campaignPage.getTotalNumEntries() == 0) {
throw new IllegalArgumentException("No campaign found with ID: " + campaignId);
}
Campaign campaign = campaignPage.getEntries(0);
if (campaign.getSettings() == null) {
throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
}
DynamicSearchAdsSetting dsaSetting = (DynamicSearchAdsSetting) Arrays.stream(campaign.getSettings()).filter(DynamicSearchAdsSetting.class::isInstance).findFirst().orElse(null);
if (dsaSetting == null) {
throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
}
// Use a page feed to specify precisely which URLs to use with your
// Dynamic Search Ads.
PageFeed pageFeed = new PageFeed();
pageFeed.setFeedIds(new long[] { feedDetails.feedId });
dsaSetting.setPageFeed(pageFeed);
// Optional: Specify whether only the supplied URLs should be used with your
// Dynamic Search Ads.
dsaSetting.setUseSuppliedUrlsOnly(true);
Campaign updatedCampaign = new Campaign();
updatedCampaign.setId(campaignId);
updatedCampaign.setSettings(campaign.getSettings());
CampaignOperation operation = new CampaignOperation();
operation.setOperand(updatedCampaign);
operation.setOperator(Operator.SET);
updatedCampaign = campaignService.mutate(new CampaignOperation[] { operation }).getValue(0);
System.out.printf("DSA page feed for campaign ID %d was updated with feed ID %d.%n", updatedCampaign.getId(), feedDetails.feedId);
}
use of com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder in project googleads-java-lib by googleads.
the class GetAccountChanges 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 CampaignService.
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
// Get the CustomerSyncService.
CustomerSyncServiceInterface customerSyncService = adWordsServices.get(session, CustomerSyncServiceInterface.class);
// Get a list of all campaign IDs.
List<Long> campaignIds = new ArrayList<>();
Selector selector = new SelectorBuilder().fields(CampaignField.Id).build();
CampaignPage campaigns = campaignService.get(selector);
if (campaigns.getEntries() != null) {
Arrays.stream(campaigns.getEntries()).forEach(campaign -> campaignIds.add(campaign.getId()));
}
// Create date time range for the past 24 hours.
DateTimeRange dateTimeRange = new DateTimeRange();
dateTimeRange.setMin(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24)));
dateTimeRange.setMax(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date()));
// Create selector.
CustomerSyncSelector customerSyncSelector = new CustomerSyncSelector();
customerSyncSelector.setDateTimeRange(dateTimeRange);
customerSyncSelector.setCampaignIds(ArrayUtils.toPrimitive(campaignIds.toArray(new Long[] {})));
// Get all account changes for campaign.
CustomerChangeData accountChanges = customerSyncService.get(customerSyncSelector);
// Display changes.
if (accountChanges != null && accountChanges.getChangedCampaigns() != null) {
System.out.printf("Most recent change: %s%n", accountChanges.getLastChangeTimestamp());
for (CampaignChangeData campaignChanges : accountChanges.getChangedCampaigns()) {
System.out.printf("Campaign with ID %d was changed:%n", campaignChanges.getCampaignId());
System.out.printf("\tCampaign changed status: '%s'%n", campaignChanges.getCampaignChangeStatus());
if (!ChangeStatus.NEW.equals(campaignChanges.getCampaignChangeStatus())) {
System.out.printf("\tAdded campaign criteria: %s%n", getFormattedList(campaignChanges.getAddedCampaignCriteria()));
System.out.printf("\tRemoved campaign criteria: %s%n", getFormattedList(campaignChanges.getRemovedCampaignCriteria()));
if (campaignChanges.getChangedAdGroups() != null) {
for (AdGroupChangeData adGroupChanges : campaignChanges.getChangedAdGroups()) {
System.out.printf("\tAd group with ID %d was changed:%n", adGroupChanges.getAdGroupId());
System.out.printf("\t\tAd group changed status: %s%n", adGroupChanges.getAdGroupChangeStatus());
if (!ChangeStatus.NEW.equals(adGroupChanges.getAdGroupChangeStatus())) {
System.out.printf("\t\tAds changed: %s%n", getFormattedList(adGroupChanges.getChangedAds()));
System.out.printf("\t\tCriteria changed: %s%n", getFormattedList(adGroupChanges.getChangedCriteria()));
System.out.printf("\t\tCriteria removed: %s%n", getFormattedList(adGroupChanges.getRemovedCriteria()));
}
}
}
}
System.out.println("");
}
} else {
System.out.println("No account changes were found.");
}
}
use of com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder in project googleads-java-lib by googleads.
the class GetAccountHierarchy 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 ServicedAccountService.
ManagedCustomerServiceInterface managedCustomerService = adWordsServices.get(session, ManagedCustomerServiceInterface.class);
// Create selector builder.
int offset = 0;
SelectorBuilder selectorBuilder = new SelectorBuilder().fields(ManagedCustomerField.CustomerId, ManagedCustomerField.Name).offset(offset).limit(PAGE_SIZE);
// Get results.
ManagedCustomerPage page;
// Map from customerId to customer node.
Map<Long, ManagedCustomerTreeNode> customerIdToCustomerNode = Maps.newHashMap();
// Map from each parent customer ID to its set of linked child customer IDs.
SortedSetMultimap<Long, Long> parentIdToChildIds = TreeMultimap.create();
do {
page = managedCustomerService.get(selectorBuilder.build());
if (page.getEntries() != null) {
// Create account tree nodes for each customer.
for (ManagedCustomer customer : page.getEntries()) {
ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
node.account = customer;
customerIdToCustomerNode.put(customer.getCustomerId(), node);
}
// Update the map of parent customer ID to child customer IDs.
if (page.getLinks() != null) {
for (ManagedCustomerLink link : page.getLinks()) {
parentIdToChildIds.put(link.getManagerCustomerId(), link.getClientCustomerId());
}
}
}
offset += PAGE_SIZE;
selectorBuilder.increaseOffsetBy(PAGE_SIZE);
} while (offset < page.getTotalNumEntries());
// of its parentNode.
for (Entry<Long, Long> parentIdEntry : parentIdToChildIds.entries()) {
ManagedCustomerTreeNode parentNode = customerIdToCustomerNode.get(parentIdEntry.getKey());
ManagedCustomerTreeNode childNode = customerIdToCustomerNode.get(parentIdEntry.getValue());
childNode.parentNode = parentNode;
parentNode.childAccounts.add(childNode);
}
// Find the root account node in the tree.
ManagedCustomerTreeNode rootNode = customerIdToCustomerNode.values().stream().filter(node -> node.parentNode == null).findFirst().orElse(null);
// Display serviced account graph.
if (rootNode != null) {
// Display account tree.
System.out.println("CustomerId, Name");
System.out.println(rootNode.toTreeString(0, new StringBuffer()));
} else {
System.out.println("No serviced accounts were found.");
}
}
Aggregations