Hello,
I'm adding a script to Google Ads, but I'm facing an issue:
The script doesn't detect any active campaigns, even though the campaign is currently running.
I used the following code, which doesn't work:
function main() {
var campaignName = "Bestsellery - produktowa";
var sharedListName = "Bestsellery-wyklucz-slowa";
var whitelist = ["carrello", "ultra", "bravo", "carbon", "lite", "omega", "alfa", "vector"];
Logger.log("=== STARTING SCRIPT ===");
Logger.log("Looking for campaign: " + campaignName);
// Get the campaign
var campaignIterator = AdsApp.campaigns()
.withCondition("Name = '" + campaignName + "'")
.get();
if (!campaignIterator.hasNext()) {
Logger.log("❌ Campaign not found: " + campaignName);
return;
}
var campaign = campaignIterator.next();
Logger.log("✅ Campaign found: " + campaign.getName());
// Find the shared negative keyword list
Logger.log("Looking for negative keyword list: " + sharedListName);
var sharedListIterator = AdsApp.negativeKeywordLists()
.withCondition("Name = '" + sharedListName + "'")
.get();
if (!sharedListIterator.hasNext()) {
Logger.log("❌ List not found: " + sharedListName);
return;
}
var sharedList = sharedListIterator.next();
Logger.log("✅ List found: " + sharedList.getName());
// Get existing negative keywords from the list
var existingNegatives = [];
var keywordIterator = sharedList.negativeKeywords().get();
while (keywordIterator.hasNext()) {
existingNegatives.push(keywordIterator.next().getText().toLowerCase());
}
Logger.log("📋 Number of existing negative keywords: " + existingNegatives.length);
// Get active keywords from the campaign
Logger.log("Looking for active keywords...");
var keywords = AdsApp.keywords()
.withCondition("CampaignName = '" + campaignName + "'")
.withCondition("Status = ENABLED")
.get();
Logger.log("🔍 Number of active keywords: " + keywords.totalNumEntities());
var processed = 0;
while (keywords.hasNext()) {
var keyword = keywords.next();
var text = keyword.getText().toLowerCase();
var cleanedText = text.replace(/[\[\]\+\"]/g, "").trim();
Logger.log("🔹 Checking keyword: " + text);
var containsAllowed = whitelist.some(function(allowed) {
return cleanedText.includes(allowed);
});
if (!containsAllowed) {
Logger.log("⛔ NOT in whitelist - will be excluded: " + cleanedText);
if (!existingNegatives.includes(cleanedText)) {
sharedList.addNegativeKeyword(cleanedText);
Logger.log("✅ Added to shared negative list: " + cleanedText);
} else {
Logger.log("ℹ️ Already in the list: " + cleanedText);
}
if (!keyword.isPaused()) {
keyword.pause();
Logger.log("⏸️ Keyword paused: " + text);
} else {
Logger.log("⏹️ Already paused: " + text);
}
} else {
Logger.log("✅ Kept active (matches whitelist): " + text);
}
processed++;
}
Logger.log("=== FINISHED: processed " + processed + " keywords ===");
}
Additionally, I created a separate test script to check whether it detects any campaigns at all — and it detects nothing:
function main() {
Logger.log("=== LIST OF REMOVED AND ENDED CAMPAIGNS ===");
Logger.log("Google Ads Account: " + AdsApp.currentAccount().getCustomerId());
var campaignIterator = AdsApp.campaigns().get();
var found = false;
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var status = campaign.getStatus();
if (status === "REMOVED" || status === "ENDED") {
Logger.log("📌 Campaign: '" + campaign.getName() + "' — Status: " + status);
found = true;
}
}
if (!found) {
Logger.log("No campaigns found with status REMOVED or ENDED.");
}
Logger.log("=== END OF LIST ===");
}
What am I doing wrong?
Best regards,