Search in sources :

Example 11 with SelectorBuilder

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, "");
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ArrayList(java.util.ArrayList) ConstantDataServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.ConstantDataServiceInterface) ProductBiddingCategoryData(com.google.api.ads.adwords.axis.v201809.cm.ProductBiddingCategoryData) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 12 with SelectorBuilder

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());
    }
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) LocationCriterion(com.google.api.ads.adwords.axis.v201809.cm.LocationCriterion) LocationCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.LocationCriterionServiceInterface) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 13 with SelectorBuilder

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);
}
Also used : CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) PageFeed(com.google.api.ads.adwords.axis.v201809.cm.PageFeed) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) DynamicSearchAdsSetting(com.google.api.ads.adwords.axis.v201809.cm.DynamicSearchAdsSetting) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 14 with SelectorBuilder

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.");
    }
}
Also used : CustomerChangeData(com.google.api.ads.adwords.axis.v201809.ch.CustomerChangeData) ArrayList(java.util.ArrayList) CampaignChangeData(com.google.api.ads.adwords.axis.v201809.ch.CampaignChangeData) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) Date(java.util.Date) CustomerSyncSelector(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupChangeData(com.google.api.ads.adwords.axis.v201809.ch.AdGroupChangeData) CustomerSyncServiceInterface(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncServiceInterface) DateTimeRange(com.google.api.ads.adwords.axis.v201809.cm.DateTimeRange) SimpleDateFormat(java.text.SimpleDateFormat) CustomerSyncSelector(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 15 with SelectorBuilder

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.");
    }
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ManagedCustomer(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomer) ManagedCustomerLink(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerLink) ManagedCustomerServiceInterface(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface) ManagedCustomerPage(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerPage)

Aggregations

SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)25 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)21 ArrayList (java.util.ArrayList)6 CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)4 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)4 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)3 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)3 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)3 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)3 Keyword (com.google.api.ads.adwords.axis.v201809.cm.Keyword)3 Parameter (com.beust.jcommander.Parameter)2 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)2 BatchJobHelper (com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobHelper)2 BatchJobMutateResponse (com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobMutateResponse)2 MutateResult (com.google.api.ads.adwords.axis.utils.v201809.batchjob.MutateResult)2 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)2 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)2 AdGroupCriterionPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionPage)2 AdGroupCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface)2 AdGroupPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupPage)2