Campaigns

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

Get all campaigns

function getAllCampaigns() {
  // AdsApp.campaigns() will return all campaigns that are not removed by
  // default.
  const campaignIterator = AdsApp.campaigns().get();
  console.log(`Total campaigns found : ${campaignIterator.totalNumEntities()}`);
  return campaignIterator;
}

Get a campaign by name

function getCampaignByName(name) {
  const campaignIterator = AdsApp.campaigns()
      .withCondition(`campaign.name = "${name}"`)
      .get();
  if (campaignIterator.hasNext()) {
    const campaign = campaignIterator.next();
    console.log(`Campaign Name: ${campaign.getName()}`);
    console.log(`Enabled: ${campaign.isEnabled()}`);
    console.log(`Bidding strategy: ${campaign.getBiddingStrategyType()}`);
    console.log(`Ad rotation: ${campaign.getAdRotationType()}`);
    console.log(`Start date: ${formatDate(campaign.getStartDate())}`);
    console.log(`End date: ${formatDate(campaign.getEndDate())}`);
    return campaign;
  } else {
    throw new Error(`No campaign named "${name}" found`);
  }
}
function formatDate(date) {
  function zeroPad(number) { return Utilities.formatString('%02d', number); }
  return (date == null) ? 'None' : zeroPad(date.year) + zeroPad(date.month) +
      zeroPad(date.day);
}

Get a campaign's stats

function getCampaignStats(name) {
  const campaignIterator = AdsApp.campaigns()
      .withCondition(`campaign.name = "${name}"`)
      .get();
  if (campaignIterator.hasNext()) {
    const campaign = campaignIterator.next();
    // You can also request reports for pre-defined date ranges. See
    // https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaign#getStatsFor_1,
    // DateRangeLiteral section for possible values.
    const stats = campaign.getStatsFor('LAST_MONTH');
    console.log(`${campaign.getName()}: ${stats.getClicks()} clicks, ${stats.getImpressions()} impressions`);
    return stats;
  } else {
    throw new Error(`No campaign named "${name}" found`);
  }
}

Pause a campaign

function pauseCampaign(name) {
  const campaignIterator = AdsApp.campaigns()
      .withCondition(`campaign.name = "${name}"`)
      .get();
  if (campaignIterator.hasNext()) {
    const campaign = campaignIterator.next();
    campaign.pause();
  } else {
    throw new Error(`No campaign named "${name}" found`);
  }
}

Get a campaign's device bid modifiers

function getCampaignBidModifiers(name) {
  const campaignIterator = AdsApp.campaigns()
      .withCondition(`campaign.name = "${name}"`)
      .get();
  if (campaignIterator.hasNext()) {
    const campaign = campaignIterator.next();
    console.log('Campaign name: ' + campaign.getName());

    const platforms = {};
    for (const platform of campaign.targeting().platforms()) {
      console.log(`${platform.getName()} bid modifier: ${platform.getBidModifier()}`);
      platforms[platform.getName()] = platform.getBidModifier();
    }
    return platforms;
  } else {
    throw new Error(`No campaign named "${name}" found`);
  }
}