AdsApp.​SnippetBuilder

Builder for Snippet objects.

Example usage:

// Create a snippet builder.
var snippetBuilder = AdsApp.extensions().newSnippetBuilder();

// Create a snippet operation.
var snippetOperation = snippetBuilder
  .withHeader("Business")             // required
  .withValues(["foo","bar","baz"])    // required
  .build();

// Optional: examine the outcome. The call to isSuccessful()
// will block until the operation completes.
if (snippetOperation.isSuccessful()) {
  // Get the result.
  var snippet = snippetOperation.getResult();
} else {
  // Handle the errors.
  var errors = snippetOperation.getErrors();
}

Methods:

MemberTypeDescription
build AdsApp.SnippetOperation Creates a Snippet.
withEndDate AdsApp.SnippetBuilder Sets the snippet's end date from either an object containing year, month, and day fields, or an 8-digit string in YYYYMMDD format.
withHeader AdsApp.SnippetBuilder Sets the header of the snippet.
withMobilePreferred AdsApp.SnippetBuilder Sets the snippet's device preference to mobile or clears it.
withSchedules AdsApp.SnippetBuilder Sets the snippet scheduling.
withStartDate AdsApp.SnippetBuilder Sets the snippet's start date from either an object containing year, month, and day fields, or an 8-digit string in YYYYMMDD format.
withValues AdsApp.SnippetBuilder Sets the values of the snippet.

build(buildLegacy)

Creates a Snippet. Returns a SnippetOperation that can be used to get the new snippet (or access any associated errors if creation failed).

Defaults to building the type of snippet that is currently serving. If there are upgraded snippets, then an upgraded snippet will be built by default. If there are only legacy snippets, then a legacy snippet will be built by default. If there are neither, then an upgraded snippet will be built by default.

Arguments:

NameTypeDescription
buildLegacy boolean If true, builds a legacy snippet. If false, builds an upgraded snippet. If unspecified, defaults to building whichever type is currently serving.

Return values:

TypeDescription
AdsApp.SnippetOperation The associated snippet operation.

withEndDate(date)

Sets the snippet's end date from either an object containing year, month, and day fields, or an 8-digit string in YYYYMMDD format. This field is optional.

For instance, snippetBuilder.withEndDate("20130503"); is equivalent to snippetBuilder.withEndDate({year: 2013, month: 5, day: 3});.

The change will fail and report an error if:

  • the given date is invalid (e.g., {year: 2013, month: 5, day: 55}),
  • the start date now comes after the end date, or
  • it's a date in the past

Arguments:

NameTypeDescription
date Object The new snippet end date.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified end date.

withHeader(header)

Sets the header of the snippet. This field is required.

See the structured snippets header translations page for supported localized headers.

Arguments:

NameTypeDescription
header String The header of the snippet.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified header.

withMobilePreferred(isMobilePreferred)

Sets the snippet's device preference to mobile or clears it. This field is optional and defaults to false.

Arguments:

NameTypeDescription
isMobilePreferred boolean Whether or not this snippet should be mobile preferred. If true is passed in, device preference will be set to mobile. If false is passed in, device preference will be set to none.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified mobile preference.

withSchedules(schedules)

Sets the snippet scheduling. Scheduling of a snippet allows you to control the days of week and times of day during which the snippet will show alongside your ads.

Passing in an empty array clears the scheduling field, causing the snippet to run at all times.

The following example sets the snippet to run on Mondays and Tuesday from 8:00 to 11:00.

 var mondayMorning = {
   dayOfWeek: "MONDAY",
   startHour: 8,
   startMinute: 0,
   endHour: 11,
   endMinute: 0
 };
 var tuesdayMorning = {
   dayOfWeek: "TUESDAY",
   startHour: 8,
   startMinute: 0,
   endHour: 11,
   endMinute: 0
 };

snippetBuilder.withSchedules([mondayMorning, tuesdayMorning]);

Arguments:

NameTypeDescription
schedules AdsApp.ExtensionSchedule[] The new snippet schedules.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified schedules.

withStartDate(date)

Sets the snippet's start date from either an object containing year, month, and day fields, or an 8-digit string in YYYYMMDD format. This field is optional.

For instance, snippetBuilder.withStartDate("20130503"); is equivalent to snippetBuilder.withStartDate({year: 2013, month: 5, day: 3});.

The change will fail and report an error if:

  • the given date is invalid (e.g., {year: 2013, month: 5, day: 55}),
  • the given date is after the snippet's end date,

Arguments:

NameTypeDescription
date Object The new snippet start date.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified start date.

withValues(values)

Sets the values of the snippet. At least three values, and at most ten values should be added. This field is required.

Arguments:

NameTypeDescription
values String[] The values of the snippet.

Return values:

TypeDescription
AdsApp.SnippetBuilder A snippet builder with the specified values.