自動資料來源可讓你輕鬆將產品資料傳送給 Google。這些資料來源可確保 Google 取得網站上相關產品的最新資訊。
本頁說明如何透過程式建立及更新資料來源,以便插入產品。
你可以使用 Content API for Shopping 建立主要資料來源。您也可以使用 Merchant Data sources API 建立下列類型的資料來源:
你只能透過檔案輸入來管理 Content API for Shopping 的資料來源。Merchant API 可讓你透過檔案和 API 輸入內容管理資料來源。
如要進一步瞭解這項功能與 Content API for Shopping 的差異,請參閱「遷移資料來源管理」一文。
您可以使用 Merchant Data sources API 執行下列操作:
- 使用特定
feedLabel
和contentLanguage
建立主要資料來源。 - 建立未設定
feedLabel
和contentLanguage
欄位的資料來源。使用這類資料來源,您可以為產品指定多個國家/地區,因為您可以在單一資料來源中插入具有不同feedLabel
和contentLanguage
組合的產品。 - 建立補充資料來源,以連結至現有的主要資料來源。
- 設定檔案資料來源的時間表。
- 註冊帳戶,自動管理資料來源。
- 管理 API 資料來源。
- 使用主要產品資料來源管理資料來源的預設規則。
- 為產品資料來源自訂
destinations
(也稱為行銷方式)。 - 使用促銷活動等其他類型的資料來源。
如要進一步瞭解資料來源管道,請參閱「管道」。
建立資料來源
主要資料來源是 Merchant Center 商品目錄的主要資料來源。你只能使用主要資料來源新增或移除產品。如果你在主要資料來源中新增的每項產品都符合 Merchant Center 的資料和資格規定,則無須額外建立其他資料來源。
如要建立含有特定 feedLabel
和 contentLanguage
的主要資料來源,請在類型專屬設定中設定 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 帳戶的專屬 ID。
- {DISPLAY_NAME}:資料來源的顯示名稱。
- {CONTENT_LANGUAGE}:資料來源中產品的雙字母 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}"
],
"destinations": [
{
"id": "SHOPPING_ADS",
"state": "ENABLED"
},
{
"id": "FREE_LISTINGS",
"state": "ENABLED"
}
]
"defaultRule": {
"takeFromDataSources": [
{
"self": true
}
]
}
},
"input": "API"
}
由於資料來源已啟用預設目的地,因此 destinations
欄位並非空白。
如要進一步瞭解如何建立資料來源,請參閱 accounts.dataSources.create 方法。
如要查看新建立的資料來源,請使用 accounts.dataSources.get 或 accounts.dataSources.list 方法。
以下範例說明如何為 GB
和 en
feedLabel
和 contentLanguage
組合建立主要產品資料來源。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a primary product datasource for all `feedLabel` and
* `contentLanguage` combinations. Note that rules functionality is limited for wildcard feeds.
*/
public class CreatePrimaryProductDataSourceWildCardSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
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;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "Primary Product Data Wildcard";
createDataSource(config, displayName);
}
}
建立新的主要資料來源,以便指定多個國家/地區
如要建立新的主要動態饋給,以便指定多個國家/地區,請使用 PrimaryProductDataSource
設定資料來源,且不要設定 feedLabel
和 contentLanguage
欄位。
使用 Content API for Shopping 時,系統只會為你建立一個 API 資料來源。使用 Merchant Data sources API 時,你可以擁有多個 API 資料來源,其中部分資料來源可能未設定 feedLabel
和 contentLanguage
欄位。
只有含有 API 輸入內容的資料來源可以不設定 feedLabel
和 contentLanguage
欄位。檔案輸入不支援這類資料來源。
建立補充資料來源,並將其連結至主要資料來源
補充資料來源只能用於更新一或多個主要資料來源中的既有產品資料。你可以建立多個補充資料來源,而每個補充資料來源可以為任意數量的主要資料來源補充資料。
您可以使用補充資料來源,在呼叫 accounts.productInputs.insert
和 accounts.productInputs.delete
方法時,將資料來源的專屬 ID 新增為查詢參數,藉此對產品資料進行部分更新。你只能使用補充資料來源更新現有產品。
如要建立補充資料來源,請使用 SupplementalProductDataSource
設定資料來源,然後更新主要資料來源的 defaultRule
欄位,藉此建立連結。
補充檔案資料來源必須設定 feedLabel
和 contentLanguage
欄位。補充 API 資料來源必須一律未設定 feedLabel
和 contentLanguage
欄位。
以下範例說明如何為 en
和 GB
contentLanguage
和 feedLabel
組合建立檔案輔助產品資料來源。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.FileInput;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a File Supplemental product datasource for the "en" and
* "GB" `feedLabel` and `contentLanguage` combination. This supplemental feed is eligible to be
* linked to both a wildcard primary feed and/or a primary feed with the same `feedLabel` and
* `contentLanguage` combination.
*/
public class CreateFileSupplementalProductDataSourceSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
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;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "British File Supplemental Product Data";
// The file input data that this datasource will receive.
FileInput fileInput = setFileInput();
createDataSource(config, displayName, fileInput);
}
}
如要建立適用於所有 feedLabel
和 contentLanguage
組合的補充資料來源,請執行下列範例。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a Supplemental product datasource all `feedLabel` and
* `contentLanguage` combinations. This works only for API supplemental feeds.
*/
public class CreateSupplementalProductDataSourceWildCardSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
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.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "Supplemental API Product Data Wildcard";
createDataSource(config, displayName);
}
}
為檔案資料來源設定時間表
如要為檔案動態饋給設定時間表,請使用 FileInput
欄位將資料來源設為檔案資料來源,然後使用 FileInput.FetchSettings
欄位設定 fetchsettings
。
刪除資料來源
如要從帳戶中刪除現有資料來源,請使用 accounts.dataSources.delete
方法。
下列範例示範如何使用 DeleteDataSourceRequest
套件刪除資料來源。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.DeleteDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a datasource. */
public class DeleteDataSourceSample {
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);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID automatically assigned to the datasource after creation by Google.
String dataSourceId = "1111111111"; // Replace with your datasource ID.
deleteDataSource(config, dataSourceId);
}
}
擷取資料來源
如要擷取在資料來源中設定的檔案,請使用 accounts.dataSources.fetch
方法。這個方法會立即在帳戶的資料來源上執行資料擷取作業。這個方法僅適用於含有檔案輸入集的資料來源。
取得資料來源
如要擷取帳戶的資料來源設定,請使用 accounts.dataSources.get
方法。
以下範例示範如何使用 GetDataSourceRequest
套件,擷取特定 Merchant Center 帳戶的特定資料來源。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.GetDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get a specific datasource for a given Merchant Center account. */
public class GetDataSourceSample {
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.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID assigned to a datasource by Google.
String datasourceId = "1111111111"; // Replace with your datasource ID.
getDataSource(config, datasourceId);
}
}
可列出資料來源
如要列出帳戶的資料來源設定,請使用 accounts.dataSources.list
方法。
以下範例說明如何使用 ListDataSourceRequest
套件,列出特定 Merchant Center 帳戶的所有資料來源。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient.ListDataSourcesPagedResponse;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.ListDataSourcesRequest;
import java.util.ArrayList;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to list all the datasources for a given Merchant Center account */
public class ListDataSourcesSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
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.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listDataSources(config);
}
}
修補資料來源
如要更新現有資料來源的設定,請使用 accounts.dataSources.patch
方法。
以下範例示範如何使用 UpdateDataSourceRequest
套件更新資料來源。並說明如何更新主要資料來源,將補充資料來源新增至預設規則。
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourceReference;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource.DefaultRule;
import com.google.shopping.merchant.datasources.v1beta.UpdateDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to update a datasource to change its name in the MC UI. It also
* demonstrates how to update a primary datasource to add supplemental datasources to its default
* rule (https://support.google.com/merchants/answer/7450276).
*/
public class UpdateDataSourceSample {
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;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The updated displayed datasource name in the Merchant Center UI.
String displayName = "Great Britain Primary Product Data";
// The ID of the datasource to update
String dataSourceId = "11111111"; // Replace with your datasource ID.
updateDataSource(config, displayName, dataSourceId);
}
}
連結資料來源
主要產品資料來源可讓你管理資料來源的預設規則。預設規則是指套用至資料來源中所有屬性的規則。您可以在建立資料來源時設定預設規則,也可以透過預設規則欄位更新現有資料來源。
如要進一步瞭解如何設定規則,請參閱「為產品資料來源設定規則」。
以下範例設定可確保系統會先從專屬 ID 1001
的資料來源擷取所有屬性。接著,系統會從主要資料來源新增缺少的屬性。最後,如果其他資料來源未提供,則會從附加資料來源取得剩餘的屬性,並使用唯一 ID 1002
。如果有多個資料來源提供相同的屬性,系統會選取清單中優先順序較高的值。
defaultRule {
takeFromDataSources: [
'1001', // Supplemental product data source
'self', // Self reference to the primary data source
'1002' // Supplemental product data source
]
}
動態饋給的自動管理
如要讓帳戶自動管理資料來源,您必須完成下列步驟:
- 請呼叫
accounts.autofeedSettings.getAutofeedSettings
方法,確認您的帳戶是否符合註冊資格。 - 確認你的帳戶不是市集帳戶。
帳戶符合註冊資格後,您可以使用 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
檔案上傳狀態可能會顯示產品的詳細資料,包括任何潛在問題。
請注意,如果檔案從未上傳,檔案上傳狀態可能不存在。如果在檔案上傳後立即要求,檔案上傳狀態可能會處於處理狀態。