Merchant API 服務

Merchant API 服務可讓您在 Apps Script 中使用 Merchant API,上傳產品及管理 Merchant Center 帳戶。

如要進一步瞭解 Merchant API,請參閱參考說明文件。與 Apps Script 中的所有進階服務一樣,Merchant API 服務使用的物件、方法和參數,都與公開 API 相同。

Merchant API 是一組子 API,也就是相關服務和資源的群組。如需子 API 清單,請參閱這篇文章

如要在 Apps Script 中使用 Merchant API 服務,請透過下列任一選項啟用 Apps Script 進階服務:

  1. 為新專案啟用 appsscript.json。建議您為新的 Apps Script 專案選取這個選項。

  2. 為現有專案啟用 Apps Script。 使用這個方法,在現有專案中啟用其他子 API。

appsscript.json 中啟用 API

以下範例顯示 appsscript.json 檔案,可啟用「產品」、「帳戶」、「報表」和「資料來源」子 API。

  1. 在 Apps Script 編輯器中,按一下「專案設定」

  2. 啟用「在編輯器中顯示『appsscript.json』資訊清單檔案」選項。

  3. 在編輯器中,選取 appsscript.json 檔案。

  4. appsscript.json 檔案的內容替換為下列內容:

    {
      "dependencies": {
        "enabledAdvancedServices": [
          {
            "userSymbol": "MerchantApiAccounts",
            "version": "accounts_v1beta",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiDataSources",
            "version": "datasources_v1beta",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiProducts",
            "version": "products_v1beta",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiReports",
            "version": "reports_v1beta",
            "serviceId": "merchantapi"
          }
        ]
      },
      "exceptionLogging": "STACKDRIVER",
      "runtimeVersion": "V8"
    }
    
  5. 按一下 [儲存]

  6. 您現在可以在程式碼中將下列子 API 參照為:

    a. MerchantApiAccounts

    b. MerchantApiDataSources

    c. MerchantApiProducts

    d. MerchantApiReports

為其他子 API 或現有專案啟用 Apps Script

如要在現有專案中啟用子 API,請按照下列步驟操作。

  1. 開啟 Apps Script 專案。

  2. 按一下左側的「編輯器 < >」

  3. 在左側「服務」旁邊,按一下「新增服務 +」

  4. 在版本選取器中,選取要啟用的子 API。

  5. 在 ID 後方附加子 API 的名稱。舉例來說,如要啟用 Inventories 子 API,請選取 inventories_v1beta 版本,並將 ID 變更為 MerchantApiInventories

  6. 現在,您可以在程式碼中將 Inventories 子 API 參照為 MerchantApiInventories

程式碼範例

本節說明如何使用 Merchant API 存取特定功能。

列出產品

這個範例說明如何列出特定 Merchant Center 帳戶的產品。


/**
 * Lists all products for a given Merchant Center account.
 */
function productList() {
  // IMPORTANT:
  // Enable the Merchant API Products Bundle Advanced Service and call it
  // "MerchantApiProducts"

  // Replace this with your Merchant Center ID.
  const accountId = '<MERCHANT_CENTER_ID>';

  // Construct the parent name
  const parent = 'accounts/' + accountId;

  try {
    console.log('Sending list Products request');
    let pageToken;
    // Set the page size to 1000. This is the maximum allowed page size.
    let pageSize = 1000;

    console.log('Retrieved products below:');
    // Call the Products.list API method. Use the pageToken to iterate through
    // all pages of results.
    do {
      response = MerchantApiProducts.Accounts.Products.list(parent, {pageToken, pageSize});
      console.log(response);
      pageToken = response.nextPageToken;
    } while (pageToken); // Exits when there is no next page token.
  } catch (e) {
    console.log('ERROR!');
    console.log(e);
  }
}

篩選遭拒登的產品

這個範例說明如何在 Merchant Center 帳戶中篩選出已拒登的產品。


/**
 * Demonstrates how to filter disapproved products using the Merchant API Reports service.
 */
function filterDisapprovedProducts() {
  // IMPORTANT:
  // Enable the Merchant API Reports Bundle Advanced Service and call it
  // "MerchantApiReports"
  // Enable the Merchant API Products Bundle Advanced Service and call it
  // "MerchantApiProducts"

  // Replace this with your Merchant Center ID.
  const accountId = '<INSERT_MERCHANT_CENTER_ID>';

  // Construct the parent name
  const parent = 'accounts/' + accountId;

  try {
    console.log('Sending search Report request');
    // Set pageSize to the maximum value (default: 1000)
    let pageSize = 1000;
    let pageToken;
    // The query below is an example of a query for the productView that gets product informations
    // for all disapproved products.
    let query = 'SELECT offer_id,' +
        'id,' +
        'price,' +
        'title' +
        ' FROM product_view' +
        ' WHERE aggregated_reporting_context_status = "NOT_ELIGIBLE_OR_DISAPPROVED"';


    // Call the Reports.search API method. Use the pageToken to iterate through
    // all pages of results.
    do {
      response =
          MerchantApiReports.Accounts.Reports.search({query, pageSize, pageToken}, parent);
      for (const reportRow of response.results) {
        console.log("Printing data from Product View:");
        console.log(reportRow);

        // OPTIONALLY, you can get the full product details by calling the GetProduct method.
        let productName = parent + "/products/" + reportRow.getProductView().getId();
        product = MerchantApiProducts.Accounts.Products.get(productName);
        console.log(product);
      }
      pageToken = response.nextPageToken;
    } while (pageToken);  // Exits when there is no next page token.

  } catch (e) {
    console.log('ERROR!');
    console.log('Error message:' + e.message);
  }
}

擷取指定帳戶的報表

這個範例說明如何擷取特定 Merchant Center 帳戶的報表。


/**
 * Searches a report for a given Merchant Center account.
 */
function searchReport() {
  // IMPORTANT:
  // Enable the Merchant API Reports Bundle Advanced Service and call it
  // "MerchantApiReports"

  // Replace this with your Merchant Center ID.
  const accountId = '<MERCHANT_CENTER_ID>';

  // Construct the parent name
  const parent = 'accounts/' + accountId;

  try {
    console.log('Sending search Report request');
    // Set pageSize to the maximum value (default: 1000)
    let pageSize = 1000;
    let pageToken;
    // Uncomment the desired query from below. Documentation can be found at
    // https://developers.google.com/merchant/api/reference/rest/reports_v1beta/accounts.reports#ReportRow
    // The query below is an example of a query for the product_view.
    let query = 'SELECT offer_id,' +
        'id,' +
        'price,' +
        'gtin,' +
        'item_issues,' +
        'channel,' +
        'language_code,' +
        'feed_label,' +
        'title,' +
        'brand,' +
        'category_l1,' +
        'product_type_l1,' +
        'availability,' +
        'shipping_label,' +
        'thumbnail_link,' +
        'click_potential' +
        ' FROM product_view';

    /*
    // The query below is an example of a query for the
    price_competitiveness_product_view. let query = "SELECT offer_id,"
                 + "id,"
                 + "benchmark_price,"
                 + "report_country_code,"
                 + "price,"
                 + "title,"
                 + "brand,"
                 + "category_l1,"
                 + "product_type_l1"
                + " FROM price_competitiveness_product_view"
                + " WHERE date BETWEEN '2023-03-03' AND '2025-03-10'"; */
    /*
    // The query below is an example of a query for the
    price_insights_product_view. let query = "SELECT offer_id,"
                     + "id,"
                     + "suggested_price,"
                     + "price,"
                     + "effectiveness,"
                     + "title,"
                     + "brand,"
                     + "category_l1,"
                     + "product_type_l1,"
                     + "predicted_impressions_change_fraction,"
                     + "predicted_clicks_change_fraction,"
                     + "predicted_conversions_change_fraction"
                    + " FROM price_insights_product_view"; */

    /*
    // The query below is an example of a query for the
    product_performance_view. let query = "SELECT offer_id,"
            + "conversion_value,"
            + "marketing_method,"
            + "customer_country_code,"
            + "title,"
            + "brand,"
            + "category_l1,"
            + "product_type_l1,"
            + "custom_label0,"
            + "clicks,"
            + "impressions,"
            + "click_through_rate,"
            + "conversions,"
            + "conversion_rate"
            + " FROM product_performance_view"
            + " WHERE date BETWEEN '2023-03-03' AND '2025-03-10'"; */

    // Call the Reports.search API method. Use the pageToken to iterate through
    // all pages of results.
    do {
      response =
          MerchantApiReports.Accounts.Reports.search({query, pageSize, pageToken}, parent);
      for (const reportRow of response.results) {
        console.log(reportRow);
      }
      pageToken = response.nextPageToken;
    } while (pageToken);  // Exits when there is no next page token.

  } catch (e) {
    console.log('ERROR!');
    console.log(e);
    console.log('Error message:' + e.message);
    if (e.stack) {
      console.log('Stack trace:' + e.stack);
    }
  }
}


列出所有資料來源

這個範例說明如何列出特定 Merchant Center 帳戶中的所有資料來源。


/**
 * Lists all data sources for a given Merchant Center account.
 */
function listDataSources() {
  // IMPORTANT:
  // Enable the Merchant API DataSources Bundle Advanced Service and call it
  // "MerchantApiDataSources"

  // Replace this with your Merchant Center ID.
  const accountId = '<MERCHANT_CENTER_ID>';

  // Construct the parent name
  const parent = 'accounts/' + accountId;
  let dataSources = [];
  let primaryDataSources = [];
  try {
    console.log('Sending list DataSources request');
    let pageToken;
    let pageSize = 10;
    // Call the DataSources.list API method. Use the pageToken to iterate through
    // all pages of results.
    do {
      response =
          MerchantApiDataSources.Accounts.DataSources.list(parent, {pageSize, pageToken});
      for (const datasource of response.dataSources) {
        dataSources.push(datasource);
        if (datasource.primaryProductDataSource) {
          primaryDataSources.push(datasource);
        }
      }
      pageToken = response.nextPageToken;
    } while (pageToken);  // Exits when there is no next page token.
    console.log('Retrieved ' + dataSources.length + ' data sources.');
    console.log(
        'There were ' + primaryDataSources.length +
        ' primary product data sources.');
  } catch (e) {
    console.log('ERROR!');
    console.log(e);
  }
}