Sitelinks

Create sitelinks

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

    // 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 an ad group
    var adGroupIterator = AdWordsApp.adGroups()
        .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
        .get();
    if (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      adGroup.addSitelink(newSitelink);
    }
}

Get sitelinks for a campaign

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 final URL: ' + sitelink.urls().getFinalUrl() +
          ', mobile preferred: ' + sitelink.isMobilePreferred());
    }
    Logger.log(
        'Total count of sitelinks: ' + sitelinksIterator.totalNumEntities());
  }
}

Get sitelink stats for a campaign

function getSitelinkStats() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Retrieve the campaign's sitelinks. Retrieving an ad group'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 schedule for sitelinks in a campaign

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

Send feedback about...

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