将 Merchant Center 帐号与 Google Ads 帐号相关联

制作购物广告系列之前,您需要先将 Google Ads 帐号与 Google Merchant Center 帐号相关联,具体操作步骤如下:

  1. 从您的 Merchant Center 帐号向 Google Ads 帐号发送关联请求。
  2. 在您的 Google Ads 帐号中批准该关联请求。

您可以通过以下两种方式发送关联请求:

  1. 使用 Merchant Center 网页界面发送关联请求
  2. 使用 Content API for Shopping 更新 AccountadsLinks

在 Google Ads API 中,关联请求的管理方式有所不同,具体取决于您使用的是 v15 或更高版本的 API,还是更早的版本。

如果您使用的是

您可以通过使用 Google Ads 网页界面批准或拒绝邀请,在 Google Ads 帐号中更改 Merchant Center 关联状态。您还可以使用 Google Ads API 更新邀请或移除现有关联,如下所述。

列出所有 Merchant Center 邀请

您可以使用以下 GAQL 查询生成 Google Ads API 报告,以检索将 Google Ads 客户 ID 与 Merchant Center 帐号相关联的所有待处理邀请的列表。

SELECT
   product_link_invitation.merchant_center.merchant_center_id,
   product_link_invitation.type
FROM product_link_invitation
WHERE product_link_invitation.status = 'PENDING_APPROVAL'
  AND product_link_invitation.type = 'MERCHANT_CENTER'

如需检索所有邀请,请移除上述查询中 product_link_invitation.status 字段的过滤条件。

接受邀请

您可以通过将 product_link_invitation 状态设置为 ACCEPTED 来批准关联。

  1. 构造一个 UpdateProductLinkInvitationRequest 对象并将 customer_id 字段设置为 Google Ads 客户 ID。

  2. resource_name 字段设置为 product_link_invitation 的资源名称。

  3. product_link_invitation_status 设置为 ACCEPTED

  4. 发出 UpdateProductLinkInvitation API 调用。

如果已经是这两个帐号的管理员的用户尝试邀请流程,则会抛出 NO_INVITATION_REQUIRED 错误。您可以检查是否存在此错误,在此类情况下回退到直接链接流程。

拒绝邀请

拒绝邀请与接受邀请类似,只不过 product_link_invitation_status 字段设置为 REJECTED。如果邀请被拒绝,它将继续处于 REJECTED 状态,无法被接受。然后,您必须创建新的邀请(如果需要)。

无需邀请的直接关联

如果尝试将 Google Ads 帐号与 Merchant Center 帐号相关联的用户是这两个帐号的管理员,那么您可以绕过邀请步骤,使用 Google Ads API 直接关联这两个帐号。

  1. 构造一个 CreateProductLinkRequest 对象并将 customer_id 字段设置为 Google Ads 客户 ID。

  2. 创建一个新的 ProductLink 对象,并将其 merchant_center_id 字段设置为 Merchant Center 帐号的 ID。

  3. ProductLink 设置为请求对象的 product_link 字段。

  4. 发出 CreateProductLink API 调用。

如果没有足够的权限的用户尝试直接链接,系统会抛出 CREATION_NOT_PERMITTED 错误。您可以检查是否存在此错误,在这种情况下会回退到邀请流程。

您可以使用以下 GAQL 查询生成 Google Ads API 报告,以检索 Google Ads 客户 ID 的关联列表。

SELECT
  product_link.merchant_center.merchant_center_id,
  product_link.product_link_id
FROM product_link
WHERE product_link.type = 'MERCHANT_CENTER'

要解除关联,请按以下步骤操作:

  1. 构造一个 RemoveProductLinkRequest 对象并将 customer_id 字段设置为 Google Ads 客户 ID。

  2. resource_name 设置为 product_link 的资源名称。

  3. 发出 RemoveProductLink API 调用。

如果您使用的是

您可以使用 Google Ads 网页界面批准或拒绝关联请求,也可以移除现有关联,从而在 Google Ads 帐号中更改 Merchant Center 关联状态。若要使用 Google Ads API 移除现有关联和更新邀请,请按以下步骤操作:

  1. 如需检索所有 Merchant Center 关联,请发出请求,以从 MerchantCenterLink 资源中检索对象。

  2. 对于每个 MerchantCenterLink 对象,您可以查看 MerchantCenterLinkStatus 字段,找到关联状态。

  3. 如需批准 PENDING 关联,请将 MerchantCenterLinkStatus 字段设置为 ENABLED

    1. 要拒绝 PENDING 关联,请移除 MerchantCenterLink 对象。
    2. 要解除 ENABLED 关联,请移除 MerchantCenterLink 对象。

您可以使用 MerchantCenterLinkService 检索 Google Ads 客户 ID 的关联列表。

以下代码示例展示了如何使用 ListMerchantCenterLinks 请求 customer_id 的所有链接。

Java

ListMerchantCenterLinksResponse response =
    merchantCenterLinkService.listMerchantCenterLinks(
        ListMerchantCenterLinksRequest.newBuilder()
            .setCustomerId(Long.toString(customerId))
            .build());

System.out.printf(
    "%d Merchant Center link(s) found with the following details:%n",
    response.getMerchantCenterLinksCount());
      

C#

ListMerchantCenterLinksResponse response =
    merchantCenterLinkService.ListMerchantCenterLinks(customerId.ToString());

Console.WriteLine($"{response.MerchantCenterLinks.Count} Merchant Center link(s)" +
    $" found with the following details:");
      

PHP

// Lists all merchant links of the specified customer ID.
$merchantCenterLinkServiceClient = $googleAdsClient->getMerchantCenterLinkServiceClient();
$response = $merchantCenterLinkServiceClient->listMerchantCenterLinks(
    ListMerchantCenterLinksRequest::build($customerId)
);
printf(
    "%d Merchant Center link(s) found with the following details:%s",
    $response->getMerchantCenterLinks()->count(),
    PHP_EOL
);
      

Python

# Retrieve all the existing Merchant Center links.
response = merchant_center_link_service.list_merchant_center_links(
    customer_id=customer_id
)
print(
    f"{len(response.merchant_center_links)} Merchant Center link(s) "
    "found with the following details:"
)
      

Ruby

# Retrieve all the existing Merchant Center links.
response = client.service.v14.merchant_center_link.list_merchant_center_links(
  customer_id: customer_id,
)
      

Perl

# List all Merchant Center links of the specified customer ID.
my $merchant_center_link_service = $api_client->MerchantCenterLinkService();
my $response =
  $merchant_center_link_service->list({customerId => $customer_id});
printf
  "%d Merchant Center link(s) found with the following details:\n",
  scalar @{$response->{merchantCenterLinks}};
      

找到与您要批准或拒绝的 Merchant Center 帐号对应的 MerchantCenterLinkService 后,您需要查看 statusMerchantCenterLinkStatus 枚举描述了可能的状态。

Java

System.out.printf(
    "Link '%s' has status '%s'.%n",
    merchantCenterLink.getResourceName(), merchantCenterLink.getStatus());
      

C#

Console.Write($"Link '{merchantCenterLink.ResourceName}' has status " +
    $"'{merchantCenterLink.Status}'.");
      

PHP

printf(
    "Link '%s' has status '%s'.%s",
    $merchantCenterLink->getResourceName(),
    MerchantCenterLinkStatus::name($merchantCenterLink->getStatus()),
    PHP_EOL
);
      

Python

print(
    f"Link '{merchant_center_link.resource_name}' has status "
    f"'{merchant_center_link.status.name}'."
)
      

Ruby

#  Iterate the results, and filter for links with pending status.
response.merchant_center_links.each do |link|
  # Enables the pending link.
  if link.status == :PENDING && link.id.to_s == merchant_center_account_id
    # Creates the update operation.
    update_operation = client.operation.v14.update_resource.merchant_center_link(
      link.resource_name) do |updated_link|
      updated_link.status = :ENABLED
    end

    # Updates the link.
    mutate_response = client.service.v14.merchant_center_link.mutate_merchant_center_link(
      customer_id: customer_id,
      operation: update_operation,
    )

    # Display the result.
    puts "Enabled a Merchant Center Link with resource name " \
      "#{mutate_response.result.resource_name} " \
      "to Google Ads account #{customer_id}"
  end
end
      

Perl

printf
  "Link '%s' has status '%s'.\n",
  $merchant_center_link->{resourceName},
  $merchant_center_link->{status};
      

如果关联状态为 PENDING,您可以通过将状态设为 ENABLED 来批准关联。使用现有的 MerchantCenterLink 对象,您可以构建更新后的 MerchantCenterLink 对象,并将 status 设置为 ENABLED

以下代码示例展示了如何构建仅更改状态所需的 mutate 操作。

Java

private void updateMerchantCenterLinkStatus(
    MerchantCenterLinkServiceClient merchantCenterLinkServiceClient,
    long customerId,
    MerchantCenterLink merchantCenterLink,
    MerchantCenterLinkStatus status) {
  // Creates an updated MerchantCenterLink object derived from the original, but with the new
  // status.
  MerchantCenterLink updatedMerchantCenterLink =
      merchantCenterLink.toBuilder().setStatus(status).build();

  // Constructs an operation that will update the merchantCenterLink, using the FieldMasks compare
  // utility to derive the update mask from the changes. This mask tells the Google Ads API which
  // attributes of the merchantCenterLink to change. In this case we only want to change the
  // MerchantCenterLinkStatus.
  MerchantCenterLinkOperation operation =
      MerchantCenterLinkOperation.newBuilder()
          .setUpdate(updatedMerchantCenterLink)
          .setUpdateMask(FieldMasks.compare(merchantCenterLink, updatedMerchantCenterLink))
          .build();

  // Sends the operation in a mutate request.
  MutateMerchantCenterLinkResponse response =
      merchantCenterLinkServiceClient.mutateMerchantCenterLink(
          String.valueOf(customerId), operation);

  // Prints the resource name of the updated object.
  MutateMerchantCenterLinkResult merchantCenterLinkResult = response.getResult();
  System.out.printf(
      "Updated Merchant Center link with resource name: '%s'.%n",
      merchantCenterLinkResult.getResourceName());
}
      

C#

private static void UpdateMerchantCenterLinkStatus(long customerId,
    MerchantCenterLinkServiceClient merchantCenterLinkService,
    MerchantCenterLink merchantCenterLink, MerchantCenterLinkStatus status)
{
    // Enables the pending link.
    MerchantCenterLink linkToUpdate = new MerchantCenterLink()
    {
        ResourceName = merchantCenterLink.ResourceName,
        Status = status
    };

    // Creates an operation.
    MerchantCenterLinkOperation operation = new MerchantCenterLinkOperation()
    {
        Update = linkToUpdate,
        UpdateMask = FieldMasks.AllSetFieldsOf(linkToUpdate)
    };

    // Updates the link.
    MutateMerchantCenterLinkResponse mutateResponse =
        merchantCenterLinkService.MutateMerchantCenterLink(
            customerId.ToString(), operation);

    // Displays the result.
    Console.WriteLine($"The status of Merchant Center Link with resource name " +
        $"'{mutateResponse.Result.ResourceName}' to Google Ads account : " +
        $"{customerId} was updated to {status}.");
}
      

PHP

private static function updateMerchantCenterLinkStatus(
    MerchantCenterLinkServiceClient $merchantCenterLinkServiceClient,
    int $customerId,
    MerchantCenterLink $merchantCenterLink,
    int $newMerchantCenterLinkStatus
) {
    // Creates an updated MerchantCenterLink object derived from the original, but with the
    // specified status.
    $merchantCenterLinkToUpdate = new MerchantCenterLink([
        'resource_name' => $merchantCenterLink->getResourceName(),
        'status' => $newMerchantCenterLinkStatus
    ]);

    // Constructs an operation that will update the Merchant Center link,
    // using the FieldMasks utility to derive the update mask. This mask tells the
    // Google Ads API which attributes of the Merchant Center link you want to change.
    $merchantCenterLinkOperation = new MerchantCenterLinkOperation();
    $merchantCenterLinkOperation->setUpdate($merchantCenterLinkToUpdate);
    $merchantCenterLinkOperation->setUpdateMask(
        FieldMasks::allSetFieldsOf($merchantCenterLinkToUpdate)
    );

    // Issues a mutate request to update the Merchant Center link and prints some
    // information.
    $response = $merchantCenterLinkServiceClient->mutateMerchantCenterLink(
        MutateMerchantCenterLinkRequest::build($customerId, $merchantCenterLinkOperation)
    );
    printf(
        "Approved a Merchant Center Link with resource name '%s' to the Google Ads "
        . "account '%s'.%s",
        $response->getResult()->getResourceName(),
        $customerId,
        PHP_EOL
    );
}
      

Python

def update_merchant_center_link_status(
    client,
    customer_id,
    merchant_center_link_service,
    merchant_center_link,
    status,
):
    """Updates the status of a Merchant Center link request.

    Args:
        client: An initialized GoogleAdsClient instance.
        customer_id: The client customer ID string.
        merchant_center_link_service: A merchant center link service instance.
        merchant_center_link: The merchant center link to be modified.
        status: The updated status to apply to the merchant center link.
    """
    # Creates an operation.
    operation = client.get_type("MerchantCenterLinkOperation")
    link_to_update = operation.update
    link_to_update.resource_name = merchant_center_link.resource_name
    # Enables the pending link.
    link_to_update.status = status
    client.copy_from(
        operation.update_mask,
        protobuf_helpers.field_mask(None, link_to_update._pb),
    )

    # Updates the link.
    mutate_response = merchant_center_link_service.mutate_merchant_center_link(
        customer_id=customer_id, operation=operation
    )

    # Displays the result.
    print(
        "The status of Merchant Center Link with resource name "
        f"'{mutate_response.result.resource_name}' to Google Ads account : "
        f"{customer_id} was updated to {status.name}."
    )
      

Ruby

#  Iterate the results, and filter for links with pending status.
response.merchant_center_links.each do |link|
  # Enables the pending link.
  if link.status == :PENDING && link.id.to_s == merchant_center_account_id
    # Creates the update operation.
    update_operation = client.operation.v14.update_resource.merchant_center_link(
      link.resource_name) do |updated_link|
      updated_link.status = :ENABLED
    end

    # Updates the link.
    mutate_response = client.service.v14.merchant_center_link.mutate_merchant_center_link(
      customer_id: customer_id,
      operation: update_operation,
    )

    # Display the result.
    puts "Enabled a Merchant Center Link with resource name " \
      "#{mutate_response.result.resource_name} " \
      "to Google Ads account #{customer_id}"
  end
end
      

Perl

foreach my $merchant_center_link (@{$response->{merchantCenterLinks}}) {
  printf
    "Link '%s' has status '%s'.\n",
    $merchant_center_link->{resourceName},
    $merchant_center_link->{status};

  # Approve a pending link request for a Google Ads account with the specified
  # customer ID from a Merchant Center account with the specified Merchant
  # Center account ID.
  if ( $merchant_center_link->{id} == $merchant_center_account_id
    && $merchant_center_link->{status} eq PENDING)
  {
    # Update the status of Merchant Center link to 'ENABLED' to approve the link.
    update_merchant_center_link_status(
      $merchant_center_link_service, $customer_id,
      $merchant_center_link,         ENABLED
    );
    # There is only one MerchantCenterLink object for a given Google Ads account
    # and Merchant Center account, so we can break early.
    last;
  }
}
      

若要拒绝链接处于 PENDING 状态的关联请求,请使用 MutateMerchantCenterLinkRequest 为资源构建 remove 操作来移除 MerchantCenterLink

Java

private void removeMerchantCenterLink(
    MerchantCenterLinkServiceClient merchantCenterLinkServiceClient,
    long customerId,
    MerchantCenterLink merchantCenterLink) {
  // Creates a single remove operation, specifying the Merchant Center link resource name.
  MerchantCenterLinkOperation operation =
      MerchantCenterLinkOperation.newBuilder()
          .setRemove(merchantCenterLink.getResourceName())
          .build();

  // Sends the operation in a mutate request.
  MutateMerchantCenterLinkResponse response =
      merchantCenterLinkServiceClient.mutateMerchantCenterLink(
          Long.toString(customerId), operation);
  MutateMerchantCenterLinkResult result = response.getResult();
  System.out.printf(
      "Removed Merchant Center link with resource name: '%s'.%n", result.getResourceName());
}
      

C#

private void RemoveMerchantCenterLink(
    MerchantCenterLinkServiceClient merchantCenterLinkServiceClient,
    long customerId, MerchantCenterLink merchantCenterLink)
{
    // Creates a single remove operation, specifying the Merchant Center link resource name.
    MerchantCenterLinkOperation operation = new MerchantCenterLinkOperation
    {
        Remove = merchantCenterLink.ResourceName
    };

    // Sends the operation in a mutate request.
    MutateMerchantCenterLinkResponse response =
        merchantCenterLinkServiceClient.MutateMerchantCenterLink(
            customerId.ToString(), operation);
    Console.WriteLine("Removed Merchant Center Link with resource name: " +
                      $"{response.Result.ResourceName}");
}
      

PHP

private static function removeMerchantCenterLink(
    MerchantCenterLinkServiceClient $merchantCenterLinkServiceClient,
    int $customerId,
    MerchantCenterLink $merchantCenterLink
) {
    // Creates a single remove operation, specifying the Merchant Center link resource name.
    $merchantCenterLinkOperation = new MerchantCenterLinkOperation();
    $merchantCenterLinkOperation->setRemove($merchantCenterLink->getResourceName());

    // Issues a mutate request to remove the link and prints the result info.
    $response = $merchantCenterLinkServiceClient->mutateMerchantCenterLink(
        MutateMerchantCenterLinkRequest::build(
            $customerId,
            $merchantCenterLinkOperation
        )
    );
    $mutateMerchantCenterLinkResult = $response->getResult();
    printf(
        "Removed Merchant Center link with resource name: '%s'.%s",
        $mutateMerchantCenterLinkResult->getResourceName(),
        PHP_EOL
    );
}
      

Python

def remove_merchant_center_link(
    client, merchant_center_link_service, customer_id, merchant_center_link
):
    """Removes a Merchant Center link from a Google Ads client customer account.

    Args:
        client: An initialized Google Ads client.
        merchant_center_link_service: An initialized
            MerchantCenterLinkService client.
        customer_id: The Google Ads customer ID of the account that has the link
            request.
        merchant_center_link: The MerchantCenterLink object to remove.
    """
    # Create a single remove operation, specifying the Merchant Center link
    # resource name.
    operation = client.get_type("MerchantCenterLinkOperation")
    operation.remove = merchant_center_link.resource_name

    # Send the operation in a mutate request.
    response = merchant_center_link_service.mutate_merchant_center_link(
        customer_id=customer_id, operation=operation
    )
    print(
        "Removed Merchant Center link with resource name "
        f"'{response.result.resource_name}'."
    )
      

Ruby

def remove_merchant_center_link(
  client,
  merchant_center_link_service,
  customer_id,
  link)
  # Creates a single remove operation, specifying the Merchant Center link
  # resource name.
  operation = client.operation.v14.remove_resource.merchant_center_link(link.resource_name)

  # Issues a mutate request to remove the link and prints the result info.
  response = merchant_center_link_service.mutate_merchant_center_link(
    customer_id: customer_id,
    operation: operation,
  )
  puts "Removed Merchant Center link with resource name: " \
    "#{response.result.resource_name}"
end
      

Perl

sub reject_merchant_center_link {
  my ($api_client, $customer_id, $merchant_center_account_id) = @_;

  my $merchant_center_link_service = $api_client->MerchantCenterLinkService();

  # Reject a pending link request or unlink an enabled link for a Google Ads
  # account with $customer_id from a Merchant Center account with $merchant_center_account_id.
  my $response =
    $merchant_center_link_service->list({customerId => $customer_id});
  printf
    "%d Merchant Center link(s) found with the following details:\n",
    scalar @{$response->{merchantCenterLinks}};

  foreach my $merchant_center_link (@{$response->{merchantCenterLinks}}) {
    printf
      "Link '%s' has status '%s'.\n",
      $merchant_center_link->{resourceName},
      $merchant_center_link->{status};

    # Check if there is a link for the Merchant Center account we are looking for.
    if ($merchant_center_account_id == $merchant_center_link->{id}) {
      # If the Merchant Center link is pending, reject it by removing the link.
      # If the Merchant Center link is enabled, unlink Merchant Center from Google
      # Ads by removing the link.
      # In both cases, the remove action is the same.
      remove_merchant_center_link($merchant_center_link_service, $customer_id,
        $merchant_center_link);
      # There is only one MerchantCenterLink object for a given Google Ads account
      # and Merchant Center account, so we can break early.
      last;
    }
  }
  return 1;
}
      

如需解除处于 ENABLED 状态的链接,请使用 MutateMerchantCenterLinkRequest 为资源构建 remove 操作,以移除 MerchantCenterLink。此操作与拒绝关联请求相同。

商家管理工具是 Google 上的商家的一种统一表示形式。如果您使用商家管理工具账号管理 Google Ads 账号和 Merchant Center 账号,商家管理工具会自动创建 Google Ads 账号与 Merchant Center 账号之间的关联。您可以使用 Google Ads API 检索这些关联,但无法通过 Google Ads API 更改这些关联。