Merchant API सेवा की मदद से, Apps Script में Merchant API का इस्तेमाल करके, प्रॉडक्ट अपलोड किए जा सकते हैं और Merchant Center खाते मैनेज किए जा सकते हैं.
Merchant API के बारे में ज़्यादा जानकारी के लिए, रेफ़रंस दस्तावेज़ देखें. Merchant API सेवा, Apps Script की सभी ऐडवांस सेवाओं की तरह ही, सार्वजनिक एपीआई के जैसे ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है.
Merchant API, सब-एपीआई का एक कलेक्शन है. ये सब-एपीआई, मिलती-जुलती सेवाओं और संसाधनों के ग्रुप होते हैं. यहां सब-एपीआई की सूची दी गई है.
Apps Script में Merchant API सेवा का इस्तेमाल करने के लिए, इनमें से किसी एक विकल्प का इस्तेमाल करके, Apps Script की बेहतर सेवाएं चालू करें:
नए प्रोजेक्ट के लिए appsscript.json चालू करें. हमारा सुझाव है कि नए Apps Script प्रोजेक्ट के लिए, यह विकल्प चुनें.
मौजूदा प्रोजेक्ट के लिए Apps Script चालू करना. मौजूदा प्रोजेक्ट में अन्य सब-एपीआई चालू करने के लिए, इस तरीके का इस्तेमाल करें.
appsscript.json
में एपीआई चालू करना
नीचे दिए गए उदाहरण में एक appsscript.json
फ़ाइल दिखाई गई है, जो प्रॉडक्ट, खाते, रिपोर्ट, और डेटा सोर्स सब-एपीआई को चालू करती है.
Apps Script एडिटर में, प्रोजेक्ट की सेटिंग
पर क्लिक करें.एडिटर में "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" }
सेव करें पर क्लिक करें.
अब अपने कोड में, इन सब-एपीआई को इस तरह रेफ़र किया जा सकता है:
a.
MerchantApiAccounts
b.
MerchantApiDataSources
c.
MerchantApiProducts
d.
MerchantApiReports
अन्य सब-एपीआई या मौजूदा प्रोजेक्ट के लिए, Apps Script चालू करना
मौजूदा प्रोजेक्ट में सब-एपीआई चालू करने के लिए, यह तरीका अपनाएं.
अपना Apps Script प्रोजेक्ट खोलें.
बाईं ओर, एडिटर < > पर क्लिक करें.
बाईं ओर, सेवाएं के बगल में मौजूद, सेवा जोड़ें + पर क्लिक करें.
वर्शन सिलेक्टर में जाकर, वह सब-एपीआई चुनें जिसे आपको चालू करना है.
आइडेंटिफ़ायर को अपने सब-एपीआई के नाम के साथ जोड़ें. उदाहरण के लिए, इन्वेंट्री सब-एपीआई को चालू करने के लिए, inventories_v1beta वर्शन चुनें और आइडेंटिफ़ायर को MerchantApiInventories में बदलें.
अब अपने कोड में इन्वेंट्री सब-एपीआई को
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);
}
}