Phone Numbers

Add a phone number to a campaign

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')
        .build()
        .getResult();

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

Get all phone numbers for a campaign

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

Get stats for all phone numbers in a campaign

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

Set schedules for 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;
      }
    }
  }
}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.