exclude non branded keywords from my branded search campaigns script

200 views
Skip to first unread message

Fay Carli

unread,
Jul 10, 2023, 12:59:24 PM7/10/23
to Google Ads Scripts Forum
Hi,

I want to exclude non branded keywords in my non branded campaigns with this script below. However, when I use the script, it also excludes keywords in my NB campaigns... Do you know what's going wrong? 

-- 

// Copyright 2022
// Free to use or alter for everyone. A mention would be nice ;-)
//
// Created by: Tibbe van Asten
//
// Created: 14-06-2018
// Last update: 16-10-2022
//
// ABOUT THE SCRIPT
// With this script you can exclude non-branded queries
// from branded shopping campaigns. By doing this
// every day, the accountstructure will remain clean.
//
////////////////////////////////////////////////////////////////////

var config = {
 
    LOG : true,

    // Branded search query. Make sure to use quotes.
    // If you only use 2 branded keywords, remove variable + row in var report
    BRANDED_KEYWORDS_1 : "vacansoleil",
    BRANDED_KEYWORDS_2 : "vacancesoleil",
    BRANDED_KEYWORDS_3 : "vacance soleil",
 
    // The mark for every branded campaign in the account
    B_SELECTOR : "[-B-]"
 
}

////////////////////////////////////////////////////////////////////

function main() {
 
    var report = AdsApp.report(
      "SELECT campaign.name, ad_group.id, ad_group.name, search_term_view.search_term " +
      "FROM search_term_view " +
      "WHERE campaign.status = 'ENABLED' AND ad_group.status = 'ENABLED' " +
      "AND campaign.name REGEXP_MATCH '(?i)(.*)?"+config.B_SELECTOR+"(.*)?' " +
      "AND search_term_view.search_term NOT REGEXP_MATCH '(?i)(.*)?"+config.BRANDED_KEYWORDS_1+"(.*)?' " +
      "AND search_term_view.search_term NOT REGEXP_MATCH '(?i)(.*)?"+config.BRANDED_KEYWORDS_2+"(.*)?' " +
      "AND search_term_view.search_term NOT REGEXP_MATCH '(?i)(.*)?"+config.BRANDED_KEYWORDS_3+"(.*)?' " +
      "AND search_term_view.status = 'NONE'"
    );    

    var rows = report.rows();
    while (rows.hasNext()) {
      var row = rows.next();
     
      var query = trimQuery(row);

      var shoppingAdGroupIterator = AdsApp
        .shoppingAdGroups()
        .withCondition("Id = '"+row["ad_group.id"]+"'")
        .get();
     
      while (shoppingAdGroupIterator.hasNext()) {
        var shoppingAdGroup = shoppingAdGroupIterator.next();
        shoppingAdGroup.createNegativeKeyword(query);
       
        if(config.LOG === true){
          Logger.log(row["campaign.name"]);
          Logger.log(row["ad_group.name"]);
          Logger.log(query);
          Logger.log(" ");      
        }
      } // shoppingAdGroupIterator
     
      var adGroupIterator = AdsApp
        .adGroups()
        .withCondition("Id = '"+row["ad_group.id"]+"'")
        .get();
     
      while (adGroupIterator.hasNext()) {
        var adGroup = adGroupIterator.next();
        adGroup.createNegativeKeyword(query);

        if(config.LOG === true){
          Logger.log(row["campaign.name"]);
          Logger.log(row["ad_group.name"]);
          Logger.log(query);
          Logger.log(" ");  
        }
      } // adGroupIterator
     
    } // rowIterator  
 
    Logger.log("Thanks for using this custom script by Tibbe van Asten!");
 
} // function main()

////////////////////////////////////////////////////////////////////

function trimQuery(row){

  // When the searchquery exceeds the limit of 10 words, we will split
  // the query and put max. 10 words back together as a phrasematch query
  var query = "";
  if (row["search_term_view.search_term"].split(" ").length < 10) {
    query = "[" + row["search_term_view.search_term"] + "]";
  } else {
    for (var i = 0; (i < row["search_term_view.search_term"].split(" ").length) && (i < 10); i++){
      query += row["search_term_view.search_term"].split(" ")[i] + " ";
    }
    query = '"' + query.replace(/\s+$/,'') + '"';
  }

  return query;

} // function trimQuery()

Nils Rooijmans

unread,
Jul 12, 2023, 3:57:36 AM7/12/23
to Google Ads Scripts Forum
most probably, the text string defined to identify your Brand campaign is not specific enough (also matches non-brand campaigns).

You should set it here, replace the yellow part with text string that is unique to your brand campaigns :
   B_SELECTOR : "[-B-]"


Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Fay Carli

unread,
Jul 13, 2023, 3:44:44 AM7/13/23
to Nils Rooijmans via Google Ads Scripts Forum
Hi,

I just renamed one campaign with BRAND to test if it would only exclude keywords in that campaign, but it still just excludes all NB keywords in all campaigns... Do you have any other ideas?

The campaign I was testing with: nl-be: -B- -CAMPING- (ALL) _BRAND_

See script below: 

// Copyright 2022
// Free to use or alter for everyone. A mention would be nice ;-)
//
// Created by: Tibbe van Asten
//
// Created: 14-06-2018
// Last update: 16-10-2022
//
// ABOUT THE SCRIPT
// With this script you can exclude non-branded queries
// from branded shopping campaigns. By doing this
// every day, the accountstructure will remain clean.
//
////////////////////////////////////////////////////////////////////

var config = {
 
    LOG : true,

    // Branded search query. Make sure to use quotes.
    // If you only use 2 branded keywords, remove variable + row in var report
    BRANDED_KEYWORDS_1 : "vacansoleil",         
    BRANDED_KEYWORDS_2 : "vacancesoleil",
    BRANDED_KEYWORDS_3 : "vacance soleil",
 
    // The mark for every branded campaign in the account
    B_SELECTOR : "[_BRAND_]"                          

Fay Carli

SEA Marketeer | E-Commerce




e. fay....@vacansoleil.com 

w. vacansoleil.com 

  

Marinus van Meelweg 16 

5657 EN Eindhoven 

Kvk Eindhoven 17032917 

  

     


From: Nils Rooijmans via Google Ads Scripts Forum <adwords...@googlegroups.com>
Sent: Wednesday, July 12, 2023 9:57 AM
To: Google Ads Scripts Forum <adwords...@googlegroups.com>
Subject: Re: exclude non branded keywords from my branded search campaigns script
 
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/yJaXvQKmQMg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/8e9cfba9-be3a-48e0-88ee-439f62c56eddn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages