광고그룹

광고그룹 추가

function addAdGroup(campaignName, adGroupName, defaultCpc = 1.2) {
  const campaignIterator = AdsApp.campaigns()
      .withCondition(`campaign.name = "${campaignName}"`)
      .get();
  if (!campaignIterator.hasNext()) {
    throw new Error(`No campaign with name "${campaignName} found`);
  }

  const campaign = campaignIterator.next();
  return campaign.newAdGroupBuilder()
      .withName(adGroupName)
      .withCpc(defaultCpc)
      .build();
}

모든 광고그룹 가져오기

function getAllAdGroups() {
  // AdsApp.adGroups() will return all ad groups that are not removed by
  // default.
  const adGroupIterator = AdsApp.adGroups().get();
  console.log('Total adGroups found : ' + adGroupIterator.totalNumEntities());
  return adGroupIterator;
}

이름별로 광고그룹 가져오기

function getAdGroupByName(name) {
  const adGroupIterator = AdsApp.adGroups()
      .withCondition(`ad_group.name = "${name}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${name}" found`);
  }

  const adGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
  }

  return adGroup;
}

광고그룹의 기본 CPC 입찰가 업데이트

function setAdGroupCpc(name, cpc) {
  const adGroupIterator = AdsApp.adGroups()
      .withCondition(`ad_group.name = "${name}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${name}" found`);
  }

  const adGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
  }

  adGroup.bidding().setCpc(cpc);
}

광고그룹 통계 가져오기

function getAdGroupStats(name, dateRange = 'LAST_MONTH') {
  const adGroupIterator = AdsApp.adGroups()
      .withCondition(`ad_group.name = "${name}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${name}" found`);
  }

  const adGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
  }

  // You can get stats for a custom date range, or, as in this example, a predefined date range.
  // A list of valid predefined date ranges is available at
  // https://developers.google.com/google-ads/api/docs/query/date-ranges#predefined_date_range
  const stats = adGroup.getStatsFor(dateRange);
  console.log(`${adGroup.getName()}, ${stats.getClicks()}, ${stats.getImpressions()}`);
  return stats;
}

광고그룹 일시중지

function pauseAdGroup(name) {
  const adGroupIterator = AdsApp.adGroups()
      .withCondition(`ad_group.name = "${name}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${name}" found`);
  }

  const adGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
  }

  adGroup.pause();
}

광고그룹의 기기 입찰 조정 가져오기

function getAdGroupBidModifiers(name) {
  const adGroupIterator = AdsApp.adGroups()
      .withCondition(`ad_group.name = "${name}"`)
      .get();

  if (!adGroupIterator.hasNext()) {
    throw new Error(`No ad group with name "${name}" found`);
  }

  const adGroup = adGroupIterator.next();
  if (adGroupIterator.totalNumEntities() > 1) {
    console.warn(`Multiple ad groups named "${name}" found.
Using the one from campaign "${adGroup.getCampaign().getName()}".`);
  }

  return {
    HighEndMobile: adGroup.devices().getMobileBidModifier(),
    Tablet: adGroup.devices().getTabletBidModifier(),
    Desktop: adGroup.devices().getDesktopBidModifier(),
  };
}