To get All ad accounts

108 views
Skip to first unread message

jeonhwan won

unread,
Aug 22, 2019, 3:24:11 AM8/22/19
to AdWords API and Google Ads API Forum

Hi there

I have three questions to get accounts information(name, id, parent id)

  1. Is there any sample code to get all accounts detail(name, id, parent id) information fast? It looks like If I have many accounts like 5,000, I need to make requests 5000 times. So please let me know If I can get all accounts within only one API call or a couple of times and If you leave some sample code for it Like Adwords have some code about it, would be really helpful.
    https://developers.google.com/adwords/api/docs/samples/java/account-management#get-the-account-hierarchy-under-the-current-account

  2. How can I get only enabled accounts? Let say I have 5,000 actual accounts but enabled accounts are about 2,000. So I want just that enabled accounts list (name, id, parent id).
    With AdWords API it's really easy to get it like below.

  • This is a sample with Adword
int offset = 0;
int PAGE_SIZE = 5000;

SelectorBuilder selectorBuilder = new SelectorBuilder()
                    .fields(ManagedCustomerField.CustomerId, ManagedCustomerField.Name,  ManagedCustomerField.CanManageClients)
                    .equals(ManagedCustomerField.CanManageClients, String.valueOf(false)).offset(offset).limit(PAGE_SIZE);

ManagedCustomerPage page;

page = managedCustomerService.get(selectorBuilder.build());

            for (ManagedCustomer customer : page.getEntries()) {

                    log.debug("customer: {}", customer.getName());

            }

  1. When I loop to get accounts information, I can see quite many times 'CUSTOMER_NOT_ENABLED' error, Even though I just want to get simple information like name, parent account id. Is there any other Service or resource I can use to get that simple information(name, parent id)?
  • I'm using CustomerServiceClient to get account information.

Here is code I tried it to get all accounts.. please let me know a better approach

// Property setting
Resource resource = new ClassPathResource("/ads.properties");
Properties props  = PropertiesLoaderUtils.loadProperties(resource);
props.setProperty("api.googleads.loginCustomerId", "xxxxxxxxx");

// Client create 
GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder().fromProperties(props).build();
GoogleAdsServiceClient googleAdsServiceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient();

// Sql setting 
String customer_client_sql = "SELECT customer.descriptive_name, customer.id, customer_client.resource_name, customer_client.client_customer, customer_client.level, customer_client.hidden  FROM customer_client";

// Search One time
 SearchGoogleAdsRequest request = SearchGoogleAdsRequest.newBuilder()
                .setCustomerId(testManagerId)
                .setPageSize(10000)
                .setQuery(customer_client_sql)
                .build();

// Loop Search to get Detail Customer Information 
for (GoogleAdsRow row : searchPagedResponse.getPage().getResponse().getResultsList()) {
     CustomerClient client = row.getCustomerClient();

     try {
                 CustomerServiceClient customerService = googleAdsClient.getLatestVersion().createCustomerServiceClient();
                Customer cc = customerService.getCustomer(client.getClientCustomer().getValue());            

                log.debug("customer info: {}", customer);
     } 
}

Google Ads API Forum Advisor Prod

unread,
Aug 22, 2019, 1:31:06 PM8/22/19
to demopro...@gmail.com, adwor...@googlegroups.com
Hi Won, 

Thank you for elaborating the issues in detail. Please find my response below:

1. The ListAccessibleCustomers will return the list of accounts that are directly accessible through the OAuth credentials that you're using. The CustomerClient will return the complete hierarchy of the account specified. This is similar to the AccountHierarchy code sample in AdWords API. Please find the sample query below:

SELECT customer_client.client_customer, customer_client.hidden, customer_client.level, customer_client.manager FROM customer_client

Could you please give this a try with the query above and let me know if you have any issues?

2. I'm afraid, it is currently not possible to filter the customer accounts based on status. There is a feature request already in place for this. Please keep an eye on our blog for more updates.

3. The CUSTOMER_NOT_ENABLED error is usually seen if the account is not completely setup. As mentioned in #2, it is currently not possible to query separately for the enabled accounts. 

Let me know if you have any further questions.

Thanks,
Bharani, Google Ads API Team



ref:_00D1U1174p._5001UHEL8E:ref
Reply all
Reply to author
Forward
0 new messages