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 サービスを使用する手順は次のとおりです。

  1. Apps Script プロジェクトが標準の Google Cloud プロジェクトにリンクされていることを確認します。

  2. このドキュメントの説明に沿って、Apps Script の拡張サービスを有効にします。

    • 新しいプロジェクトで appsscript.json を有効にします。
    • 既存のプロジェクトで Apps Script を有効にします。
  3. Merchant API クイックスタートの説明に沿って、標準の Google Cloud プロジェクトを Merchant Center アカウントに登録します。

Apps Script の高度なサービスを有効にする

Apps Script サービスは、次のいずれかの方法で有効にできます。

appsscript.json で API を有効にする

次の例は、Products、Accounts、Reports、Data sources の各サブ API を有効にする appsscript.json ファイルを示しています。

  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. サブ API の名前を含む識別子を追加します。たとえば、Inventories サブ API を有効にするには、バージョン inventories_v1beta を選択し、識別子を 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 sub-API 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 sub-API Advanced Service and call it
  // "MerchantApiReports"
  // Enable the Merchant API Products sub-API 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 sub-API 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 sub-API 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);
  }
}