I talked in my MCC scripting beginner’s guide about generating report data and viewing this via the log, but only goes so far to being useful. When something needs to happen on a scheduled basis there are very few times that you don’t want to receive confirmation, and for the most part sending this via email makes life far easier.
Building the HTML
Before I talk about actually sending an email, it’s important that we understand how this is actually created. 99% of the time you’ll want to include HTML as part of the body text, and AdWords scripting makes it really easy to build that. Let’s start off by creating our variables:
function main() { // Set up the email header and footer var emailHeader = []; var emailFooter = []; var emailBody = []; }
As you can see above, we’ve created a separate variable for the header, footer, and body of the email. You could do this in a single part but it makes my life easier when explaining this if I split it out. You’ll also notice the [] after the declaration of the variable – this simple means that they’re arrays which, again, makes our life easier when we’re building the HTML.
The email header and footer
So, now we have our variables let’s do something with them. We’ll start by creating the HTML that will form our header and footer:
emailHeader.push('<html>', '<head></head>', '<body>'); emailFooter.push('</body>', '</html>');
By using variableName.push, with commas separating each addition, we’re adding code into the variable with different references. For the header & footer this isn’t overly useful – we’ll come back to it in more detail when we create the body – but hopefully this will all be making sense so far.
The body of the email
In my first example I showed you how to get simple weekly stats out of AdWords, such as the account name, clicks, impressions and CTR. We used Logger.log to output these to the console but this time around we’re going to add them to the body of our email:
emailBody.push('<strong>' + account.getName() + '</strong><br />Clicks: ' + clicks + '<br />Impressions: ' + impressions + '<br />CTR: ' + ctr + '<br />Cost: £' + cost + '<br /><br />');
As you can see, we’re using .push to add this to an array. If we build this into our “loop” that gets stats for each account we’re working on we’ll end up with a set of stats that can then be emailed all in one go.
Sending the email
Once we have our HTML it’s then just a case of creating the code to send the email. Here’s an example:
MailApp.sendEmail({ to: 'matt@aira.net', name: 'Reporting Script Sender', subject: 'Report Data from Adwords', htmlBody: emailHeader.join('\n') + emailBody.join('\n') + emailFooter.join('\n') });
As you can see, we’re using the built in MailApp.sendEmail function to send our mail. We’re then specifying the “to” and “cc” fields (yes, scripting a cc address into an AdWords script really is that simple) to say who should receive this email, giving the sender a name, adding a subject line, and then joining together our header, body and footer HTML into one chunk.
Putting it all together
Finally, let’s take the example from the MCC scripting beginner’s guide and add this email functionality:
function main() { // Specify the label name to look for var labelName = 'Friday Report'; // Select the accounts to be processed. var accountIterator = MccApp.accounts() .withCondition('LabelNames CONTAINS "' + labelName + '"') .get(); var emailHeader = []; var emailFooter = []; var emailBody = []; emailHeader.push('<html>', '<head></head>', '<body>'); emailFooter.push('</body>', '</html>'); // First we take the accountIterator variable from before while (accountIterator.hasNext()) { // And, for each account, we save the data into another variable var account = accountIterator.next(); // Switch to the account you want to process. MccApp.select(account); // Now we can do something // Generate account level query var report = AdWordsApp.report( 'SELECT Clicks, Impressions, Cost, Ctr ' + 'FROM ACCOUNT_PERFORMANCE_REPORT ' + 'DURING LAST_WEEK'); // Get the results and save them into a variable var rows = report.rows(); while (rows.hasNext()) { var row = rows.next(); var clicks = row['Clicks']; var impressions = row['Impressions']; var cost = row['Cost']; var ctr = row['Ctr']; } Logger.log(account.getName() + ": Clicks " + clicks + ', Impressions ' + impressions + ' CTR ' + ctr + ', Cost £' + cost); emailBody.push('<strong>' + account.getName() + "</strong><br />Clicks: " + clicks + '<br />Impressions: ' + impressions + '<br />CTR: ' + ctr + '<br />Cost: £' + cost + '<br /><br />'); } // Send our report email MailApp.sendEmail({ to: 'you@you.com', name: 'Reporting Script Sender', subject: 'Report Data from Adwords', htmlBody: emailHeader.join('\n') + emailBody.join('\n') + emailFooter.join('\n') }); }
Advanced options
So now you know to send email with an AdWords script, what’s next? Well, how about including attachments? What if that attachment was a spreadsheet with a load of extra data? Well, that’s possible too and something that I’ll cover in my next post.
If you’re looking for help with your AdWords campaigns, I offer consulting on both an agency and freelance basis.
22 Comments
Thanks for sharing Matt! Very helpful! 🙂 Can’t wait to read your next blog post.
Very helpful piece Matt.
I was wondering, how much time does it take to put it all together?
Kind regards,
Filip
Nice guide! Helpful indeed! Thanks for sharing this.
Great Guide! Thanks for the help!
ssjhgafjblhdizxxrykogrmcflwtwg
blzrqahktocnmycccciqtmguumqmjo
bhkposaaaualmakhfdgzlwsylujhnj
oandpyzddxmepyrudminoovbtigvss
rdhxheylfbibcyvmqhqpjfdqsayyhz
wlfjgqhtdpizfcxrnknoufradaagev
fjgrcsfmengrazcthuzlydjphzqtuc
tqagyrskbqtvvkhhhvhacbbtbnxhei
syuuresazksqmwtphsvpkjtphgtidb
aufzuwuwgfglwixpavfcajkizqlwfy
usodlledkijqatamuizltjrzbdmxpy
xislpyapasczbcppigujnrkmotgsun
zgbjoyqccoyjgjpkkdihaiudtdjxfq
hihtpkaexmecfbnewxxunmtwjtsvyf
hviepzugttdjxjvrlbrluqqhtecglx
pyasjsbtpnnjvnfzarhomaqzacxhii
ssrknkebhkyirhnhaqibqecxvwneyp
mhnbqqmbklwjcgebnlvjevqcbptdgr