automated exclusion of keywords without brand terms

54 views
Skip to first unread message

Adam

unread,
May 23, 2025, 9:15:52 AM5/23/25
to Google Ads Scripts Forum

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,

Google Ads Scripts Forum Advisor

unread,
May 23, 2025, 10:15:31 AM5/23/25
to adwords...@googlegroups.com

Hi,

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

I would like to inform you that the method “AdsApp.campigns()” you are using will only iterate through search and display type campaigns and it won’t work for other types of campaigns. I would suggest that you use the methods “AdsApp.performanceMaxCampaigns()”, “AdsApp.shoppingCampaigns()”, and “AdsApp.videoCampaigns()” in place of “AdsApp.campaigns()” to iterate through performance max, shopping, and video campaigns respectively.

I hope this helps! If the issue still persists even after following the above steps, kindly get back to us with the following details.

  • Google Ads account ID/CID
  • Name of the affected script
You can share the requested details via Reply privately to the author option or a direct private reply to this email.

Thanks,
 
Google Logo Google Ads Scripts Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-05-23 14:14:57Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01qY4YL:ref" (ADR-00307930)



Reply all
Reply to author
Forward
0 new messages