上传商品和管理 Merchant Center 账号。
借助 Merchant API 服务,您可以在 Google Apps 脚本中使用 Merchant API 来上传商品和管理 Merchant Center 账号。
如需详细了解 Merchant API,请参阅参考文档。与 Apps 脚本中的所有高级服务一样,Merchant API 服务使用的对象、方法和参数均与公共 API 相同。
Merchant API 是 sub-API(相关服务和资源的群组)的集合。以下是 sub-API 的列表。
如需在 Apps 脚本中使用 Merchant API 服务,请按以下步骤操作:
确保您的 Apps 脚本项目已关联到标准 Google Cloud 项目。如需了解相关信息,请参阅使用其他标准 Cloud 项目。
启用 Apps 脚本高级服务,如本文档中所述:
- 为新项目启用
appsscript.json。 - 为现有项目启用 Apps 脚本。
- 为新项目启用
按照 Merchant API 快速入门指南中所述,将您的标准 Google Cloud 项目注册到 Merchant Center 账号。
启用 Apps 脚本高级服务
您可以使用以下两种方法中的任一种来启用 Apps 脚本服务:
在 appsscript.json 中启用 API
以下示例展示了一个 appsscript.json 文件,该文件可启用 Products、Accounts、Reports 和 Data sources 子 API。
在 Apps 脚本编辑器中,选择项目设置图标 。

启用在编辑器中显示“appsscript.json”清单文件选项。
在编辑器中,选择
appsscript.json文件。将
appsscript.json文件的内容替换为以下内容:{ "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" }点击保存。
现在,您可以在代码中按如下方式引用以下子 API:
a.
MerchantApiAccountsb.
MerchantApiDataSourcesc.
MerchantApiProductsd.
MerchantApiReports
为其他子 API 或现有项目启用 Apps 脚本
如需在现有项目中启用子 API,请执行以下操作:
打开您的 Apps 脚本项目。
点击左侧的编辑器 < >。
在左侧,点击服务旁边的添加服务 +。
在版本选择器中,选择要启用的子 API。
附加子 API 的名称标识符。例如,如需启用 Inventories 子 API,请选择版本
inventories_v1并将标识符更改为MerchantApiInventories。
现在,您可以在代码中将 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);
}
}