Merchant Data Sources API の概要

このページでは、商品を挿入できるデータソースをプログラムで作成して更新する方法について説明します。自動データソースを使用すると、商品データを Google に簡単に送信できます。自動化されたデータソースを使用すると、ウェブサイト上の関連商品に関する最新情報が Google に確実に届きます。

Content API for Shopping では、プライマリ データソースのみを作成できます。Merchant Data sources API を使用すると、次のタイプのデータソースを作成できます。

Content API for Shopping では、ファイル入力でのみデータソースを管理できます。Merchant API を使用すると、ファイルと API の両方の入力を使用してデータソースを管理できます。

Merchant Data sources API を使用すると、次のことができます。

  • 特定の feedLabelcontentLanguage を使用して新しいメイン データソースを作成します。
  • feedLabel フィールドと contentLanguage フィールドが設定されていないデータソースを作成します。このタイプのデータソースを使用すると、feedLabelcontentLanguage の組み合わせが異なる商品を 1 つのデータソースに挿入できるため、商品の対象国を複数に設定できます。
  • 既存のプライマリ データソースにリンクする補助データソースを作成します。
  • ファイル データソースのスケジュールを設定する。
  • データソースの自動管理のためにアカウントを登録します。
  • API データソースを管理する。
  • プライマリ商品データソースを使用して、データソースのデフォルト ルールを管理します。
  • プロモーションなど、他の種類のデータソースを使用する。

Merchant API を使用して、ローカル商品とオンライン商品の両方を含むデータソースに商品を挿入することはできません。データソース チャンネルの詳細については、チャンネルをご覧ください。

前提条件

  • アカウントが 単一言語 / 地域フィードに移行されていること。
  • アカウントがデータ ターゲット分割にすでに移行されていることを確認するには、データソースのリストまたは取得メソッドを使用します。対象外の場合は、次の例外メッセージが表示され、サポートにお問い合わせいただく必要があります。

    This account is in the data sources migration process and can't be used with
    this API yet. Contact support for more info on when this account will be able
    to use the data sources endpoint.
    

新しいデータソースを作成する

メイン データソースは、Merchant Center の在庫に関するメインのデータソースです。商品を追加または削除できるのは、メイン データソースを使用した場合のみです。メインのデータソースに追加する商品がすべて Merchant Center のデータ要件と利用資格要件を満たしている場合、データソースを追加作成する必要はありません。

特定の feedLabelcontentLanguage を使用して新しいプライマリ データソースを作成するには、タイプ固有の構成で feedLabel フィールドと contentLanguage フィールドを設定します。これらのフィールドの詳細については、PrimaryProductDataSource をご覧ください。

次のサンプル リクエストは、プライマリ プロダクト データソースを作成する方法を示しています。

POST https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources

{
  "displayName": "{DISPLAY_NAME}",
  "primaryProductDataSource": {
    "contentLanguage": "{CONTENT_LANGUAGE}",
    "feedLabel": "{FEED_LABEL}",
    "countries": [
      "{COUNTRY}"
    ],
    "channel": "ONLINE_PRODUCTS"
  }
}

次のように置き換えます。

  • {ACCOUNT_ID}: Merchant Center アカウントの一意の識別子。
  • {DISPLAY_NAME}: データソースの表示名。
  • {CONTENT_LANGUAGE}: データソースの商品の 2 文字の ISO 639-1 言語コード。
  • {FEED_LABEL}: データソースのフィードラベル。
  • {COUNTRY}: データソースを使用してアップロードされる商品のターゲット国を表す CLDR 地域コード。

リクエストが正常に実行されると、次のレスポンスが表示されます。

{
  "name": "accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}",
  "dataSourceId": "{DATASOURCE_ID}",
  "displayName": "{DISPLAY_NAME}",
  "primaryProductDataSource": {
    "channel": "ONLINE_PRODUCTS",
    "feedLabel": "{FEED_LABEL}",
    "contentLanguage": "{CONTENT_LANGUAGE}",
    "countries": [
      "{COUNTRY}"
    ],
    "defaultRule": {
      "takeFromDataSources": [
        {
          "self": true
        }
      ]
    }
  },
  "input": "API"
}

データソースの作成の詳細については、accounts.dataSources.create メソッドをご覧ください。

新しく作成したデータソースを表示するには、accounts.dataSources.get メソッドまたは accounts.dataSources.list メソッドを使用します。

次のサンプルは、GBenfeedLabelcontentLanguage の組み合わせのプライマリ プロダクト データソースを作成する方法を示しています。

Java

     public static String createDataSource(Config config, String displayName) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    // The type of data that this datasource will receive.
    PrimaryProductDataSource primaryProductDataSource =
        PrimaryProductDataSource.newBuilder()
            // Channel can be "ONLINE_PRODUCTS" or "LOCAL_PRODUCTS" or "PRODUCTS" .
            // While accepted, datasources with channel "products" representing unified products
            // currently cannot be used with the Products bundle.
            .setChannel(PrimaryProductDataSource.Channel.ONLINE_PRODUCTS)
            .addCountries("GB")
            .build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setPrimaryProductDataSource(primaryProductDataSource)
                      .build())
              .build();

      System.out.println("Sending Create PrimaryProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      // Null is necessary to satisfy the compiler as we're not returning a String on failure.
      return null;
    }
  }

複数の国をターゲットにする新しいメイン データソースを作成する

複数の国をターゲットとする新しいメインフィードを作成するには、PrimaryProductDataSource を使用してデータソースを構成し、feedLabel フィールドと contentLanguage フィールドを設定しないでください。

Content API for Shopping を使用すると、作成される API データソースは 1 つだけです。Merchant Data sources API を使用すると、複数の API データソースを設定できます。一部のデータソースでは、feedLabel フィールドと contentLanguage フィールドを設定しないこともできます。

feedLabel フィールドと contentLanguage フィールドを設定しないデータソースは、API 入力を使用するデータソースに限られます。このタイプのデータソースは、ファイル入力ではサポートされていません。

補助データソースを作成してメイン データソースにリンクする

補助データソースは、1 つ以上のメイン データソースにすでに存在している商品データを更新することのみに使用されます。補助データソースは複数作成することができ、1 つの補助データソースで複数のメインデータソースのデータを追加できます。

補助データソースを使用して商品データを部分的に更新するには、accounts.productInputs.insert メソッドと accounts.productInputs.delete メソッドを呼び出すときに、データソースの一意の識別子をクエリ パラメータとして追加します。補助データソースは、既存の商品を更新する場合にのみ使用できます。

補助データソースを作成するには、SupplementalProductDataSource を使用してデータソースを構成し、メイン データソースの defaultRule フィールドを更新してリンクします。

補足ファイルのデータソースには、feedLabel フィールドと contentLanguage フィールドを設定する必要があります。補足 API データソースでは、feedLabel フィールドと contentLanguage フィールドは常に未設定にする必要があります。

次のサンプルは、enGB contentLanguagefeedLabel の組み合わせのファイル補足プロダクト データソースを作成する方法を示しています。

Java

     private static FileInput setFileInput() {
    // If FetchSettings are not set, then this will be an `UPLOAD` file type
    // that you must manually upload via the Merchant Center UI.
    return FileInput.newBuilder()
        // FileName is required for `UPLOAD` fileInput type.
        .setFileName("British T-shirts Supplemental Data")
        .build();
  }

  public static String createDataSource(Config config, String displayName, FileInput fileInput)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setSupplementalProductDataSource(
                          SupplementalProductDataSource.newBuilder()
                              .setContentLanguage("en")
                              .setFeedLabel("GB")
                              .build())
                      .setFileInput(fileInput)
                      .build())
              .build();

      System.out.println("Sending create SupplementalProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      // Null is necessary to satisfy the compiler as we're not returning a String on failure.
      return null;
    }
  }

すべての feedLabelcontentLanguage の組み合わせで機能する補助データソースを作成するには、次のサンプルを実行します。

Java

     public static String createDataSource(Config config, String displayName) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setSupplementalProductDataSource(
                          SupplementalProductDataSource.newBuilder().build())
                      .build())
              .build();

      System.out.println("Sending create SupplementalProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning a
      // String on failure.
    }
  }

ファイル データソースのスケジュールを設定する

ファイルフィードのスケジュールを設定するには、FileInput フィールドを使用してデータソースをファイルデータソースとして構成し、FileInput.FetchSettings フィールドを使用して fetchsettings を設定します。

データソースを削除する

アカウントから既存のデータソースを削除するには、accounts.dataSources.delete メソッドを使用します。

次のサンプルは、DeleteDataSourceRequest パッケージを使用してデータソースを削除する方法を示しています。

Java

     public static void deleteDataSource(Config config, String dataSourceId) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
      DeleteDataSourceRequest request = DeleteDataSourceRequest.newBuilder().setName(name).build();

      System.out.println("Sending deleteDataSource request");
      // Delete works for any datasource type.
      // If Type "Supplemental", delete will only work if it's not linked to any primary feed.
      // If a link exists and the Type is "Supplemental", you will need to remove the supplemental
      // feed from the default and/or custom rule(s) of any primary feed(s) that references it. Then
      // retry the delete.

      dataSourcesServiceClient.deleteDataSource(request); // No response returned on success.
      System.out.println(
          "Delete successful, note that it may take a few minutes for the delete to update in"
              + " the system.");
    } catch (Exception e) {
      System.out.println(e);
    }
  }

データソースを取得する

データソースで構成されたファイルを取得するには、accounts.dataSources.fetch メソッドを使用します。この方法では、アカウントのデータソースでデータの取得が直ちに実行されます。この方法は、ファイル入力が設定されているデータソースでのみ機能します。

データソースの取得

アカウントのデータソース構成を取得するには、accounts.dataSources.get メソッドを使用します。

次のサンプルは、GetDataSourceRequest パッケージを使用して、特定の Merchant Center アカウントの特定のデータソースを取得する方法を示しています。

Java

     public static DataSource getDataSource(Config config, String dataSourceId) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates datasource name to identify datasource.
    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    // Calls the API and catches and prints any network failures/errors.
    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      // The name has the format: accounts/{account}/datasources/{datasource}
      GetDataSourceRequest request = GetDataSourceRequest.newBuilder().setName(name).build();

      System.out.println("Sending GET DataSource request:");
      DataSource response = dataSourcesServiceClient.getDataSource(request);

      System.out.println("Retrieved DataSource below");
      System.out.println(response);
      return response;
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning a
      // DataSource on failure.
    }
  }

データソースの一覧表示

アカウントのデータソースの構成を一覧表示するには、accounts.dataSources.list メソッドを使用します。

次のサンプルは、ListDataSourceRequest パッケージを使用して、特定の Merchant Center アカウントのすべてのデータソースを一覧表示する方法を示しています。

Java

     public static ArrayList<DataSource> listDataSources(Config config) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates parent to identify the account from which to list all the datasources.
    String parent = getParent(config.getAccountId().toString());

    // Calls the API and catches and prints any network failures/errors.
    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      // The parent has the format: accounts/{account}
      ListDataSourcesRequest request =
          ListDataSourcesRequest.newBuilder().setParent(parent).build();

      System.out.println("Sending list datasources request:");
      ListDataSourcesPagedResponse response = dataSourcesServiceClient.listDataSources(request);

      int count = 0;
      ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
      ArrayList<DataSource> justPrimaryDataSources = new ArrayList<DataSource>();

      // Iterates over all rows in all pages and prints the datasource in each row.
      // Automatically uses the `nextPageToken` if returned to fetch all pages of data.
      for (DataSource element : response.iterateAll()) {
        System.out.println(element);
        count++;
        dataSources.add(element);
        // The below lines show how to filter datasources based on type.
        // `element.hasSupplementalProductDataSource()` would give you supplemental
        // datasources, etc.
        if (element.hasPrimaryProductDataSource()) {
          justPrimaryDataSources.add(element);
        }
      }
      System.out.print("The following count of elements were returned: ");
      System.out.println(count);
      return dataSources;
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning an
      // ArrayList<DataSource> on failure.
    }
  }

パッチ データソース

既存のデータソースの構成を更新するには、accounts.dataSources.patch メソッドを使用します。

次のサンプルは、UpdateDataSourceRequest パッケージを使用してデータソースを更新する方法を示しています。また、メイン データソースを更新して、補助データソースをデフォルトのルールに追加する方法についても説明します。

Java

     public static String updateDataSource(Config config, String displayName, String dataSourceId)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates datasource name to identify datasource.
    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    DataSource dataSource =
        DataSource.newBuilder()
            // Update the datasource to have the new display name
            .setDisplayName(displayName)
            .setName(name)
            .build();

    FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      UpdateDataSourceRequest request =
          UpdateDataSourceRequest.newBuilder()
              .setDataSource(dataSource)
              .setUpdateMask(fieldMask)
              .build();

      System.out.println("Sending Update DataSource request");
      DataSource response = dataSourcesServiceClient.updateDataSource(request);
      System.out.println("Updated DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null;
    }
  }

  public String updateDataSource(
      Config config,
      String primaryDataSourceName,
      String firstSupplementalDataSourceName,
      String secondSupplementalDataSourceName)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Setting self to 'true' refers to the primary datasource itself.
    DataSourceReference dataSourceReferenceSelf =
        DataSourceReference.newBuilder().setSelf(true).build();
    DataSourceReference firstSupplementalDataSourceReference =
        DataSourceReference.newBuilder()
            .setSupplementalDataSourceName(firstSupplementalDataSourceName)
            .build();
    DataSourceReference secondSupplementalDataSourceReference =
        DataSourceReference.newBuilder()
            .setSupplementalDataSourceName(secondSupplementalDataSourceName)
            .build();

    // The attributes will first be taken from the primary DataSource.
    // Then the first supplemental DataSource if the attribute is not in the primary DataSource
    // And finally the second supplemental DataSource if not in the first two DataSources.
    // Note that CustomRules could change the behavior of how updates are applied.
    DefaultRule defaultRule =
        DefaultRule.newBuilder()
            .addTakeFromDataSources(dataSourceReferenceSelf)
            .addTakeFromDataSources(firstSupplementalDataSourceReference)
            .addTakeFromDataSources(secondSupplementalDataSourceReference)
            .build();

    // The type of data that this datasource will receive.
    PrimaryProductDataSource primaryProductDataSource =
        PrimaryProductDataSource.newBuilder().setDefaultRule(defaultRule).build();

    DataSource dataSource =
        DataSource.newBuilder()
            // Update the primary datasource to have the default rule datasources in the correct
            // order.
            .setPrimaryProductDataSource(primaryProductDataSource)
            .setName(primaryDataSourceName)
            .build();

    // The '.' signifies a nested field.
    FieldMask fieldMask =
        FieldMask.newBuilder().addPaths("primary_product_data_source.default_rule").build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      UpdateDataSourceRequest request =
          UpdateDataSourceRequest.newBuilder()
              .setDataSource(dataSource)
              .setUpdateMask(fieldMask)
              .build();

      System.out.println("Sending Update DataSource request");
      DataSource response = dataSourcesServiceClient.updateDataSource(request);
      System.out.println("Updated DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null;
    }
  }

メインの商品データソースでは、データソースのデフォルトのルールを管理できます。デフォルトのルールは、データソース内のすべての属性に適用されるルールです。デフォルトのルールは、データソースの作成時に設定するか、デフォルトのルール フィールドを使用して既存のデータソースを更新することで設定できます。

ルールの設定の詳細については、商品データソースのルールを設定するをご覧ください。

次の構成例では、一意の識別子 1001 を持つデータソースから、すべての属性が最初に取得されます。次に、不足している属性がプライマリ データソースから追加されます。他のどのデータソースでも指定されていない場合、残りの属性は、一意の識別子 1002 を持つ補助データソースから取得されます。複数のデータソースで同じ属性が指定されている場合、リストの上位の値が選択されます。

defaultRule {
 takeFromDataSources: [
   '1001', // Supplemental product data source
   'self', //  Self reference to the primary data source
   '1002' // Supplemental product data source
 ]
}

フィードの自動管理

データソースの自動管理にアカウントを登録するには、次の操作を行う必要があります。

アカウントが登録の対象となったら、accounts.autofeedSettings.updateAutofeedSettings メソッドを使用してデータソースの自動管理を有効にできます。データソースの自動管理を有効にすると、Google がオンライン ショップから商品を自動的に追加し、Google のプラットフォームで常に最新の状態に保つことができます。

ファイルのアップロード ステータスを取得する

ファイル、フェッチ、スプレッドシートを使用してデータソースのステータスを取得するには、accounts.dataSources.fileUploads サービスの GET メソッドを呼び出します。データソースの処理が完了したときに非同期で計算されたデータソースの最後の取得結果を取得するには、名前 ID latest を使用します。

GET https://merchantapi.googleapis.com/accounts/v1beta/{ACCOUNT_ID}/datasources/{DATASOURCE_ID}/fileUploads/latest

ファイルのアップロード ステータスには、潜在的な問題など、商品の詳細が表示される場合があります。

ファイルがアップロードされていない場合、ファイルのアップロード ステータスは存在しない場合があります。ファイルのアップロード直後にリクエストした場合、ファイルのアップロード ステータスは処理中になることがあります。