Get a list of MCC Accounts
I have been struggling with the Google Ads Api to get the MCC Accounts
for the last two weeks but I am unable to achieve what I want.
I have the App.Config figured out
<GoogleAdsApi> <!-- API-specific settings --> <add key="DeveloperToken" value="XXXXXXX"/> <!-- OAuth2 settings --> <add key="AuthorizationMethod" value="OAuth2" /> <add key = "OAuth2ClientId" value = "XXXXXXX.apps.googleusercontent.com" /> <add key = "OAuth2ClientSecret" value = " XXXXXXX " /> <add key = "OAuth2Mode" value="APPLICATION"/> <add key = "OAuth2RefreshToken" value = "1//0gd2 XXXXXXX " /> </GoogleAdsApi>
This is the code that I am currently working with
Public Sub GetMCCAccounts(ByVal client As GoogleAdsClient) Dim customerService As CustomerServiceClient = client.GetService(Services.V8.CustomerService) Dim customerResourceNames As String() = customerService.ListAccessibleCustomers() For Each customerResourceName As String In customerResourceNames Dim ManagerCustomerId = customerResourceName.Substring(customerResourceName.IndexOf("/") + 1) TextBox1.AppendText(vbCrLf & ManagerCustomerId) Next End Sub
I am not getting the correct results. The MCC accounts that I was hoping for do not appear in the output. I am getting different account numbers. What am I missing?
Expected MCC Accounts
I am looking for these 10 MCC accounts.
Sub Account Settings Overview
Output
I am getting these 16 account numbers.
64xxxxxxxx 71xxxxxxxx 10xxxxxxxx 88xxxxxxxx 32xxxxxxxx 58xxxxxxxx 31xxxxxxxx 73xxxxxxxx 98xxxxxxxx 22xxxxxxxx 48xxxxxxxx 37xxxxxxxx 98xxxxxxxx 94xxxxxxxx 88xxxxxxxx 43xxxxxxxx
After burning the midnight oil, I finally managed to crack it!
This finally gave me the MCC accounts that I wanted.
Imports Google.Ads.GoogleAds.Lib Imports Google.Ads.GoogleAds.V8.Services Imports Google.Ads.GoogleAds Imports Google.Ads.GoogleAds.V8.Resources Imports Google.Api.Gax Imports Google.Apis Public Class GoogleAdsCode Private Const PAGE_SIZE As Integer = 1000 '~~> Get MCC Accounts Public Shared Function GetMCCAccounts(MgrId As Long?) As List(Of String) Dim googleAdsClient As New GoogleAdsClient Dim googleAdsServiceClient As GoogleAdsServiceClient = googleAdsClient.GetService(Services.V8.GoogleAdsService) Dim customerServiceClient As CustomerServiceClient = googleAdsClient.GetService(Services.V8.CustomerService) Dim seedCustomerIds As New List(Of Long) Dim MgrList As New List(Of String) seedCustomerIds.Add(MgrId.Value) Const query As String = "SELECT customer_client.client_customer, customer_client.level, customer_client.manager, customer_client.descriptive_name, customer_client.currency_code, customer_client.time_zone, customer_client.id FROM customer_client WHERE customer_client.level <= 1" Dim customerIdsToChildAccounts As Dictionary(Of Long, List(Of CustomerClient)) = New Dictionary(Of Long, List(Of CustomerClient))() For Each seedCustomerId As Long In seedCustomerIds Dim unprocessedCustomerIds As Queue(Of Long) = New Queue(Of Long)() unprocessedCustomerIds.Enqueue(seedCustomerId) Dim rootCustomerClient As CustomerClient = Nothing While unprocessedCustomerIds.Count > 0 MgrId = unprocessedCustomerIds.Dequeue() Dim response As PagedEnumerable(Of SearchGoogleAdsResponse, GoogleAdsRow) = googleAdsServiceClient.Search(MgrId.ToString(), query, pageSize:=PAGE_SIZE) For Each googleAdsRow As GoogleAdsRow In response Dim customerClient As CustomerClient = googleAdsRow.CustomerClient If customerClient.ToString.Contains("MCC") Then MgrList.Add(customerClient.Id & " (" & customerClient.DescriptiveName & ")") Next End While Next Return MgrList End Function End Class