Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Configure this
  2. var EMAIL_SUBJECT = "Active Account Campaigns Close to Budget";
  3. var CLOSE_THRESHOLD = 0.8; // enter what you consider "close" to daily budget, e.g. 0.8 = 80% of budget
  4. var EMAIL_RECIPIENTS = [
  5.     "martin.baxter@boom-online.co.uk",
  6.     "siobhan.brown@boom-online.co.uk",
  7.     "chris.dennis@boom-online.co.uk",
  8.     "ian@boom-online.co.uk"
  9. ];
  10.  
  11. // Leave this
  12. var style = "text-align:left;font-family:sans-serif;color:#333;margin:0 auto;";
  13.  
  14. function main() {
  15.     // Get all accounts in the MCC with the `Active` label
  16.     var accounts = MccApp.accounts()
  17.         .withCondition("LabelNames CONTAINS 'Active'")
  18.         .get();
  19.  
  20.     // Build report tables for each account
  21.     var emailBodyText = "<div style='" + style + "max-width:650px;display:block;padding:20px;margin:20px;'><h2>These Campaigns are close (" + (CLOSE_THRESHOLD * 100) + "%) to their Daily Budgets</h2>";
  22.     while (accounts.hasNext()) {
  23.         emailBodyText += makeTableForAccountCampaigns(accounts.next());
  24.     }
  25.     emailBodyText += "</div>";
  26.  
  27.     // Send the email
  28.     MailApp.sendEmail({
  29.         to: EMAIL_RECIPIENTS.join(", "),
  30.         subject: EMAIL_SUBJECT,
  31.         htmlBody: emailBodyText
  32.     });
  33. }
  34.  
  35. function makeTableForAccountCampaigns(thisAccount) {
  36.     // Get enabled campaigns for this account
  37.     MccApp.select(thisAccount);
  38.  
  39.     var emailBodyText = "<h3 style='color:#103070;border-top:1px solid #AAA;padding-top:15px;margin-top:15px;display:block;'>" + thisAccount.getName() + "</h3>";
  40.  
  41.     var campaignGroups = [
  42.         {
  43.             "type": "Campaigns",
  44.             "campaigns": AdWordsApp.campaigns().withCondition('Status = ENABLED').get()
  45.         },
  46.         {
  47.             "type": "Shopping Campaigns",
  48.             "campaigns": AdWordsApp.shoppingCampaigns().withCondition('Status = ENABLED').get()
  49.         }
  50.     ]
  51.     for(var i=0; i++; i < campaignGroups.length) {
  52.         emailBodyText += loopThroughCampaigns(campaignGroups[i])
  53.     }
  54.     return emailBodyText;
  55. }
  56.  
  57. function loopThroughCampaigns(campaignGroup) {
  58.    // Init
  59.     var n = 0;
  60.     var emailBodyText = "<h4>"+campaignGroup.type+"</h4>";
  61.     emailBodyText += "<table style='" + style + "width:100%;color:#555'><tr><th width=50%>Campaign</u></th><th width=15%>Budget (£)</th><th>Spend (£)</th></tr>";
  62.    
  63.     // Add a table row for each campaign in this account
  64.     while (campaignGroup.campaigns.hasNext()) {
  65.         var thisCampaign = campaignGroup.campaigns.next();
  66.         var stats = thisCampaign.getStatsFor("YESTERDAY");
  67.         var budget = thisCampaign.getBudget();
  68.         var spend = stats.getCost();
  69.         var name = thisCampaign.getName();
  70.         if (spend > (budget * CLOSE_THRESHOLD)) {
  71.             n++;
  72.             emailBodyText += "<tr><td width=50%>" + name + "</td><td width=15%>£" + budget + "</td><td>£" + spend + "</td></tr>";
  73.         }
  74.     }
  75.     emailBodyText += "</table>"
  76.     if (n === 0) emailBodyText = "";
  77.     return emailBodyText;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement