Advertisement
Marc_Cornelius

Google Ads Script: Account Budget Allocator

Jul 23rd, 2024
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. function main() {
  2. listAccountCostsAndSendEmail(); // Lists Account Names, Costs, Projected Monthly Costs, checks against Max Budget, and sends email
  3. }
  4.  
  5. function listAccountCostsAndSendEmail() {
  6. var spreadsheetUrl = '[SPREADSHEET URL]'; // Placeholder for the actual URL of the Google Spreadsheet
  7. var sheet = SpreadsheetApp.openByUrl(spreadsheetUrl).getSheetByName('Table'); // Access the specific sheet by name
  8.  
  9. var iterator = AdsManagerApp.accounts().get(); // Get all managed accounts
  10. var currentDate = new Date();
  11. var daysInMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0).getDate();
  12. var dayOfMonth = currentDate.getDate();
  13.  
  14. var results = []; // Array to hold all the rows of data to write back to the sheet
  15. var emailBody = '<h1>Monthly Projected Costs Report</h1>' +
  16. '<p>Click <a href="' + spreadsheetUrl + '">here</a> to access the full report in Google Sheets.</p>' +
  17. '<table border="1" style="width:100%; border-collapse:collapse;">' +
  18. '<tr><th>Account Name</th><th>Total Costs this Month</th><th>Projected Costs (Month)</th>' +
  19. '<th>Max Budget</th><th>Exceeding</th><th>% From Max Budget</th></tr>';
  20.  
  21. while (iterator.hasNext()) {
  22. var account = iterator.next();
  23. AdsManagerApp.select(account);
  24. var stats = account.getStatsFor("THIS_MONTH");
  25. var totalSpent = stats.getCost();
  26. var projectedMonthlyCosts = (totalSpent / dayOfMonth) * daysInMonth;
  27.  
  28. var maxBudgetCell = sheet.getRange(results.length + 2, 4);
  29. var maxBudget = parseFloat(maxBudgetCell.getValue());
  30. if (isNaN(maxBudget)) {
  31. maxBudget = 0;
  32. maxBudgetCell.setValue(0);
  33. }
  34.  
  35. var exceeding = projectedMonthlyCosts > maxBudget ? 'YES' : 'NO';
  36. var percentFromBudget = ((projectedMonthlyCosts - maxBudget) / maxBudget) * 100;
  37. var rowColor = exceeding === 'YES' ? ' style="background-color:red;"' : '';
  38.  
  39. var dataRow = [
  40. account.getName(),
  41. totalSpent.toFixed(2),
  42. projectedMonthlyCosts.toFixed(2),
  43. maxBudget.toFixed(2),
  44. exceeding,
  45. percentFromBudget.toFixed(2) + '%'
  46. ];
  47. results.push(dataRow);
  48.  
  49. emailBody += '<tr' + rowColor + '><td>' + account.getName() + '</td><td>$' + totalSpent.toFixed(2) + '</td>' +
  50. '<td>$' + projectedMonthlyCosts.toFixed(2) + '</td><td>$' + maxBudget.toFixed(2) + '</td>' +
  51. '<td>' + exceeding + '</td><td>' + percentFromBudget.toFixed(2) + '%</td></tr>';
  52. }
  53. emailBody += '</table>';
  54.  
  55. var range = sheet.getRange(2, 1, results.length, 6);
  56. range.setValues(results);
  57.  
  58. MailApp.sendEmail({
  59. to: "[EMAIL ADDRESS]",
  60. subject: "Monthly Projected Costs Report",
  61. htmlBody: emailBody
  62. });
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement