Drafts and Experiments

Create a draft campaign

function createDraft() {
  var campaign = AdsApp.campaigns()
      .withCondition("Name = 'INSERT_CAMPAIGN_NAME_HERE'")
      .get()
      .next();

  var draftBuilder = campaign.newDraftBuilder()
      .withName("INSERT_NEW_DRAFT_NAME_HERE")
      .build();

  var draft = draftBuilder.getResult();
}

Get draft campaigns

function getDrafts() {
  // Get all drafts.
  var drafts = AdsApp.drafts().get();

  Logger.log(drafts.totalNumEntities());

  while (drafts.hasNext()) {
    var draft = drafts.next();
    Logger.log("Draft: " + draft.getName());
  }

  // Get a specific draft.
  var campaignIterator = AdsApp.drafts()
      .withCondition("DraftName = 'INSERT_DRAFT_NAME'")
      .get();

  while (campaignIterator.hasNext()) {
    Logger.log(campaignIterator.next().getName());
  }
}

Create an experiment

function createExperiment() {
  var draft = AdsApp.drafts()
      .withCondition("DraftName = INSERT_DRAFT_NAME")
      .get()
      .next();

  var experimentBuilder = draft.newExperimentBuilder();

  experimentBuilder.withName("INSERT_NEW_EXPERIMENT_NAME_HERE")
      .withTrafficSplitPercent(50)
      .startBuilding();
}

Get experiments

function getExperiments() {
  // Get all experiments.
  var exps = AdsApp.experiments().get();

  Logger.log(exps.totalNumEntities());

  while (exps.hasNext()) {
    var exp = exps.next();
    Logger.log("Experiment: " + exp.getName());
  }

  // Get specific experiment.
  var campaignIterator = AdsApp.experiments()
      .withCondition("Name = 'INSERT_EXPERIMENT_NAME'")
      .get();

  while (campaignIterator.hasNext()) {
    Logger.log(campaignIterator.next().getName());
  }
}

Send feedback about...

Google Ads scripts
Google Ads scripts
Need help? Visit our support page.