This is the legacy documentation for Google Ads scripts. Go to the current docs.

Snippets

Stay organized with collections Save and categorize content based on your preferences.

Create snippets


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

  // Add snippet to a campaign
  var campaignIterator = AdsApp.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 = AdsApp.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 = AdsApp.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 = AdsApp.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 = AdsApp.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;
      }
    }
  }
}