連結 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

視您使用的是 v15 以上版本 API 或較舊版本而定,連結要求在 Google Ads API 中管理方式有所不同。

如果使用

如要變更 Google Ads 帳戶中的 Merchant Center 連結狀態,請使用 Google Ads 網頁介面核准或拒絕邀請。您也可以按照下文說明,使用 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 帳戶中的 Merchant Center 連結狀態,您可以使用 Google Ads 網頁介面核准/拒絕連結要求,或是移除現有連結。使用 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

以下程式碼範例說明如何建構只變更狀態所需的 change 作業。

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 變更。