Google Ads API is returning to beta status. Please read our blog post for more details.

与经理帐号关联

经理帐号是用于管理而不是投放广告的 Google Ads 帐号。对于所管理的帐号而言,经理帐号发挥着单个访问点的作用,因此,也用于设置涉及多个帐号的合并结算及其他功能。

服务

CustomerClientLinkServiceCustomerManagerLinkService 是 Google Ads API 中的两种服务,用于在两个帐号间建立关联。在这些服务的名称中,“Customer”一词指的是相关请求情境下当前正在使用的帐号。

  • 如果您使用的是经理帐号,管理着各个客户帐号,则应选用 CustomerClientLinkService

  • 如果您使用的是客户帐号,但希望与层次结构在您之上的经理帐号相关联,则应选择 CustomerManagerLinkService

这两种服务其实是同一关联的两种不同视图。如果经理帐号 M 管理客户帐号 C,则(从帐号 M 角度看的)CustomerClientLink 与(从帐号 C 角度看的)CustomerManagerLink 是同一个实体。

流程

在关联两个帐号时,必须始终从经理帐号发起关联,然后必须由客户帐号接受该关联。关联的状态存储在 CustomerClientLinkCustomerManagerLinkstatus 字段中。查看有效状态列表。请使用 PENDING 发起关联,然后使用 ACTIVE 接受关联。

在两个既有的 Google Ads 帐号之间建立关联,可以分三步完成。

  1. 在以经理帐号身份进行身份验证时,通过创建状态为 PENDINGCustomerClientLink 向客户帐号发出邀请。
  2. 在以经理帐号身份进行身份验证时,查询 GoogleAdsService 以找到您创建的 CustomerClientLinkmanager_link_id
  3. 在以客户帐号身份进行身份验证时,通过转变 CustomerManagerLink 使状态变为 ACTIVE,以此方式接受来自经理帐号的邀请。

示例

以下代码示例演示了如何在经理帐号与其客户帐号之间建立关联:

Ruby

def link_manager_to_client(manager_customer_id, client_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

  # This example assumes that the same credentials will work for both customers,
  # but that may not be the case. If you need to use different credentials
  # for each customer, then you may either update the client configuration or
  # instantiate two clients, one for each set of credentials. Always make sure
  # to update the configuration before fetching any services you need to use.

  # Extend an invitation to the client while authenticating as the manager.
  client.configure do |config|
    config.login_customer_id = manager_customer_id.to_i
  end

  client_link_service = client.service(:CustomerClientLink)

  client_link = client.resource(:CustomerClientLink)
  client_link.client_customer = client.wrapper.string(
    client.path.customer(client_customer_id),
  )
  client_link.status = :PENDING

  client_link_operation = client.operation(:CustomerClientLink)
  client_link_operation['create'] = client_link

  response = client_link_service.mutate_customer_client_link(
    manager_customer_id,
    client_link_operation,
  )

  client_link_resource_name = response.result.resource_name
  puts "Extended an invitation from customer #{manager_customer_id} to " \
      "customer #{client_customer_id} with client link resource name " \
      "#{client_link_resource_name}."

  # Find the manager_link_id of the link we just created, so we can construct
  # the resource name for the link from the client side.
  ga_service = client.service(:GoogleAds)

  query = <<~QUERY
    SELECT
      customer_client_link.manager_link_id
    FROM
      customer_client_link
    WHERE
      customer_client_link.resource_name = '#{client_link_resource_name}'
  QUERY

  response = ga_service.search(manager_customer_id, query)
  manager_link_id = response.first.customer_client_link.manager_link_id

  # Accept the link using the client account.
  client.configure do |config|
    config.login_customer_id = client_customer_id.to_i
  end

  manager_link_service = client.service(:CustomerManagerLink)

  manager_link = client.resource(:CustomerManagerLink)
  manager_link.resource_name = client.path.customer_manager_link(
    client_customer_id,
    manager_customer_id,
    manager_link_id,
  )

  mask = client.field_mask.with manager_link do
    manager_link.status = :ACTIVE
  end

  manager_link_operation = client.operation(:CustomerManagerLink)
  manager_link_operation['update'] = manager_link
  manager_link_operation['update_mask'] = mask

  response = manager_link_service.mutate_customer_manager_link(
    client_customer_id,
    [manager_link_operation],
  )

  puts "Client accepted invitation with resource name " \
      "#{response.results.first.resource_name}."
end