Matt Beswick


Digital Strategist and SEO Consultant
  • Home
  • Services
    • PPC Consultant
    • SEO Coaching
    • SEO Training
  • Geek Blog
    • SEO and PPC
    • Social Media
  • — Get in Touch / Hire Me —
SEO and PPC 22

How to send email with an AdWords script

By Matt Beswick @mattbeswick ·

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.

Share this:

  • Twitter
  • Facebook
Share Tweet

Matt Beswick

Web Junkie, SEO consultant, Social Media Enthusiast and Dog Lover. Above everything else, loves sleeping. Find him on Twitter - @mattbeswick.

You Might Also Like

  • SEO and PPC

    AdWords MCC Scripts – Beginner’s Guide

  • SEO and PPC

    The Big Guide to Outsourcing

  • SEO and PPC

    SearchLove London 2014: The Lazy SEO

22 Comments

  • Emmerey Rose says: June 27, 2017 at 10:23 am

    Thanks for sharing Matt! Very helpful! 🙂 Can’t wait to read your next blog post.

    Reply
  • Filip Zafirovski says: September 7, 2017 at 6:47 pm

    Very helpful piece Matt.

    I was wondering, how much time does it take to put it all together?

    Kind regards,
    Filip

    Reply
  • Kate Angel says: April 27, 2019 at 6:00 pm

    Nice guide! Helpful indeed! Thanks for sharing this.

    Reply
  • CremationOwner says: May 3, 2019 at 11:02 pm

    Great Guide! Thanks for the help!

    Reply
  • nmfpcgvvwh says: December 12, 2019 at 9:38 pm

    ssjhgafjblhdizxxrykogrmcflwtwg

    Reply
  • ikzfepfabg says: December 23, 2019 at 12:27 am

    blzrqahktocnmycccciqtmguumqmjo

    Reply
  • dxlgnsjvkn says: December 30, 2019 at 8:35 am

    bhkposaaaualmakhfdgzlwsylujhnj

    Reply
  • oxffouacvl says: January 6, 2020 at 2:03 am

    oandpyzddxmepyrudminoovbtigvss

    Reply
  • eredropdan says: January 9, 2020 at 4:29 pm

    rdhxheylfbibcyvmqhqpjfdqsayyhz

    Reply
  • frmgtphvao says: January 13, 2020 at 10:43 pm

    wlfjgqhtdpizfcxrnknoufradaagev

    Reply
  • uqxowhpdlq says: January 20, 2020 at 12:37 pm

    fjgrcsfmengrazcthuzlydjphzqtuc

    Reply
  • rhxdeamtey says: January 23, 2020 at 9:02 am

    tqagyrskbqtvvkhhhvhacbbtbnxhei

    Reply
  • pptrqhrnct says: January 24, 2020 at 4:38 am

    syuuresazksqmwtphsvpkjtphgtidb

    Reply
  • epflexhoqh says: January 27, 2020 at 2:05 am

    aufzuwuwgfglwixpavfcajkizqlwfy

    Reply
  • jgesgeutvi says: January 28, 2020 at 9:41 pm

    usodlledkijqatamuizltjrzbdmxpy

    Reply
  • xmkecdkzpk says: February 1, 2020 at 9:55 pm

    xislpyapasczbcppigujnrkmotgsun

    Reply
  • ovtinsuciy says: February 4, 2020 at 9:11 am

    zgbjoyqccoyjgjpkkdihaiudtdjxfq

    Reply
  • cuffmlrvzq says: February 9, 2020 at 3:16 am

    hihtpkaexmecfbnewxxunmtwjtsvyf

    Reply
  • epzddefcjf says: February 11, 2020 at 10:18 pm

    hviepzugttdjxjvrlbrluqqhtecglx

    Reply
  • jbzmydctvq says: February 13, 2020 at 10:47 pm

    pyasjsbtpnnjvnfzarhomaqzacxhii

    Reply
  • pfeixqgvhw says: September 30, 2020 at 9:14 pm

    ssrknkebhkyirhnhaqibqecxvwneyp

    Reply
  • yrqppfiqqr says: October 1, 2020 at 7:09 am

    mhnbqqmbklwjcgebnlvjevqcbptdgr

    Reply
  • Leave a reply Cancel reply

    Twitter

    Tweets by @mattbeswick

    About Matt

    This blog is my outlet to write about things I love. Developing a number of successful Facebook games introduced me to start-up life, and I've since moved onto freelance as an SEO consultant before co-founding Aira, a digital agency based in Milton Keynes.

    I'm also a frequent speaker in the SEO / Digital Marketing industry, which started because I hate networking and needed to find a way to get people to come and talk to me.

    The site is constantly evolving, with posts appearing and disappearing, but if you'd like to reach out feel free to get in touch on LinkedIn, Twitter, or Facebook.

    Remember, if you're looking for an SEO Consultant, digital marketing expert or digital strategist... or just fancy a chat... please feel free to get in touch - I'd love to hear from you! I cover Milton Keynes, Northampton, and surrounding areas.

    © Matt Beswick. Digital strategist & SEO consultant. All rights reserved.