Advertisement
Guest User

Untitled

a guest
Sep 9th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var TIME_PERIOD = "LAST_30_DAYS" // THIS_MONTH, LAST_MONTH, LAST_30_DAYS, ALL_TIME
  3. var EMAIL_ADDRESS = "[email protected]" //
  4.  
  5.  
  6. function main(){
  7.   // Get out account data
  8.   var account_data = AdsApp.currentAccount().getStatsFor(TIME_PERIOD);
  9.  
  10.   // Total Values
  11.   ttl_clicks = account_data.getClicks();
  12.   ttl_impr = account_data.getImpressions();
  13.   ttl_cost = account_data.getCost();
  14.   ttl_conversions = account_data.getConversions();
  15.  
  16.   // Averages
  17.   avg_cpa = ttl_cost / ttl_conversions;
  18.   avg_ctr = account_data.getCtr();
  19.   avg_cpc = account_data.getAverageCpc();
  20.  
  21.   // This will create a string with a space in the beggining and then ISO code like: USD, CAD, AUD, INR, EUR...
  22.   currency = " " + AdsApp.currentAccount().getCurrencyCode();
  23.  
  24.  
  25.   // Create an array with all the data
  26.   data = []
  27.   data.push("Total Clicks " + ttl_clicks );
  28.   data.push("Total Imp " + ttl_impr );
  29.   data.push("Total Cost " + ttl_cost + currency);
  30.   data.push("Total Conv " + ttl_conversions );
  31.   data.push("AVG CPA " + avg_cpa + currency);
  32.   data.push("AVG CTR " + avg_ctr );
  33.   data.push("AVG CPC " + avg_cpc + currency);
  34.  
  35.  
  36.   for (i = 0; i < data.length; i++){
  37.     // Send each element of array (out stats) as separate message
  38.     Logger.log(data[i]);
  39.   }
  40.  
  41.     columns = ["CampaignName", "AdGroupName", "Criteria", "Clicks", "Cost", "CostPerConversion"];
  42.  
  43.     conditions = [];
  44.     conditions.push("Cost > " + (avg_cpa * 1000000).toString());
  45.     conditions.push("CostPerConversion > " + (avg_cpa * 2 * 1000000).toString());
  46.  
  47.     columns_string = columns.join(", ") // CampaignName, AdGroupName
  48.     conditions_string = conditions.join(" AND ") // Cost > 10 AND CostPerConversion > 10
  49.  
  50.     Logger.log(columns_string)
  51.     Logger.log(conditions_string)
  52.  
  53.     var awql = "SELECT " + columns_string + " FROM KEYWORDS_PERFORMANCE_REPORT WHERE " + conditions_string + " DURING " + TIME_PERIOD;
  54.  
  55.     var report = AdsApp.report(awql).rows();
  56.  
  57.     var total_bad_keywords = 0;
  58.     Logger.log("Here's you data");
  59.  
  60.     var report_data = []; //[  ["cmpName", "AdgrName", "bmw buy la", "10", "100", "10"], ... ]
  61.  
  62.     while(report.hasNext()){
  63.       row = report.next();
  64.      
  65.       metrics = [];
  66.  
  67.       for(i = 0; i < columns.length; i++){
  68.         metrics.push(row[columns[i]]);
  69.       }
  70.       report_data.push(metrics)
  71.       total_bad_keywords += 1;
  72.     }
  73.  
  74.     Logger.log("Total Bad Keywords " + total_bad_keywords.toString());
  75.  
  76.     //
  77.     report_data.sort(function(a, b) {
  78.         return b[4] - a[4];
  79.     });
  80.  
  81.  
  82.     // Contruct Beautiful e-mail
  83.     html_table_header = '<table border="0" cellspacing="0" style="border-color: #C0C0C0; border-collapse: collapse; padding: 5px; width: 100%">';
  84.     html_table_columns = '<tr><th align="left">' + columns.join('</th><th align="left">') + '</th></tr>';
  85.     html_table_rows = ''
  86.     html_final_row = '</table><br><h3>Find more automation solutions on YouTube - Markteting Watch House</h3> '
  87.  
  88.    
  89.     for (i = 0; i < report_data.length; i++){
  90.       html_table_rows += '<tr style="border-bottom: 1px"><td>' + report_data[i].join('</td><td>') + '</td></tr>';
  91.     }
  92.  
  93.     html_first_row = '<b> Total Bad Keywords Found: ' + total_bad_keywords + '</b><br>'+
  94.              'Currency: ' + currency + '<br />'
  95.  
  96.     final_html = html_first_row + html_table_header + html_table_columns + html_table_rows + html_final_row;
  97.  
  98.     if (total_bad_keywords > 0 && total_bad_keywords < 200) {
  99.         var date = new Date().toLocaleDateString();
  100.         var account = AdsApp.currentAccount().getName();
  101.  
  102.         MailApp.sendEmail({
  103.             to: EMAIL_ADDRESS,
  104.             subject: "KeyWord Analysis: " + account + ". Date: " + date + "",
  105.             htmlBody: final_html
  106.         });
  107.     } else if (total_bad_keywords >= 200){
  108.     var date = new Date().toLocaleDateString();
  109.         var account = AdsApp.currentAccount().getName();
  110.  
  111.         MailApp.sendEmail({
  112.             to: EMAIL_ADDRESS,
  113.             subject: "Found too many bad keywords. Need a Manual look.",
  114.             htmlBody: final_html
  115.         });
  116.     }
  117.  
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement