use of com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface 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.");
}
}
use of com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface in project googleads-java-lib by googleads.
the class ParallelReportDownload method getAllManagedCustomers.
/**
* Retrieves all managed customers under the manager account identified by
* {@link AdWordsSession#getClientCustomerId()}.
*/
private static Map<Long, ManagedCustomer> getAllManagedCustomers(AdWordsServicesInterface adWordsServices, ImmutableAdWordsSession session) throws RemoteException {
// Get the ManagedCustomerService.
ManagedCustomerServiceInterface managedCustomerService = adWordsServices.get(session, ManagedCustomerServiceInterface.class);
SelectorBuilder selectorBuilder = new SelectorBuilder().fields(ManagedCustomerField.CustomerId).equals(ManagedCustomerField.CanManageClients, "false").limit(PAGE_SIZE).offset(0);
ManagedCustomerPage managedCustomerPage;
int offset = 0;
Map<Long, ManagedCustomer> managedCustomers = Maps.newHashMap();
do {
selectorBuilder.offset(offset);
managedCustomerPage = managedCustomerService.get(selectorBuilder.build());
if (managedCustomerPage.getEntries() != null) {
for (ManagedCustomer managedCustomer : managedCustomerPage.getEntries()) {
managedCustomers.put(managedCustomer.getCustomerId(), managedCustomer);
}
}
offset += PAGE_SIZE;
} while (offset < managedCustomerPage.getTotalNumEntries());
return managedCustomers;
}
use of com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface in project googleads-java-lib by googleads.
the class CreateAccount 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.
ManagedCustomerServiceInterface managedCustomerService = adWordsServices.get(session, ManagedCustomerServiceInterface.class);
// Create account.
ManagedCustomer customer = new ManagedCustomer();
customer.setName("Customer created with ManagedCustomerService on " + DateTime.now());
customer.setCurrencyCode("EUR");
customer.setDateTimeZone("Europe/London");
// Create operations.
ManagedCustomerOperation operation = new ManagedCustomerOperation();
operation.setOperand(customer);
operation.setOperator(Operator.ADD);
ManagedCustomerOperation[] operations = new ManagedCustomerOperation[] { operation };
// Add account.
ManagedCustomerReturnValue result = managedCustomerService.mutate(operations);
// Display accounts.
for (ManagedCustomer customerResult : result.getValue()) {
System.out.printf("Account with customer ID %d was created.%n", customerResult.getCustomerId());
}
}
Aggregations