Search in sources :

Example 1 with ManagedCustomerLink

use of com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerLink 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)1 ManagedCustomer (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomer)1 ManagedCustomerLink (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerLink)1 ManagedCustomerPage (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerPage)1 ManagedCustomerServiceInterface (com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface)1