הצגת רשימה של חשבונות נגישים

אתם יכולים להציג את רשימת הלקוחות שיש לכם גישה אליהם באמצעות השיטה ListAccessibleCustomers ב-CustomerService. עם זאת, חשוב להבין אילו לקוחות מוחזרים בבקשות מהסוג הזה.

לקוחות נגישים היא אחת מהבקשות היחידות ב-Google Ads API שבהן לא צריך לציין מספר לקוח בבקשה, ומתעלמים מכל login-customer-id שצוין.

רשימת הלקוחות שתתקבל מבוססת על פרטי הכניסה שלכם ל-OAuth. הבקשה מחזירה רשימה של כל החשבונות שאפשר לבצע עליהם פעולה ישירות עם פרטי הכניסה הנוכחיים שלכם. היא לא תכלול בהכרח את כל החשבונות בהיררכיית החשבונות, אלא רק חשבונות שבהם המשתמש המאומת נוסף עם הרשאות אדמין או הרשאות אחרות בחשבון.

נניח שאתם משתמשים A, עם הרשאת אדמין בארגון M1 ובחשבון C3 בשתי ההיררכיות שמפורטות למעלה. אם תבצעו קריאה ל-Google Ads API, למשל GoogleAdsService, תוכלו לגשת למידע של החשבונות M1, C1, C2 ו-C3. עם זאת, קריאה ל-CustomerService.ListAccessibleCustomers תחזיר רק את M1 ואת C3, כי אלה החשבונות היחידים שבהם למשתמש A יש גישה ישירה.

לפניכם קוד לדוגמה שממחיש את השימוש ב-method CustomerService.ListAccessibleCustomers:

Java

private void runExample(GoogleAdsClient client) {
  // Optional: Change credentials to use a different refresh token, to retrieve customers
  //           available for a specific user.
  //
  // UserCredentials credentials =
  //     UserCredentials.newBuilder()
  //         .setClientId("INSERT_OAUTH_CLIENT_ID")
  //         .setClientSecret("INSERT_OAUTH_CLIENT_SECRET")
  //         .setRefreshToken("INSERT_REFRESH_TOKEN")
  //         .build();
  //
  // client = client.toBuilder().setCredentials(credentials).build();

  try (CustomerServiceClient customerService =
      client.getLatestVersion().createCustomerServiceClient()) {
    ListAccessibleCustomersResponse response =
        customerService.listAccessibleCustomers(
            ListAccessibleCustomersRequest.newBuilder().build());

    System.out.printf("Total results: %d%n", response.getResourceNamesCount());

    for (String customerResourceName : response.getResourceNamesList()) {
      System.out.printf("Customer resource name: %s%n", customerResourceName);
    }
  }
}
      

C#

public void Run(GoogleAdsClient client)
{
    // Get the CustomerService.
    CustomerServiceClient customerService = client.GetService(Services.V17.CustomerService);

    try
    {
        // Retrieve the list of customer resources.
        string[] customerResourceNames = customerService.ListAccessibleCustomers();

        // Display the result.
        foreach (string customerResourceName in customerResourceNames)
        {
            Console.WriteLine(
                $"Found customer with resource name = '{customerResourceName}'.");
        }
    }
    catch (GoogleAdsException e)
    {
        Console.WriteLine("Failure:");
        Console.WriteLine($"Message: {e.Message}");
        Console.WriteLine($"Failure: {e.Failure}");
        Console.WriteLine($"Request ID: {e.RequestId}");
        throw;
    }
}
      

PHP

public static function runExample(GoogleAdsClient $googleAdsClient)
{
    $customerServiceClient = $googleAdsClient->getCustomerServiceClient();

    // Issues a request for listing all accessible customers.
    $accessibleCustomers =
        $customerServiceClient->listAccessibleCustomers(new ListAccessibleCustomersRequest());
    print 'Total results: ' . count($accessibleCustomers->getResourceNames()) . PHP_EOL;

    // Iterates over all accessible customers' resource names and prints them.
    foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
        /** @var string $resourceName */
        printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
    }
}
      

Python

def main(client):
    customer_service = client.get_service("CustomerService")

    accessible_customers = customer_service.list_accessible_customers()
    result_total = len(accessible_customers.resource_names)
    print(f"Total results: {result_total}")

    resource_names = accessible_customers.resource_names
    for resource_name in resource_names:
        print(f'Customer resource name: "{resource_name}"')
      

Ruby

def list_accessible_customers()
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new

  accessible_customers = client.service.customer.list_accessible_customers().resource_names

  accessible_customers.each do |resource_name|
    puts "Customer resource name: #{resource_name}"
  end
end
      

Perl

sub list_accessible_customers {
  my ($api_client) = @_;

  my $list_accessible_customers_response =
    $api_client->CustomerService()->list_accessible_customers();

  printf "Total results: %d.\n",
    scalar @{$list_accessible_customers_response->{resourceNames}};

  foreach
    my $resource_name (@{$list_accessible_customers_response->{resourceNames}})
  {
    printf "Customer resource name: '%s'.\n", $resource_name;
  }

  return 1;
}
      

הצגת רשימה של חשבונות שבוטלו

Google Ads API לא מאפשר להציג רשימה של החשבונות שבוטלו בחשבון ניהול באופן ישיר. עם זאת, אפשר להשתמש בפתרון החלופי הבא כדי לאחזר את הרשימה הזו.

  1. מאחזרים את רשימת הקישורים של ACTIVE באמצעות המשאב customer_client_link ויוצרים רשימה של הלקוחות באמצעות השדה customer_client_link.client_customer.

    SELECT customer_client_link.client_customer, customer_client_link.status FROM
        customer_client_link WHERE customer_client_link.status = ACTIVE
    
  2. מאחזרים את הרשימה של ENABLED החשבונות באמצעות המשאב customer_client.

    SELECT customer_client.id, customer_client.descriptive_name FROM customer_client
    
  3. בהתאם להפרש בין שתי הרשימות, תופיע רשימה של החשבונות שבוטלו.