AdWords Scripts Experiment: Working Outside of the AdWords User Interface

Every experienced PPC manager knows them: AdWords Scripts. With these small JavaScript tools, minor tasks in AdWords can be automated neatly. This not only makes daily tasks easier, it also saves valuable time. As I began to deal with AdWords Script functions more closely, at some point, I asked myself: is it possible to work with AdWords Scripts outside of the user interface? Is it possible to add data from Google Drive to an account using Google API and AdWords Scripts? I decided to find out. 

For the experiment, I want to automatically add “negative keywords” to a campaign by collecting the keywords, copying them into a small tool, clicking a button, and letting an AdWords Script do the rest – all without using the AdWords user interface.

Why am I using negative keywords for the test? PPC managers regularly check search terms reports in order to keep a lookout for keywords that should be excluded. Normally, these keyword lists are managed in an Excel sheet and regularly uploaded into AdWords. Alternatively, this list can be downloaded from AdWords Editor as a CSV file. It would be even simpler, however, to share a Google Doc among colleagues, add negative keywords, and allow them to automatically be synchronized to the AdWords account. You can do exactly that by using the following AdWords Script.

The idea generators for this experiment are two special AdWords Script functions:

  1. The planned implementation of the Script (scheduling).
  2. Access to Google Drive.

The following should happen in the background:

  • The tool adds negative keywords to Google Drive as a file.
  • The AdWords Script regularly reads the file.
  • The script ads the keywords to one or several campaigns.

Such a tool could appear as follows:


let us first look at the code to upload the keywords as a file in Google Drive. For this, we use the Google Api Client gem. We assume that Drive API access is set up in the Google Developers Console.

require 'google/api_client'

module GoogleApi

  def initialize_client
    client =
    client.authorization.client_id = static_config[:client_id]
    client.authorization.client_secret = static_config[:client_secret]
    client.authorization.scope = static_config[:scope]
    client.authorization.redirect_uri = "http://localhost:3000/oauth_callback"

  def get_authorization_url

  def authorize_client(auth_code)
    client.authorization.code = auth_code

  def static_config



class GoogleDrive

  include GoogleApi

  def initialize
    @client ||= initialize_client

  def upload_file
    file = drive.files.insert
                       'title' => 'Negative_Keywords',
                       'description' => 'negative keywords for google adwords script',
                       'mimeType' => 'text/plain'
    media =, 'text/plain')
    result = client.execute(
                            :api_method => drive.files.insert,
                            :body_object => file,
                            :media => media,
                            :parameters => {
                              'uploadType' => 'multipart',
                              'alt' => 'json'


  attr_reader :client

  def drive
    client.discovered_api('drive', 'v2')


#We use a temp file for now. In actual production code, the keywords
#can probably go in a database
class TempStorage

  PATH = Rails.root.join('tmp', "negative_keywords.txt")

  def self.store_keywords(content), "w") do |file|
      content.each{|line| file.puts(line + "n")}


When registering with Google API, users are given a Client ID and a Client Secret. The code displayed above assumes that both of these values are set up in Rails.config.

Since uploading to Google Drive requires OAuth tokens, we first need to authorize the user before we execute the upload. For this experiment, we store the negative keywords given by the user in a temporary file and then upload it to Google Drive. On the controller level, it may look like this:

class NegativeKeywordsController < ApplicationController

  def new

  def create
    negative_keywords = params['negative_keywords'].split("n")
    TempStorage.store_keywords negative_keywords
    uri =

    redirect_to uri.to_s

  #for now this can be our OAuth callback action
  def upload
    google_drive =
    google_drive.authorize_client params['code']
    redirect_to action: 'new'


Before we continue, it is important that the user also have access to the AdWords account and can run AdWords Scripts. If these requirements are fulfilled, we can write a small script that reads the file in Google Drive and adds the negative keywords to single or multiple campaigns:

function addNegativeKeywordsToAllCampaigns(keywords_csv) {
  //Get all accounts for our MCC
  var accounts = MccApp.accounts().get();

  //Iterate over the accounts
  while (accounts.hasNext()) {
    // Select the client account.
    var account =;;
    // Select all campaigns under the client account
    var campaignIterator = AdWordsApp.campaigns().get();
    if (campaignIterator.hasNext()) {
      var campaign =;
      addNegativeKeywordsToCampaign(campaign, keywords_csv);

function addNegativeKeywordsToCampaign(campaign, keywords_csv) {
  var kws = Utilities.parseCsv(keywords_csv);
  for (var i = 0; i < kws.length; i++) {
    var row = kws[i];
    Logger.log('Adding Keyword: ' + row[0] + ' to Campaign Name: ' + campaign.getName());

function getFileContentsFromDrive() {
  var filesIterator = DriveApp.getFilesByName('Negative_Keywords');
  var file =;
  var data = file.getBlob().getDataAsString()
  return data;

function main() {
  var kws = getFileContentsFromDrive();

As with all AdWords Scripts, the entry point is function main(). First, the script catches up on the content of the file in Google Drive. Then, it gives the keywords over to AdWords, where they can be added to campaigns. If the account is an MCC, you will need to iterate over all accounts as well (as shown in code above).

Finally, we add in the script using the AdWords interface and plan its implementation:


That’s it! The script now runs hourly and checks the file in Google Drive. When new negative keywords are added to the file using the tool, these will then be automatically added to the necessary campaigns – no AdWords user interface required.


The idea was to test whether one can work with AdWords scripts outside of the AdWords user interface. The answer is a resounding “Yes!” The experiment* is just one example of the far-reaching possibilities that AdWords Scripts offer in combination with Google API with regard to automation and time-saving. Further articles about Google API and AdWords API, as well as AdWords scripts, will be available in the months to come.

As a side note, professional campaign management technologies, such as our semantic SEA software camato, work specifically with AdWords API in order to transfer data directly into AdWords accounts. Extensive accounts with several hundred thousand keywords that are regularly developed and expanded are best managed with professional software such as camato.

* Note: the code used here is experimental and not intended for reproduction.

[shareaholic app="share_buttons" id="19406647" link=""]


Maximilian Hainlein

I'm working for crealytics as Social Media and Marketing Manager since 2011. My motto: "It's better to be the needle than the haystack."