Snippets

Create snippets


function createSnippet() {
  var snippetBuilder = AdWordsApp.extensions().newSnippetBuilder();
  var newSnippet = snippetBuilder
      .withHeader('Brands')
      .withValues(['Nest', 'Waymo','Google'])
      .withMobilePreferred(true)
      .build()
      .getResult();

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

  // Add snippet to an ad group
  var adGroupIterator = AdWordsApp.adGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (adGroupIterator.hasNext()) {
    var adGroup = adGroupIterator.next();
    adGroup.addSnippet(newSnippet);
  }
}

Get snippets for a campaign

function getSnippetsForCampaign() {
  var campaignIterator = AdWordsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var snippetsIterator = campaign.extensions().snippets().get();
    while (snippetsIterator.hasNext()) {
      var snippet = snippetsIterator.next();
      Logger.log('Snippet header: ' + snippet.getHeader() +
          ', Snippet values: ' + snippet.getValues() +
          ', mobile preferred: ' + snippet.isMobilePreferred());
    }
    Logger.log(
        'Total count of snippets: ' + snippetsIterator.totalNumEntities());
  }
}

Get snippet stats for a campaign

function getSnippetStats() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
        .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    // Retrieve the campaign's snippets. Retrieving an ad group's
    // snippets is similar.
    var snippetsIterator = campaign.extensions().snippets().get();

    while (snippetsIterator.hasNext()) {
      var snippet = snippetsIterator.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 = snippet.getStatsFor('LAST_MONTH');
      Logger.log(snippet.getHeader() + ', ' + stats.getClicks() + ', ' +
          stats.getImpressions());
    }
  }
}

Set schedule for snippets in a campaign

function setSnippetSchedule() {
  var campaignIterator = AdWordsApp.campaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var snippetsIterator = campaign.extensions().snippets().get();
    var snippet = null;

    // Scan through the snippets to locate the one with header set as Brands.
    while (snippetsIterator.hasNext()) {
      snippet = snippetsIterator.next();
      if (snippet.getHeader() == 'Brands') {
        // Set snippet 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
        };

        snippet.setSchedules([monday, tuesday]);

        break;
      }
    }
  }
}

Send feedback about...

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