如要建立新的客戶帳戶,請使用 CustomerService 的 CreateCustomerClient 方法,提交預先填入的 Customer 物件。
與標準實體建立作業 (例如使用 mutate 呼叫建立廣告活動) 不同,您必須在要求中指定管理員帳戶的客戶 ID,而非要建立的客戶 ID。
要求和顧客欄位
呼叫 CustomerService.CreateCustomerClient 時,請設定 CreateCustomerClientRequest 及其巢狀 Customer 物件,如下表所示:
| 欄位或標頭 | 規定 | 可變動性與說明 |
|---|---|---|
customer_id |
必填 | 直接上層管理員帳戶的 10 位數客戶 ID (不含連字號),新客戶帳戶會建立在這個帳戶下。 |
login-customer-id (標頭) |
條件式 | 如果已驗證的使用者透過階層中較高的上層管理員帳戶存取父項管理員帳戶 (customer_id),則必須在要求標頭中提供。 |
customer_client.descriptive_name |
必填 | 可變動:新帳戶的顯示名稱 (例如 "Acct Created with CustomerService"),日後可使用 CustomerService.MutateCustomer 更新。 |
customer_client.currency_code |
必填 | 建立後無法變更:3 個字元的 ISO 4217 帳單幣別代碼 (例如 USD 或 EUR)。請參閱支援的貨幣代碼。 |
customer_client.time_zone |
必填 | 建立後無法變更:帳戶時區 ID (例如 America/New_York)。請參閱支援的時區。 |
email_address和access_role |
選用 (僅限許可清單) | 在建立帳戶時,邀請使用者 (ADMIN、STANDARD、READ_ONLY 或 EMAIL_ONLY) 加入。未列入許可清單的帳戶,必須在建立後使用 CustomerUserAccessInvitationService 邀請使用者。 |
validate_only |
選用 | 當 true 時,會驗證要求參數和管理員資格,但不會建立帳戶。 |
程式碼範例
以下範例說明如何在現有管理員帳戶下建立客戶帳戶:
Java
private void runExample(GoogleAdsClient googleAdsClient, Long managerId) { // Formats the current date/time to use as a timestamp in the new customer description. String dateTime = ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME); // Initializes a Customer object to be created. Customer customer = Customer.newBuilder() .setDescriptiveName("Account created with CustomerService on '" + dateTime + "'") .setCurrencyCode("USD") .setTimeZone("America/New_York") // Optional: Sets additional attributes of the customer. .setTrackingUrlTemplate("{lpurl}?device={device}") .setFinalUrlSuffix("keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}") .build(); // Sends the request to create the customer. try (CustomerServiceClient client = googleAdsClient.getLatestVersion().createCustomerServiceClient()) { CreateCustomerClientResponse response = client.createCustomerClient(managerId.toString(), customer); System.out.printf( "Created a customer with resource name '%s' under the manager account with" + " customer ID '%d'.%n", response.getResourceName(), managerId); } }
C#
public void Run(GoogleAdsClient client, long managerCustomerId) { // Get the CustomerService. CustomerServiceClient customerService = client.GetService(Services.V26.CustomerService); Customer customer = new Customer() { DescriptiveName = $"Account created with CustomerService on '{DateTime.Now}'", // For a list of valid currency codes and time zones see this documentation: // https://developers.google.com/google-ads/api/reference/data/codes-formats#codes_formats. CurrencyCode = "USD", TimeZone = "America/New_York", // The below values are optional. For more information about URL // options see: https://support.google.com/google-ads/answer/6305348. TrackingUrlTemplate = "{lpurl}?device={device}", FinalUrlSuffix = "keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}" }; try { // Create the account. CreateCustomerClientResponse response = customerService.CreateCustomerClient( managerCustomerId.ToString(), customer); // Display the result. Console.WriteLine($"Created a customer with resource name " + $"'{response.ResourceName}' under the manager account with customer " + $"ID '{managerCustomerId}'"); } 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, int $managerCustomerId) { $customer = new Customer([ 'descriptive_name' => 'Account created with CustomerService on ' . date('Ymd h:i:s'), // For a list of valid currency codes and time zones see this documentation: // https://developers.google.com/google-ads/api/reference/data/codes-formats. 'currency_code' => 'USD', 'time_zone' => 'America/New_York', // The below values are optional. For more information about URL // options see: https://support.google.com/google-ads/answer/6305348. 'tracking_url_template' => '{lpurl}?device={device}', 'final_url_suffix' => 'keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}' ]); // Issues a mutate request to create an account $customerServiceClient = $googleAdsClient->getCustomerServiceClient(); $response = $customerServiceClient->createCustomerClient( CreateCustomerClientRequest::build($managerCustomerId, $customer) ); printf( 'Created a customer with resource name "%s" under the manager account with ' . 'customer ID %d.%s', $response->getResourceName(), $managerCustomerId, PHP_EOL ); }
Python
def main(client: GoogleAdsClient, manager_customer_id: str) -> None: """The main method that creates all necessary entities for the example. Args: client: an initialized GoogleAdsClient instance. manager_customer_id: a manager client customer ID. """ customer_service: CustomerServiceClient = client.get_service( "CustomerService" ) customer: Customer = client.get_type("Customer") now: str = datetime.today().strftime("%Y%m%d %H:%M:%S") customer.descriptive_name = f"Account created with CustomerService on {now}" # For a list of valid currency codes and time zones see this documentation: # https://developers.google.com/google-ads/api/reference/data/codes-formats customer.currency_code = "USD" customer.time_zone = "America/New_York" # The below values are optional. For more information about URL # options see: https://support.google.com/google-ads/answer/6305348 customer.tracking_url_template = "{lpurl}?device={device}" customer.final_url_suffix = ( "keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}" ) response: CreateCustomerClientResponse = ( customer_service.create_customer_client( customer_id=manager_customer_id, customer_client=customer ) ) print( f'Customer created with resource name "{response.resource_name}" ' f'under manager account with ID "{manager_customer_id}".' )
小茹
def create_customer(manager_customer_id) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new customer = client.resource.customer do |c| c.descriptive_name = "Account created with CustomerService on #{(Time.new.to_f * 1000).to_i}" # For a list of valid currency codes and time zones, see this documentation: # https://developers.google.com/google-ads/api/reference/data/codes-formats c.currency_code = "USD" c.time_zone = "America/New_York" # The below values are optional. For more information about URL options, see: # https://support.google.com/google-ads/answer/6305348 c.tracking_url_template = "{lpurl}?device={device}" c.final_url_suffix = "keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}" end response = client.service.customer.create_customer_client( customer_id: manager_customer_id, customer_client: customer ) puts "Created a customer with resource name #{response.resource_name} under" + " the manager account with customer ID #{manager_customer_id}." end
Perl
sub create_customer { my ($api_client, $manager_customer_id) = @_; # Initialize a customer to be created. my $customer = Google::Ads::GoogleAds::V26::Resources::Customer->new({ descriptiveName => "Account created with CustomerService on #" . uniqid(), # For a list of valid currency codes and time zones, see this documentation: # https://developers.google.com/google-ads/api/reference/data/codes-formats currencyCode => "USD", timeZone => "America/New_York", # The below values are optional. For more information about URL options, see: # https://support.google.com/google-ads/answer/6305348 trackingUrlTemplate => "{lpurl}?device={device}", finalUrlSuffix => "keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}" }); # Create the customer client. my $create_customer_client_response = $api_client->CustomerService()->create_customer_client({ customerId => $manager_customer_id, customerClient => $customer }); printf "Created a customer with resource name '%s' under the manager account " . "with customer ID %d.\n", $create_customer_client_response->{resourceName}, $manager_customer_id; return 1; }
curl
# Creates a customer client. # # Variables: # API_VERSION, # MANAGER_CUSTOMER_ID, # OAUTH2_ACCESS_TOKEN: # See https://developers.google.com/google-ads/api/rest/auth#request_headers # for details. # curl -f --request POST \ "https://googleads.googleapis.com/v${API_VERSION}/customers/${MANAGER_CUSTOMER_ID}:createCustomerClient" \ --header "Content-Type: application/json" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data @- <<EOF { "customerClient": { "descriptiveName": "Account created with CustomerService #${RANDOM}", "currencyCode": "USD", "timeZone": "America/New_York" } } EOF
回應與後續步驟
成功後,CreateCustomerClient 會傳回 CreateCustomerClientResponse,其中包含新建立帳戶的 resource_name (customers/{new_customer_id}),以及 invitation_link (如果許可清單中的管理員帳戶包含 email_address 和 access_role)。然後,您可以使用這個資源名稱或客戶 ID,在階層中驗證帳戶、授予使用者存取權、設定帳單或填入廣告活動。
在管理員階層中驗證建立的帳戶
使用 GoogleAdsService.SearchStream 查詢管理員帳戶 (customer_id) 的 customer_client 資源,檢查新建立的子帳戶:
SELECT
customer_client.id,
customer_client.resource_name,
customer_client.client_customer,
customer_client.descriptive_name,
customer_client.currency_code,
customer_client.time_zone,
customer_client.manager,
customer_client.level,
customer_client.status
FROM customer_client
WHERE customer_client.level = 1
AND customer_client.id = {new_customer_id}
邀請使用者加入新帳戶
如果管理員帳戶不在許可清單中,無法直接在 CreateCustomerClientRequest 上設定 email_address 和 access_role,請使用 MutateCustomerUserAccessInvitation 在 CustomerUserAccessInvitationService 上傳送存取權邀請給新建立的 new_customer_id。透過上層管理員帳戶驗證時,請在 HTTP 要求標頭中加入 login-customer-id: {manager_customer_id}:
POST https://googleads.googleapis.com/v26/customers/{new_customer_id}/customerUserAccessInvitations:mutate
{
"operation": {
"create": {
"emailAddress": "user@example.com",
"accessRole": "ADMIN"
}
}
}
錯誤處理
對 CreateCustomerClient 的呼叫可能會傳回下列錯誤:
CustomerError:表示帳戶層級的建立限制,例如CREATION_DENIED_INELIGIBLE_MCC(管理員帳戶不符合建立新帳戶的資格) 或CREATION_DENIED_FOR_POLICY_VIOLATION(因違反政策而無法建立客戶)。ManagerLinkError:表示階層或帳戶限制條件,例如TOO_MANY_ACCOUNTS、TOO_MANY_ACCOUNTS_AT_MANAGER或MAX_DEPTH_EXCEEDED。AccessInvitationError:如果CreateCustomerClientRequest提供選用的email_address(以及access_role,僅適用於許可清單中的帳戶),邀請使用者加入新帳戶,但邀請失敗 (例如INVALID_EMAIL_ADDRESS、GOOGLE_CONSUMER_ACCOUNT_NOT_ALLOWED或EMAIL_DOMAIN_POLICY_VIOLATED),系統就會傳回這項錯誤。不在許可清單中的帳戶應使用CustomerUserAccessInvitationService在建立帳戶後邀請使用者。CurrencyCodeError.UNSUPPORTED或TimeZoneError.INVALID_TIME_ZONE:如果Customer物件上指定的currency_code或time_zone無效或不受支援,就會傳回這個錯誤。