Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Configure this
- var EMAIL_SUBJECT = "Active Account Campaigns Close to Budget";
- var CLOSE_THRESHOLD = 0.8; // enter what you consider "close" to daily budget, e.g. 0.8 = 80% of budget
- var EMAIL_RECIPIENTS = [
- "martin.baxter@boom-online.co.uk",
- "siobhan.brown@boom-online.co.uk",
- "chris.dennis@boom-online.co.uk",
- "ian@boom-online.co.uk"
- ];
- // Leave this
- var style = "text-align:left;font-family:sans-serif;color:#333;margin:0 auto;";
- function main() {
- // Get all accounts in the MCC with the `Active` label
- var accounts = MccApp.accounts()
- .withCondition("LabelNames CONTAINS 'Active'")
- .get();
- // Build report tables for each account
- 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>";
- while (accounts.hasNext()) {
- emailBodyText += makeTableForAccountCampaigns(accounts.next());
- }
- emailBodyText += "</div>";
- // Send the email
- MailApp.sendEmail({
- to: EMAIL_RECIPIENTS.join(", "),
- subject: EMAIL_SUBJECT,
- htmlBody: emailBodyText
- });
- }
- function makeTableForAccountCampaigns(thisAccount) {
- // Get enabled campaigns for this account
- MccApp.select(thisAccount);
- var emailBodyText = "<h3 style='color:#103070;border-top:1px solid #AAA;padding-top:15px;margin-top:15px;display:block;'>" + thisAccount.getName() + "</h3>";
- var campaignGroups = [
- {
- "type": "Campaigns",
- "campaigns": AdWordsApp.campaigns().withCondition('Status = ENABLED').get()
- },
- {
- "type": "Shopping Campaigns",
- "campaigns": AdWordsApp.shoppingCampaigns().withCondition('Status = ENABLED').get()
- }
- ]
- for(var i=0; i++; i < campaignGroups.length) {
- emailBodyText += loopThroughCampaigns(campaignGroups[i])
- }
- return emailBodyText;
- }
- function loopThroughCampaigns(campaignGroup) {
- // Init
- var n = 0;
- var emailBodyText = "<h4>"+campaignGroup.type+"</h4>";
- emailBodyText += "<table style='" + style + "width:100%;color:#555'><tr><th width=50%>Campaign</u></th><th width=15%>Budget (£)</th><th>Spend (£)</th></tr>";
- // Add a table row for each campaign in this account
- while (campaignGroup.campaigns.hasNext()) {
- var thisCampaign = campaignGroup.campaigns.next();
- var stats = thisCampaign.getStatsFor("YESTERDAY");
- var budget = thisCampaign.getBudget();
- var spend = stats.getCost();
- var name = thisCampaign.getName();
- if (spend > (budget * CLOSE_THRESHOLD)) {
- n++;
- emailBodyText += "<tr><td width=50%>" + name + "</td><td width=15%>£" + budget + "</td><td>£" + spend + "</td></tr>";
- }
- }
- emailBodyText += "</table>"
- if (n === 0) emailBodyText = "";
- return emailBodyText;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement