Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. function main() {
  2. var CAMPAIGN_NAME_CONTAINS = [];
  3. // Use this if you only want to look at some campaigns.
  4. // For example ["Generic"] would only look at campaigns with 'generic' in the name,
  5. // while ["Generic", "Competitor"] would only look at campaigns with either
  6. // 'generic' or 'competitor' in the name.
  7. // Leave as [] to include all campaigns.
  8.  
  9. var CAMPAIGN_NAME_EXCLUDES = [];
  10. // Use this if you want to exclude some campaigns.
  11. // For example ["Brand"] would ignore any campaigns with 'brand' in the name,
  12. // while ["Brand", "Key Terms"] would ignore any campaigns with 'brand' or
  13. // 'key terms' in the name.
  14. // Leave as [] to not exclude any campaigns.
  15.  
  16. var WARNING_PERCENTAGE_CAP = -1;
  17. // The percentage of a campaign's budget that needs to have been spent for
  18. // the tool to consider that campaign over cap and warn about it.
  19.  
  20. var EMAILS = [''];
  21. // The email address you want the hourly update to be sent to.
  22. // If you'd like to send to multiple addresses then have them separated by commas,
  23. // for example ["aa@example.com", "bb@example.com"]
  24.  
  25. try {
  26. var campaigns = getCampaigns(CAMPAIGN_NAME_CONTAINS, CAMPAIGN_NAME_EXCLUDES);
  27. var overCap = getOverCap(WARNING_PERCENTAGE_CAP, campaigns);
  28. alert(overCap, EMAILS);
  29. } catch(e) {
  30. alertError(e, EMAILS);
  31. }
  32. }
  33.  
  34. function getCampaigns(campNameContains, campNameExcludes) {
  35. var campaigns = {};
  36. var whereStatementsArray = buildWhereStatements(campNameContains, campNameExcludes);
  37. for (var i = 0; i < whereStatementsArray.length; i++) {
  38. var query = "SELECT CampaignId, Date, Cost, Amount, CampaignName" +
  39. " FROM CAMPAIGN_PERFORMANCE_REPORT " +
  40. whereStatementsArray[i] +
  41. "DURING TODAY";
  42. var rows = AdWordsApp.report(query).rows();
  43. while (rows.hasNext()) {
  44. var campaign = campaignFromRow(rows.next());
  45. campaigns[campaign.id] = campaign;
  46. }
  47. }
  48. return campaigns;
  49. }
  50.  
  51. function buildWhereStatements(campNameContains, campNameExlcudes) {
  52. var whereStatement = "WHERE CampaignStatus IN ['ENABLED','PAUSED'] ";
  53. for (var i = 0; i < campNameExlcudes.length; i++) {
  54. whereStatement += "AND CampaignName DOES_NOT_CONTAIN_IGNORE_CASE '"
  55. + campNameExlcudes[i].replace(/"/g,'\\\"') + "' ";
  56. }
  57. var whereStatementsArray = [];
  58. if (campNameContains.length == 0) {
  59. whereStatementsArray = [whereStatement];
  60. } else {
  61. for (var i = 0; i < campNameContains.length; i++) {
  62. whereStatementsArray.push(whereStatement + 'AND CampaignName CONTAINS_IGNORE_CASE "'
  63. + campNameContains[i].replace(/"/g,'\\\"') + '" ');
  64. }
  65. }
  66. return whereStatementsArray;
  67. }
  68.  
  69. function campaignFromRow(reportRow) {
  70. var obj = {};
  71. obj.id = reportRow["CampaignId"];
  72. obj.name = reportRow["CampaignName"];
  73. obj.budget = reportRow["Amount"];
  74. obj.spend = reportRow["Cost"];
  75. obj.spent = function() {
  76. return 100 * (1 - (obj.budget - obj.spend)/obj.budget);
  77. }
  78. obj.overSpendCap = function(cap) {
  79. return (obj.spent() > cap);
  80. }
  81. return obj;
  82. }
  83.  
  84. function getOverCap(cap, campaigns) {
  85. var overCap = [];
  86. for (var campaignId in campaigns) {
  87. var campaign = campaigns[campaignId];
  88. if (campaign.overSpendCap(cap)) {
  89. overCap.push(campaign);
  90. }
  91. }
  92. return overCap;
  93. }
  94.  
  95. function alert(campaigns, EMAILS) {
  96. var subject = AdWordsApp.currentAccount().getName() + " - Budgets Nearly Spent";
  97. var message = buildTable(campaigns);
  98.  
  99. MailApp.sendEmail({
  100. to: EMAILS.join(','),
  101. subject: subject,
  102. htmlBody: message
  103. });
  104. }
  105.  
  106. function buildTable(campaigns) {
  107. var table = "<table border=1 style='border: 1px solid black; border-collapse: collapse;'>";
  108. table += "<tr><th>Campaign ID</th><th>Campaign Name</th><th>Budget</th><th>Percent of Budget Spent</th></tr>";
  109. for (var campaignId in campaigns) {
  110. var campaign = campaigns[campaignId];
  111. table += "<tr><td>" + campaign.id + "</td>";
  112. table += "<td>" + campaign.name + "</td>";
  113. table += "<td>" + campaign.budget + "</td>";
  114. table += "<td>" + campaign.spent().toFixed(2) + "</td>";
  115. table += "</tr>";
  116. }
  117. table += "</table>";
  118. return table;
  119. }
  120.  
  121. function alertError(error, emails) {
  122. MailApp.sendEmail(
  123. emails.join(','),
  124. 'Budget Script - Error',
  125. error);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement