Can't get the GMC Bestseller Stock checker script to send email alerts

44 views
Skip to first unread message

Nenad Blagojevic

unread,
Jun 20, 2025, 6:17:59 AMJun 20
to Google Ads Scripts Forum
Hi team,

I've been trying to get this script to send email alerts, logs on SKU stock are being returned in script, but it won't send the email, can you please take a look?

Thanks,
Nenad

* Description:
* This Google Ads script checks selected products across one or more Google Merchant Center accounts.
* It flags products that are either:
* - Not approved for the "Shopping" destination
* - Marked as not in stock
*
* When issues are found, the script sends an email alert.
*
* Instructions:
* - Can be run from an MCC Google Ads account or an individual account
* - Shopping Content API under Advanced APIs must be enabled
* - Recommended frequency: Hourly
*/

var EMAIL = "v-nenad.b...@shoplc.com";
var SUBJECT = "[Script Alert] New CX Bestsellers not eligible for Shopping";

function main() {
var merchants = [ // This example checks two Merchant Center accounts. You can remove the second one or add more as needed.
{
merchantId: "2886886", // Replace with your Merchant Center ID
productIds: [
"online:en:US:3335270",
"online:en:US:7968282",
"online:en:US:7894845",
"online:en:US:7700710",
"online:en:US:4267254",
"online:en:US:7016935",
"online:en:US:7688041",
"online:en:US:8018909",
"online:en:US:8104792",
"online:en:US:7016934",
"online:en:US:8898901",
"online:en:US:4262789",
"online:en:US:7752424",
"online:en:US:7687299",
"online:en:US:7850472",
"online:en:US:7863552",
"online:en:US:7984790",
"online:en:US:8041084",
"online:en:US:7504047",
]
},
];

for (var i = 0; i < merchants.length; i++) {
var merchant = merchants[i];
checkStatus(merchant.merchantId, merchant.productIds);
}
}

function checkStatus(merchantId, productIds) {
var badStatus = [];

for (var i = 0; i < productIds.length; i++) {
var productId = productIds[i];

try {
var status = ShoppingContent.Productstatuses.get(merchantId, productId);
var product = ShoppingContent.Products.get(merchantId, productId);
var availability = product.availability || "Unknown";
var title = status.title || "Unknown title";
var link = status.link || "No link available";
var destinationStatuses = status.destinationStatuses || [];
var itemIssues = status.itemLevelIssues || [];
var disapproved = false;

// Extract country code from product ID
var countryCode = productId.split(":")[3];

if (availability.toLowerCase() !== "in stock") {
Logger.log("Product " + productId + " is out of stock.");
badStatus.push({
id: productId,
title: title,
link: link,
reason: "<strong style='color:red'>" + availability + "</strong>"
});
} else {
Logger.log("Product " + productId + " is in stock.");
}


for (var j = 0; j < destinationStatuses.length; j++) {
//Logger.log(destinationStatuses);
var entry = destinationStatuses[j];
if (entry.destination === 'Shopping') {
if (entry.hasOwnProperty('disapprovedCountries')) {
disapproved = true;
break;
} else {
continue;
}
}
}

if (disapproved) {
Logger.log("Product " + productId + " is NOT APPROVED in Shopping.");
var reasons = [];
var seen = {};

for (var k = 0; k < itemIssues.length; k++) {
var reason = itemIssues[k].description || "Unknown reason";
var doc = itemIssues[k].documentation || "";
if (!seen[reason]) {
seen[reason] = true;
reasons.push("<strong style='color:red'>" + reason + "</strong>" + (doc ? " <a href='" + doc + "'>(documentation)</a>" : ""));
}
}

badStatus.push({
id: productId,
title: title,
link: link,
reason: reasons.length > 0 ? reasons.join(", ") : "Unknown reason"
});
} else {
Logger.log("Product " + productId + " is approved or pending in Shopping.");
}
} catch (e) {
Logger.log("Error retrieving product " + productId + ": " + e.message);
}
}

if (badStatus.length > 0) {
var website = ShoppingContent.Accounts.get(merchantId, merchantId).websiteUrl || "Unknown website";
sendEmail(badStatus, website);
}
}

function sendEmail(products, websiteUrl) {
var output = products.map(function(p) {
return "<p><strong>ID:</strong> " + p.id +
"<br><strong>Title:</strong> " + p.title +
"<br><strong>Link:</strong> <a href='" + p.link + "'>" + p.link + "</a>" +
"<br><strong>Reason(s):</strong> " + p.reason +
"</p>";
}).join("<hr>");

var html = [
"<html>",
"<body>",
"<p>" + SUBJECT + "</p>",
"<p>Merchant website: " + websiteUrl + "</p>",
"<p>-------------</p>",
output,
"</body>",
"</html>"
];

MailApp.sendEmail(EMAIL, SUBJECT + " - " + websiteUrl, "", { htmlBody: html.join("\n") });
Logger.log("Email sent.");
}

Google Ads Scripts Forum Advisor

unread,
Jun 20, 2025, 8:57:22 AMJun 20
to adwords...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads Scripts Support team.

On reviewing your concern, I understand that you are facing issue in getting the mail after the script execution. I would like to inform you that the merchant center related scripts and the MailApp() methods related issues are out of scope from Google Ads Scripts. I would recommend you to reach out to the Apps Scripts team for further assistance as they are better equipped to address your concern. We can assist you only on the technical queries related to Google Ads Scripts.​

Thanks,
 
Google Logo Google Ads Scripts Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-06-20 12:56:54Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01rfnnv:ref" (ADR-00315052)



Nenad Blagojevic

unread,
Jun 20, 2025, 9:37:46 AMJun 20
to Google Ads Scripts Forum
Hi - thank you for the response!

Although - this is a Google Ads script, meaning I am using it in our Google Ads account, not even sure where else it could be used.

Thanks in any case, best regards!
Nenad
Reply all
Reply to author
Forward
0 new messages