PLA Script: 自动添加转化词到PLA_Best否定列表

function main() {
  
  //How do you want to call your negative keyword list?
  var targetList = 'PLA_Best'
​
var now = new Date();
var timezone = AdWordsApp.currentAccount().getTimeZone();
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
//var last90Days = new Date(now.getTime() - 90 * MILLIS_PER_DAY);  
//var sqrStartDate = Utilities.formatDate(last90Days, timezone, 'yyyyMMdd')
var firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
var sqrStartDate = Utilities.formatDate(firstDay, timezone, 'yyyyMMdd');
var sqrEndDate = Utilities.formatDate(new Date(), timezone, "yyyyMMdd")
​
var CPA_Threshold_Micro = 50; 
var CPA_Threshold = CPA_Threshold_Micro*1000000;
  
  var brand = 'your brand name'
​
  Logger.log('Report start date: '+sqrStartDate)
  Logger.log('Report end date: '+sqrEndDate.replace(/-/g,''))
  
  //let's build the negKw list if not existing yet  
  if(AdsApp.negativeKeywordLists().withCondition('Name = "'+targetList+'"').get().totalNumEntities()==1)
  {Logger.log('Negative list called '+targetList+' already exists!')}
  //else create the lsit
  else {
    Logger.log('Negative list called '+targetList+' does not exist yet - building now!')
    AdsApp.newNegativeKeywordListBuilder().withName(targetList).build()}
   
  //let's run search query report
  //keep in mind that cost numbers from API are multiplied by 1,000,000 for some reason - that's why I am multiplying by 1,000,000
  var reportText =
     'SELECT Query,Clicks,Cost,Ctr,ConversionRate,CostPerConversion,Conversions,ConversionValue,AllConversions,CampaignId,AdGroupId ' +
     'FROM SEARCH_QUERY_PERFORMANCE_REPORT ' +
     'WHERE Impressions > 0 AND Clicks > 0 AND Conversions > 0 AND CostPerAllConversion < '+CPA_Threshold+' AND CampaignName CONTAINS_IGNORE_CASE "PLA_" + AND Query DOES_NOT_CONTAIN_IGNORE_CASE "'+brand+'" ' +
     'DURING '+sqrStartDate+','+sqrEndDate
  
  Logger.log('Running this SQT Report now: ' + reportText)
  
  var queries = AdsApp.report(reportText);
  
  Logger.log('I am done with running the SQT Report.')
  
  var rows = queries.rows()
  
  //let's create negative kws as exact match
  var negKwsToAdd = []
  while(rows.hasNext()){
    var eachRow=rows.next()
    //Logger.log(eachRow["Query"])
    negKwsToAdd.push('['+eachRow["Query"]+']')
  }
  
  //start checking only there are bad terms to process
  if(negKwsToAdd.length>0){
    Logger.log('These terms are eligible to be added as exact negs: '+negKwsToAdd+'\n'+
              'However, checking if they are not in the list already.'
            )  
  
    //let's start comparing existing neg KWs in the list vs neg KWs which you want to add base on the latest run of search query report 
    var existingNegKws = []
​
    var lists = AdsApp.negativeKeywordLists().withCondition('Name = "'+targetList+'"').get()
    while(lists.hasNext()){
      list = lists.next()
​
      var allCurrentNegKws = list.negativeKeywords().get()
      while (allCurrentNegKws.hasNext()){
        negKwInTheList = allCurrentNegKws.next()
        existingNegKws.push(negKwInTheList.getText())
      }
    }
​
    if(existingNegKws.length==0){
      Logger.log(targetList+' is empty at the moment.')}
      else {Logger.log('These negative keywords are already present in the list: '+existingNegKws)}
​
    var checkedNegKwsToAdd = []
    var i=0
    for(i=0;i<negKwsToAdd.length;i++){
      //here is the main trick for array comparison
      if(existingNegKws.indexOf(negKwsToAdd[i])>-1){
        Logger.log(negKwsToAdd[i]+' is already in '+targetList)}
        else {checkedNegKwsToAdd.push(negKwsToAdd[i])}
      }
  }
  else{Logger.log('No bad terms to process - lucky you!')}
  
  if(checkedNegKwsToAdd.length>0){  
    Logger.log('These neg kws will be added into '+targetList+' list:'+checkedNegKwsToAdd)}
    else {Logger.log('No new neg kws will be added into '+targetList+'. All the eligibile KWs are already there!')}
​
​
    //Let's add the negative kewords into the target list
    if(checkedNegKwsToAdd.length>0){  
      var lists = AdsApp.negativeKeywordLists().withCondition('Name = "'+targetList+'"').get()
      while(lists.hasNext()){
        var list = lists.next()
        list.addNegativeKeywords(checkedNegKwsToAdd)
        Logger.log(checkedNegKwsToAdd+' added as neg KWs into '+targetList)
      }
    }
  }

发表评论

电子邮件地址不会被公开。 必填项已用*标注