Merchant API-Dienst

Produkte hochladen und Merchant Center-Konten verwalten

Mit dem Merchant API-Dienst können Sie die Merchant API in Google Apps Script verwenden, um Produkte hochzuladen und Merchant Center Konten zu verwalten.

Ausführliche Informationen zur Merchant API finden Sie in der Referenz dokumentation. Wie alle erweiterten Dienste in Apps Script verwendet der Merchant API-Dienst dieselben Objekte, Methoden und Parameter wie die öffentliche API.

Die Merchant API ist eine Sammlung von Sub-APIs – Gruppen verwandter Dienste und Ressourcen. Hier ist die Liste der Sub-APIs.

So verwenden Sie den Merchant API-Dienst in Apps Script:

  1. Achten Sie darauf, dass Ihr Apps Script-Projekt mit einem Standard-Google Cloud-Projekt verknüpft ist. Weitere Informationen finden Sie unter Ein anderes Standard-Cloud-Projekt verwenden.

  2. Aktivieren Sie den erweiterten Apps Script-Dienst, wie in diesem Dokument beschrieben:

    • appsscript.json für neue Projekte aktivieren
    • Apps Script für bestehende Projekte aktivieren
  3. Registrieren Sie Ihr Standard-Google Cloud-Projekt mit Ihrem Merchant Center Konto, wie im Merchant API Schnellstart beschrieben.

Erweiterten Apps Script-Dienst aktivieren

Sie können den Apps Script-Dienst mit einer der folgenden beiden Methoden aktivieren:

APIs in appsscript.json aktivieren

Das folgende Beispiel zeigt eine appsscript.json-Datei, in der die Sub-APIs „Products“, „Accounts“, „Reports“ und „Data sources“ aktiviert sind.

  1. Wählen Sie im Apps Script-Editor die Projekteinstellungen aus.

    Apps Script-Projekteinstellungen mit der Option „Manifestdatei ‚appsscript.json‘ im Editor anzeigen“

  2. Aktivieren Sie die Option Manifestdatei „appsscript.json“ im Editor anzeigen.

  3. Wählen Sie im Editor die Datei appsscript.json aus.

  4. Ersetzen Sie den Inhalt Ihrer appsscript.json-Datei durch Folgendes:

    {
      "dependencies": {
        "enabledAdvancedServices": [
          {
            "userSymbol": "MerchantApiAccounts",
            "version": "accounts_v1",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiDataSources",
            "version": "datasources_v1",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiProducts",
            "version": "products_v1",
            "serviceId": "merchantapi"
          },
          {
            "userSymbol": "MerchantApiReports",
            "version": "reports_v1",
            "serviceId": "merchantapi"
          }
        ]
      },
      "exceptionLogging": "STACKDRIVER",
      "runtimeVersion": "V8"
    }
    
  5. Klicken Sie auf Speichern.

  6. Sie können jetzt in Ihrem Code auf die folgenden Sub-APIs verweisen:

    a. MerchantApiAccounts

    b. MerchantApiDataSources

    c. MerchantApiProducts

    d. MerchantApiReports

Apps Script für zusätzliche Sub-APIs oder bestehende Projekte aktivieren

So aktivieren Sie Sub-APIs in bestehenden Projekten:

  1. Öffnen Sie Ihr Apps Script-Projekt.

  2. Klicken Sie links auf Editor < >.

  3. Klicken Sie links neben Dienste auf Dienst hinzufügen +.

  4. Wählen Sie im Versionsselektor die Sub-API aus, die Sie aktivieren möchten.

  5. Hängen Sie den Namen Ihrer Sub-API an die Kennung an. Wenn Sie beispielsweise die Sub-API „Inventories“ aktivieren möchten, wählen Sie die Version inventories_v1 aus und ändern Sie die Kennung in MerchantApiInventories.

  6. Sie können jetzt in Ihrem Code auf die Sub-API „Inventories“ als MerchantApiInventories verweisen.

Beispielcode

In diesem Abschnitt wird erläutert, wie Sie die Merchant API für ausgewählte Funktionen verwenden.

Produkte auflisten

In diesem Beispiel wird gezeigt, wie Sie die Produkte für ein bestimmtes Merchant Center-Konto auflisten.


/**
 * 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);
  }
}

Abgelehnte Produkte filtern

In diesem Beispiel wird gezeigt, wie Sie abgelehnte Produkte in einem Merchant Center-Konto filtern.


/**
 * 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);
  }
}

Bericht für ein bestimmtes Konto abrufen

In diesem Beispiel wird gezeigt, wie Sie einen Bericht für ein bestimmtes Merchant Center-Konto abrufen.


/**
 * 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);
    }
  }
}


Alle Datenquellen auflisten

In diesem Beispiel wird gezeigt, wie Sie alle Datenquellen in einem bestimmten Merchant Center-Konto auflisten.


/**
 * 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);
  }
}