Script per l’Esclusione Automatica delle Query da Foglio Google

Automatic Exclusion of Queries from Google Sheet

Qualche tempo fa ho creato uno script che permette di escludere automaticamente le query nelle campagne Search o Shopping. Si imposta una lista di parole chiave da non escludere e tutte le query che non contengono una delle parole della lista vengono escluse.

Ammetto che il verbo “creare” è un po’ esagerato, perché sono partito dallo script di Floris de Schrijver, grande esperto e “fornitore” di script utilissimi. Il suo script gratuito aveva però un limite: funzionava solo con una singola parola chiave. Ho quindi chiesto al mio amico GPT di modificare lo script in modo da poter usare una lista di parole chiave.

Qualche settimana fa, avevo l’esigenza di ripulire rapidamente una campagna Shopping con un elenco di oltre 200 parole da NON escludere. Gestire oltre 200 parole separate da virgole e apici non è semplicissimo e, soprattutto, lo script non girava. Avevo superato il limite.

Ho quindi interpellato nuovamente GPT. Ora la lista da escludere si carica in un foglio Google separato, senza limiti di parole. Tutte le query (presenti nel report delle query) che non contengono una delle parole del foglio vengono automaticamente escluse in un elenco.

Lo script non è case-sensitive, quindi non fa distinzione tra maiuscole e minuscole.

Nota Bene: Le liste di keyword negative possono contenere fino a 5.000 parole. Se la campagna genera un alto numero di impression, la lista negativa raggiungerà velocemente le 5.000 parole e lo script non funzionerà più. Ti consiglio quindi di alzare le soglie minime di clic e impression.

/* 
	GIUSEPPE SCOLLO AUTOMATIC EXCLUSION FROM QUERY BRAND v3 + GOOGLE SHEET
	copyright by giuseppescollo.it
	CREDIT TO - Floris de Schrijver - for the original script i modified

	Instructions
	1.Create the Google Spreadsheet
	Create a new Google Spreadsheet and enter the exclusion keywords in column A, one per row.
	Name the sheet (e.g., Sheet1) and copy the Spreadsheet URL for script configuration.
	
	2.Script in Google Ads.
	Configure the following variables:
	- exclusionsSpreadsheetUrl: insert the Google Spreadsheet URL.
	- exclusionsSheetName: specify the sheet name (e.g., Sheet1).
	- impressions and clicks: set minimum thresholds for analyzing queries.
	- listName: enter the name of the negative keyword list.
	- campaignName: specify the name of the campaign to monitor.
*/ 

var exclusionsSpreadsheetUrl = 'SPREADSHEET URL'; // insert the Google Spreadsheet URL
var exclusionsSheetName = 'Foglio1'; // specify the sheet name (e.g., Sheet1 or Foglio1)
var impressions = 10; // set minimum thresholds impression for analyzing queries 
var clicks = 1; // set minimum thresholds clic impression for analyzing queries 
var listName = "NO BRAND"; // name of the negative keyword list
var campaignName = "CAMPAIGN NAME"; //  name of the campaign to monitor
function main() {
  var exclusions = loadExclusionsFromSpreadsheet(); // Carica esclusioni da Google Spreadsheet
  
  var searchQueryReport = AdWordsApp.report(
    'SELECT Query, Clicks, Impressions, Cost ' +
    'FROM SEARCH_QUERY_PERFORMANCE_REPORT ' +
    'WHERE CampaignName = "' + campaignName + '" AND Clicks >= ' + clicks + ' AND Impressions >= ' + impressions +
    ' DURING LAST_7_DAYS'
  );

  var negativeKeywordList = AdWordsApp.negativeKeywordLists()
    .withCondition('Name = "' + listName + '"')
    .get()
    .next();

  var rows = searchQueryReport.rows();
  while (rows.hasNext()) {
    var row = rows.next();
    var query = row.Query;
    if (!shouldExcludeQuery(query, exclusions)) {
      negativeKeywordList.addNegativeKeyword("[" + query + "]");
    }
  }
}

function loadExclusionsFromSpreadsheet() {
  var spreadsheet = SpreadsheetApp.openByUrl(exclusionsSpreadsheetUrl);
  var sheet = spreadsheet.getSheetByName(exclusionsSheetName);
  var data = sheet.getRange('A:A').getValues(); // Supponendo che le parole siano nella colonna A
  var exclusions = data.map(function(row) { return row[0]; }).filter(String); // Rimuove eventuali righe vuote
  return exclusions;
}

function shouldExcludeQuery(query, exclusions) {
  var lowerCaseQuery = query.toLowerCase(); // Converti la query in minuscolo
  for (var i = 0; i < exclusions.length; i++) {
    if (lowerCaseQuery.indexOf(exclusions[i].toLowerCase()) !== -1) { // Confronto case-insensitive
      return true;
    }
  }
  return false;
}

Buon scripting.

Lascia una risposta

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *