Google Ads Script: Auto add queries

Google Ads Script that automatically ads good performing queries.

If the query meets centrain conditions, it will be added as exact match keyword to the same AdGroup.

The script will also send an email to the specified email address when queries are found and added.

var email = 'example@gmail.com';
var subject = 'Queries added';
var dateRange = 'LAST_30_DAYS';

function main () {
  addQueries();  
}

function addQueries () {
  var queries = [];
  
  var queryData = AdWordsApp.report(
    "SELECT Query, AdGroupName, CampaignName, Impressions, Clicks, AdGroupId " +
    "FROM SEARCH_QUERY_PERFORMANCE_REPORT " +
    "WHERE " + 
    // add conditions here according to awql search query performance report
    "Impressions > 5 " +
    "AND Clicks > 1 " + 
    "DURING " + dateRange);

  var rows = queryData.rows();
  var counter = 0;
  while (rows.hasNext()) {
    counter++;
    var row = rows.next();
    var query = row['Query'];
    var campaign = row['CampaignName'];
    var adGroup = row['AdGroupName'];
    var adGroupId = row['AdGroupId'];
    queries.push('Query: ' + query + '\nIn AdGroup : ' + adGroup + ' -> in Campaign: ' + campaign + '\n');
    
    var adGroupData = AdWordsApp.adGroups()
      .withIds([adGroupId])
      .get()
      .next()
      .newKeywordBuilder()
      .withText('[' + query + ']')
      .build();
    
  }
  if (counter > 0) {
    sendEmail(queries);
  }
}

function sendEmail(queries) {
  MailApp.sendEmail(email, subject, queries);
}