Sudheer Kiran

Sudheer Kiran

Digital Marketing Consultant

AdWords Scripts: Create, Get and Schedule Your Ad Extensions

Do you ever heard about AdWords Scripts which gives you some amazing features to save your time and make your Ads so accurate?

What is AdWords Scripts?

AdWords scripts provide a way to programmatically control your AdWords data using simple JavaScript.

Why AdWords Scripts?

Saves Time: AdWords Scripts let you automate the tasks you frequently do while monitoring and managing AdWords accounts.

Customizable: AdWords Scripts are easy to customize to your own specifications so they will do exactly what you want.

Automated Scheduling: AdWords Scripts let you automate management of an AdWords account.

Where to Add AdWords Scripts?

Click on Bulk Operations in left side of your AdWords account

 AdWords Bulk Operations

 

Click on Scripts under the Bulk Operations menu

AdWords Create Scripts

 

 Click on Script button to create new script

  AdWords New Script

Add your Adwords script code into the editor area

 AdWords Scripts

Now you understand the procedure to add your AdWords scripts.

To help you get started with Adwords Scripts I’m starting with below Ad Extensions.

Ad Extensions

  • Mobile Apps
  • Phone Numbers
  • Sitelinks

Now, Let’s see some examples which you can customize and use it for your AdWords account. There are lot of ready-made scripts which needs a little customization according to your needs.

Here is some useful sample Adwords script for Ad Extensions for Beginers.

 

Create Extensions to a Campaign:

Mobile Apps:

function addMobileApp() {
  var campaignIterator = AdWordsApp.campaigns()
         .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
         .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // See https://support.google.com/adwords/answer/2402582 for details on how
    // to obtain applications specific store id
    var newMobileApp = AdWordsApp.extensions().newMobileAppBuilder()
        .withAppId('store specific app id')                 // required
        .withStore('Android')                               // required
        .withLinkText('Download Android App Here')          // required
        .withLinkUrl('http://wwww.example.com/androidApp')  // required
        .withStartDate({day: 12, month: 9, year: 2013})     // optional
        .create();

    // Add mobile app to campaign. Adding mobile app to adgroup is similar.
    campaign.addMobileApp(newMobileApp);
  }
}

Phone Number:

function addPhoneNumber() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var phoneNumberBuilder = AdWordsApp.extensions().newPhoneNumberBuilder();
    var newPhoneNumber = phoneNumberBuilder
        .withCountry('US')
        .withPhoneNumber('INSERT_PHONE_NUMBER_HERE')
        .withCallOnly(false)
        .create();

    // Add phone number to campaign. Adding phone number to adgroup is
    // similar.
    campaign.addPhoneNumber(newPhoneNumber);
  }
}

Sitelinks:

function createSitelink() {
    var sitelinkBuilder = AdWordsApp.extensions().newSitelinkBuilder();
    var newSitelink = sitelinkBuilder
        .withLinkText('Music')
        .withLinkUrl('http://www.example.com/Music')
        .withMobilePreferred(true)
        .create();

    // Add siteLink to a campaign
    var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
    if (campaignIterator.hasNext()) {
      var campaign = campaignIterator.next();
      campaign.addSitelink(newSitelink);
    }

    // Add siteLink to adgroup
    var adGroupIterator = AdWordsApp.adGroups()
         .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
         .get();
    if (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      adGroup.addSitelink(newSitelink);
    }
}

Get Extensions for a Campaign:

Mobile Apps:

function getMobileAppsForCampaign() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var mobileAppsIterator = campaign.extensions().mobileApps().get();

    while (mobileAppsIterator.hasNext()) {
      var mobileApp = mobileAppsIterator.next();
      Logger.log('Mobile app id: ' + mobileApp.getAppId() + ', link text: ' +
          mobileApp.getLinkText() + ', link url: ' + mobileApp.getLinkUrl());
    }

    Logger.log(mobileAppsIterator.totalNumEntities() +
        ' mobile apps in the account');
  }
}

Phone Numbers:

function getPhoneNumbersForCampaign() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var phoneNumberIterator = campaign.extensions().phoneNumbers().get();

    while (phoneNumberIterator.hasNext()) {
      var phoneNumber = phoneNumberIterator.next();
      Logger.log('Phone number : ' + phoneNumber.getPhoneNumber());
    }

    Logger.log('Number of phone number extensions applied to this campaign: ' +
        phoneNumberIterator.totalNumEntities());
  }
}

Sitelinks:

function getSitelinksForCampaign() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var sitelinksIterator = campaign.extensions().sitelinks().get();
    while (sitelinksIterator.hasNext()) {
      var sitelink = sitelinksIterator.next();
      Logger.log('Sitelink text: ' + sitelink.getLinkText() +
          ', Sitelink url: ' + sitelink.getLinkUrl() +
          ', mobile preferred: ' + sitelink.isMobilePreferred());
    }
    Logger.log('Total count of sitelinks: ' +
        sitelinksIterator.totalNumEntities());
  }
}

 

Get Stats for Extensions in a Campaign:

Mobile Apps:

function getMobileAppStats() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Retrieve the campaign's mobile apps. Retrieving an adgroup's mobile
    // apps is similar.
    var appsIterator = campaign.extensions().mobileApps().get();

    while (appsIterator.hasNext()) {
      var apps = appsIterator.next();
      // You can also request reports for pre-defined date ranges. See
      // https://developers.google.com/adwords/api/docs/guides/awql,
      // DateRangeLiteral section for possible values.

      var stats = apps.getStatsFor('LAST_MONTH');
      Logger.log(sitelink.getLinkText() + ', ' + stats.getClicks() + ', ' +
          stats.getImpressions());
    }
  }
}

Phone Numbers:

function getPhoneNumberStats() {
  // To retrieve stats for the phone number over all campaigns you can use
  //    AdWordsApp.extensions().phoneNumbers()
  //    directly instead of filtering by a specific campaign.
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Retrieve the campaign's phone numbers. Retrieving an adgroup's phone
    // numbers is similar.

    var campaignPhoneNumberIterator = campaign.extensions()
                                              .phoneNumbers()
                                              .get();

    while (campaignPhoneNumberIterator.hasNext()) {
      var campaignPhoneNumber = campaignPhoneNumberIterator.next();

      // You can also request reports for pre-defined date ranges. See
      // https://developers.google.com/adwords/api/docs/guides/awql,
      // DateRangeLiteral section for possible values.
      var stats = campaignPhoneNumber.getStatsFor('LAST_MONTH');
      Logger.log(campaignPhoneNumber.getPhoneNumber() + ', ' +
          stats.getClicks() + ', ' + stats.getImpressions());
    }
  }
}

Sitelinks:

function getSitelinkStats() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Retrieve the campaign's phone numbers. Retrieving an adgroup's
    // sitelinks is similar.
    var sitelinksIterator = campaign.extensions().sitelinks().get();

    while (sitelinksIterator.hasNext()) {
      var sitelink = sitelinksIterator.next();

      // You can also request reports for pre-defined date ranges. See
      // https://developers.google.com/adwords/api/docs/guides/awql,
      // DateRangeLiteral section for possible values.
      var stats = sitelink.getStatsFor('LAST_MONTH');
      Logger.log(sitelink.getLinkText() + ', ' + stats.getClicks() + ', ' +
          stats.getImpressions());
    }
  }
}

Set Schedules in a Campaign

Phone Numbers:

function setPhoneNumberSchedule() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var phoneNumberIterator = campaign.extensions().phoneNumbers().get();
    var phoneNumber = null;

    // Scan through the phone numbers to locate the one with matching number.
    while (phoneNumberIterator.hasNext()) {
      phoneNumber = phoneNumberIterator.next();
      if (phoneNumber.getPhoneNumber() == '"INSERT_PHONE_NUMBER_HERE"') {
        // Set phone number schedule to run only on Mondays and Tuesdays, 9 AM
        // to 6 PM. You can follow a similar approach to set schedules for
        // other ad extension types.
        var monday = {
          dayOfWeek: 'MONDAY',
          startHour: 9,
          startMinute: 0,
          endHour: 18,
          endMinute: 0
        };
        var tuesday = {
          dayOfWeek: 'TUESDAY',
          startHour: 9,
          startMinute: 0,
          endHour: 18,
          endMinute: 0
        };

        phoneNumber.setSchedules([monday, tuesday]);
        break;
      }
    }
  }
}

Sitelinks:

function setSitelinkSchedule() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var sitelinksIterator = campaign.extensions().sitelinks().get();
    var sitelink = null;

    // Scan through the sitelinks to locate the one with text as Music.
    while (sitelinksIterator.hasNext()) {
      sitelink = sitelinksIterator.next();
      if (sitelink.getLinkText() == 'Music') {
        // Set sitelink schedule to run only on Mondays and Tuesdays, 9 AM to
        // 6 PM. You can follow a similar approach to set schedules for other
        // ad extension types.
        var monday = {
          dayOfWeek: 'MONDAY',
          startHour: 9,
          startMinute: 0,
          endHour: 18,
          endMinute: 0
        };

        var tuesday = {
          dayOfWeek: 'TUESDAY',
          startHour: 9,
          startMinute: 0,
          endHour: 18,
          endMinute: 0
        };

        sitelink.setSchedules([monday, tuesday]);

        break;
      }
    }
  }
}

There are many ready-made scripts to automate and customize your AdWords reports, to customize your Ads, to interact with external data sources and to create alerts to notify you by email and much more..! I will come up with few more examples in my next post.

Leave a Reply