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 0

AdWords MCC Scripts – Beginner’s Guide

By Matt Beswick @mattbeswick ·

When working with multiple clients there are very often tasks that you need to do – whether it’s every week or every month – that are mundane, repetitive, and could seemingly be done by a robot. Happily, if that really is the case, scripting is very often the right answer. Keep in mind that although automation can be incredibly powerful when done well, it isn’t an excuse to spend no time analysing and manually updating your campaigns. This post is an intro to MCC scripting and, although straightforward, assumes that you have at least some passing knowledge in javascript.

What we need

For this post we’re going to take a really simple example of a weekly report. This needs to be generated for 3 accounts within our MCC, and is a simple set of to level stats (overall CTR, CPC, etc.) that we then send onto the client manually every Friday. We generate a more detailed monthly report, which is done manually, but for this weekly email there’s no need for any insights – it’s a very straightforward “here’s how the account’s performed this week”.

Creating our first script

First things first, we log in to our MCC account and go to the “scripts” menu. Once we’re there we click the button to “add a new script” and we immediately end up on a screen that lets us create our code.

From here we need to name our script and authorise it to have access to our AdWords account. Let’s do that straight away to keep life simple and make sure that as we want to test things out we can do. You’ll also notice that there’s a single function that’s already been created for us.

With the formalities out of the way, it’s time to start building our code. Unlike normal AdWords scripts, an MCC script needs to be told which accounts to access first, and then what to do with each one afterwards. This could be done by specifying the account id(s) or, and this is my preference, by using account labels. The reason I prefer the latter is that if you want to add or remove accounts you don’t need to edit your script – you simply update the labels and the script does the rest.

So, here’s our first code snippet to do just that:

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();
}

Nice and simple. We specify the label we want to look for, and then “get” any accounts that have a label containing that name. These accounts are then stored within the “accountIterator” variable which we’ll use later.

If you’d rather specify by account id, this code would change to the following:

function main() {
// Specify the label name to look for
var labelName = 'Friday Report';
// Select the accounts to be processed.
var accountIterator = MccApp.accounts()
.withIds(['123-456-7890', '223-456-7890', '323-456-7890'])
.get();
}

So far so good but all we’ve done so far is select a few account. Now it’s time to do something with them.

Iterating through accounts

Moving on from account selection, we now want to create a loop that goes through each one and then does something. Let’s start with that loop:

// 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
}

So that’s moved us on slightly – we’ve now gone from having a list of accounts to being able to select each one, but we’re still not actually doing anything. Let’s grab some data.

// Generate account level query
var report = AdWordsApp.report(
'SELECT Clicks, Impressions, Cost, AllConversions, AllConversionValue, ViewThroughConversions, 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'];
}

We have two option for generating this data – we could access to the account, check the stats, and do some calculations… or we could use the AdWords reporting API to get all of our data really quickly. There are a load of reports that you can access, doing so via an AWQL query (which is basically a SQL query), the documentation for which can be found here.

Knowing it’s working

So far we’ve written some code but have no idea at all whether or not we’ve been able to access the data. Here’s where logging comes into play. Adding this line below or AWQL query will output results into the console so we can see what’s going on:

Logger.log(account.getName() + ": Clicks " + clicks + ', Impressions ' + impressions + ' CTR ' + ctr + ', Cost £' + cost);

If we put all of that together, here’s our code:

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();

// 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, AllConversions, AllConversionValue, ViewThroughConversions, 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);

}

}

Finally, make sure you’ve applied the “Friday Report” label to at least one account, click the “preview” button, go to the “log” tab, and here’s what we see:

Done! Well, not quite…

That’s really useful but it means that we have to log into the account each week to run the report at a certain time. Fortunately it’s possible to send emails from AdWords scripts as well as being able to schedule them, which is something I cover in my “How to send email with an AdWords script” tutorial.

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

    How to send email with an AdWords script

  • SEO and PPC

    The Big Guide to Outsourcing

  • SEO and PPC

    SearchLove London 2014: The Lazy SEO

No Comments

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.