บริการ Merchant API ช่วยให้คุณใช้ Merchant API ใน Apps Script เพื่ออัปโหลดผลิตภัณฑ์และจัดการบัญชี Merchant Center ได้
ดูข้อมูลโดยละเอียดเกี่ยวกับ Merchant API ได้ที่เอกสารอ้างอิง เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ Merchant API จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ
Merchant API คือชุดของ API ย่อย ซึ่งเป็นกลุ่มของบริการและทรัพยากรที่เกี่ยวข้อง รายการ API ย่อยมีดังนี้
หากต้องการใช้บริการ Merchant API ใน Apps Script ให้เปิดใช้บริการขั้นสูงของ Apps Script โดยใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้
เปิดใช้ appsscript.json สำหรับโปรเจ็กต์ใหม่ เราขอแนะนำให้ใช้ตัวเลือกนี้สำหรับโปรเจ็กต์ Apps Script ใหม่
เปิดใช้ Apps Script สำหรับโปรเจ็กต์ที่มีอยู่ ใช้วิธีนี้เพื่อเปิดใช้ API ย่อยเพิ่มเติมในโปรเจ็กต์ที่มีอยู่
เปิดใช้ API ใน appsscript.json
ตัวอย่างต่อไปนี้แสดงappsscript.json
ไฟล์ที่เปิดใช้ API ย่อยของผลิตภัณฑ์ บัญชี รายงาน และแหล่งข้อมูล
ในเครื่องมือแก้ไข Apps Script ให้คลิกการตั้งค่า โปรเจ็กต์
เปิดใช้ตัวเลือกแสดงไฟล์ Manifest "appsscript.json" ในเครื่องมือแก้ไข
เลือกไฟล์
appsscript.json
ในโปรแกรมแก้ไขแทนที่เนื้อหาของไฟล์
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" }
คลิกบันทึก
ตอนนี้คุณอ้างอิงถึง API ย่อยต่อไปนี้ภายในโค้ดได้ดังนี้
ก.
MerchantApiAccounts
ข.
MerchantApiDataSources
ค.
MerchantApiProducts
ง.
MerchantApiReports
เปิดใช้ Apps Script สำหรับ API ย่อยเพิ่มเติมหรือโปรเจ็กต์ที่มีอยู่
ทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ API ย่อยในโปรเจ็กต์ที่มีอยู่
เปิดโปรเจ็กต์ Apps Script
คลิกเอดิเตอร์ < > ทางด้านซ้าย
ที่ด้านซ้าย ถัดจากบริการ ให้คลิกเพิ่มบริการ +
เลือก API ย่อยที่ต้องการเปิดใช้ในตัวเลือกเวอร์ชัน
ต่อท้ายตัวระบุด้วยชื่อของ Sub-API เช่น หากต้องการเปิดใช้ Inventories Sub-API ให้เลือกเวอร์ชัน inventories_v1beta แล้วเปลี่ยนตัวระบุเป็น MerchantApiInventories
ตอนนี้คุณสามารถอ้างอิง API ย่อยของพื้นที่โฆษณาภายในโค้ดเป็น
MerchantApiInventories
โค้ดตัวอย่าง
ส่วนนี้จะอธิบายวิธีใช้ Merchant API สำหรับฟีเจอร์ที่เลือก
แสดงรายการผลิตภัณฑ์
ตัวอย่างนี้แสดงวิธีแสดงผลิตภัณฑ์สำหรับบัญชี Merchant Center ที่ระบุ
/**
* Lists all products for a given Merchant Center account.
*/
function productList() {
// IMPORTANT:
// Enable the Merchant API Products Bundle 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 Bundle Advanced Service and call it
// "MerchantApiReports"
// Enable the Merchant API Products Bundle 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 Bundle 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 Bundle 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);
}
}