AdWords Scripts Bulks Up With Bulk File Uploads

AdWords scripts now support bulk uploads to your accounts, and columnist Russell Savage shows how you can use this new functionality to create campaigns, modify accounts and more.

Chat with SearchBot

benchpress-1200x675

Managers of large AdWords accounts, rejoice! Google recently added the ability to perform bulk uploads using AdWords scripts. This means you can apply massive changes to your MCC without having to write the logic yourself or log into each account you manage. To learn more, check out the AdWords help section on bulk uploads.

Not only does this save you time, but it also solves a long standing issue with scripts: creating campaigns. Using the bulk upload feature and a simple spreadsheet, you can create as many campaigns as you want.

An example of a bulk upload sheet to create campaigns

An example of a bulk upload sheet used to create campaigns

By combining Google Drive and AdWords scripts, we can build an easy system that allows us to automatically apply changes to our accounts using CSV files.

If you are not using Google Drive on your computer, now would be a good time to download and install it. The scripts shown here will still work if you don’t, but it makes opening and updating the CSV files on your computer much easier.

The first thing we are going to need is a simple folder structure that helps us keep everything in order. I find that a root folder specifically for managing bulk uploads containing subfolders for each account you are managing works pretty well.

An example folder structure

This script will create the following folder structure in your Google Drive account

When you want to make changes to your account, simply drop the spreadsheet into that account’s “To Process” folder and the script will pick it up on the next run, process it, and move it to the “Processed” folder.

We can write an MCC level script to accomplish this. Let’s start with a simple function to help us create folders.

// name: The name of the folder to find or create
// folder: The parent folder. Pass null to look in the root folder
function findOrCreateFolder(name,folder) {
  if(!folder) { folder = DriveApp.getRootFolder(); }
  var fileIter = folder.getFoldersByName(name);
  if(fileIter.hasNext()) {
    return fileIter.next();
  } else {
    return folder.createFolder(name);
  }
}

This function will look for the folder we need and if it can’t find it, the function will create it for us.

Now we can write our main function to create and monitor the folders for any new files. If the script finds any, it will load the files into the account using the new bulk upload feature.

// This is the name of the folder to hold the account data
var ROOT_FOLDER_NAME = 'AdWords Management';
// This folder will hold the bulk uploads we have already processed
var PROCESSED_FOLDER_NAME = 'Processed';
// This folder will hold the bulk uploads we need to process
var TO_PROCESS_FOLDER_NAME = 'To Process';

function main() {
  var accountIter = MccApp.accounts().get();
  var successfulUploads = [];
  while(accountIter.hasNext()) {
    var account = accountIter.next();
    MccApp.select(account);
    
    var rootFolder = findOrCreateFolder(ROOT_FOLDER_NAME,null);
    // The folder name for each account will be in the format  
    var accountFolderName = AdWordsApp.currentAccount().getCustomerId() + 
                      ' ' + AdWordsApp.currentAccount().getName();
    
    // This will create the folder structure we need
    var accountFolder = findOrCreateFolder(accountFolderName,rootFolder);
    var toProcessFolder = findOrCreateFolder(TO_PROCESS_FOLDER_NAME,accountFolder);
    var processedFolder = findOrCreateFolder(PROCESSED_FOLDER_NAME,accountFolder);
    
    // Now we look in the "To Process" folder for any new CSV files
    var filesToProcessIter = toProcessFolder.getFilesByType(MimeType.CSV);
    while(filesToProcessIter.hasNext()) {
      var file = filesToProcessIter.next();
      // Change the following line to use .apply() instead of .preview()
      // once you are satisfied things are working correctly. When using .preview(),
      // you will need to log into your account to apply the change.
      AdWordsApp.bulkUploads().newFileUpload(file).preview();
      // To move a file, we add it to the new folder and remove it from the old one
      processedFolder.addFile(file);
      toProcessFolder.removeFile(file);
      // We record some information here to help us with a notification email.
      successfulUploads.push({customerId:AdWordsApp.currentAccount().getCustomerId(),
                              accountName:AdWordsApp.currentAccount().getName(),
                              filename:file.getName()});
    }
  }
  // Send the notification email
  notifySuccessfulUploads(successfulUploads);
}

As you can see, we start by iterating through all the accounts in the MCC, building out the folder structure (if it is a new account or if this is the first run), and then calling bulk upload to load the file into the account.

One thing to note is that in the code above, I am simply using the .preview() command to load the file and generate a preview of the changes in the AdWords UI. This doesn’t actually make any changes to the account. Once you are satisfied that everything is working correctly, you can switch over to using the .apply() function which will automatically apply the changes to the account.

Another good thing about bulk uploads is that the changes are applied to your account in the background while the script keeps running. According to this post in the forums, the bulk upload will timeout after two hours of processing.

It’s always nice to get notified when there are changes made to your account, so I added a function to send an email to notify me when files are uploaded. The code was simply modified from the error notification in my blog post about running executeInParallel on more than 50 accounts.

// These can go at the top of the script with the other global variables
var SCRIPT_NAME = 'Bulk Uploader';
var TIMEZONE = 'PST';
var TODAY_STR = Utilities.formatDate(new Date(), TIMEZONE, 'yyyy-MM-dd');
var NOTIFY = ['[email protected]'];

// successfulUploads should be an array of objects containing
// [{customerId:'',accountName:'',filename:''},...]
function notifySuccessfulUploads(successfulUploads) {
  if(!successfulUploads || successfulUploads.length == 0) { return; }
  if(typeof NOTIFY == 'undefined') { throw 'NOTIFY is not defined.'; }
  var subject = SCRIPT_NAME+' - Successful Uploads - '+TODAY_STR;
   
  var htmlBody = 'The following files were uploaded to your accounts.<br / >';
  htmlBody += 'Log in to AdWords: https://goo.gl/7mS6A';
  var body = htmlBody;
  htmlBody += '<br / ><br / >';
  htmlBody += '<table border="1" width="95%" style="border-collapse:collapse;">' +
              '<tr><td>Account Id</td><td>Account Name</td><td>Filename</td></tr>';
  for(var i in successfulUploads) {
    htmlBody += '<tr><td>'+ successfulUploads[i].customerId +
      '</td><td>' + successfulUploads[i].accountName +
      '</td><td>' + successfulUploads[i].filename + '</td></tr>';
  }
  htmlBody += '</table>';
  var options = { htmlBody : htmlBody };
  for(var i in NOTIFY) {
    Logger.log('Sending email to: '+NOTIFY[i]+' with subject: '+subject);
    MailApp.sendEmail(NOTIFY[i], subject, body, options);
  }
}

And that does it. When we put it all together and schedule the script to run hourly, we should have the ability to place bulk upload spreadsheets into the “To Process” folder and have the changes automatically applied to our account.

This merely scratches the surface of what you can do with this new feature. One of the other cool features is the ability to upload offline conversion data. You can also easily duplicate an entire account and target a different location.

Bulk file uploads will also help you avoid timeout errors on many of your scripts because they process in the background and have different timeouts. What other automations can you think of using the new bulk upload feature?

One Last Note: Anyone attending SMX Munich on March 17th and interested in some hands-on training using scripts should know I will be running a workshop as well as speaking at the conference, and I’d love for you to join me. In the workshop, we will learn how to build AdWords scripts from scratch, writing all the code yourself. We will also have time to debug any problems you have in your existing scripts. I encourage you to sign up if you are interested.


Opinions expressed in this article are those of the guest author and not necessarily Search Engine Land. Staff authors are listed here.


About the author

Russell Savage
Contributor
Russell Savage is an Application Engineer for Cask Data and loves tinkering with marketing data and automation in his spare time. He is the creator of FreeAdWordsScripts.com, where he posts AdWords Scripts for anyone to use.

Get the must-read newsletter for search marketers.