广告类型

Google Ads 支持各种类型的广告,例如文字广告、图片广告和移动广告。 本指南介绍了如何使用 Google Ads 脚本创建、检索和报告广告。有关 Google Ads 支持的所有广告类型的概述,请参阅 API 指南

创建

脚本可以使用 AdGroup 实例上的 newAd() 方法创建广告。此方法会返回一个 AdBuilderSpace,用于为支持的广告类型创建 builder

下面的代码段演示了如何制作加大型文字广告:

let adOperation = adGroup.newAd().expandedTextAdBuilder()
    .withHeadlinePart1("First headline part")
    .withHeadlinePart2("Second headline part")
    .withDescription("Ad description")
    .withFinalUrl("http://www.example.com")
    .withPath1("path1") // optional
    .withPath2("path2") // optional
    .build();

检查

与所有广告类型相关联的某些信息可立即通过 Ad 获取,例如广告的 ID 和审批状态。此外,您还可以暂停、启用或移除任何广告。

如需访问特定于广告类型的字段(例如加大型文字广告的说明),请使用 asType() 方法创建 AdViewSpace。这可提供对 Ad 的扩展版本的访问权限,该版本会公开特定于类型的方法。

以下代码段用于获取每个加大型文字广告的说明:

const iterator = AdsApp.ads().withCondition("Type = EXPANDED_TEXT_AD").get();
while (iterator.hasNext()) {
  let ad = iterator.next();
  let expandedTextAd = ad.asType().expandedTextAd();
  let description = expandedTextAd.getDescription();
}

请注意,条件 Type = EXPANDED_TEXT_AD 可确保迭代器中的每个广告都是加大型文字广告。尝试查看类型不正确的广告会导致错误,从而停止脚本的执行,因此仅当广告的类型已知时,查看特定于类型的字段才非常重要。

以下代码段展示了如何使用 Ad.isType() 方法来确定广告是否属于正确的类型:

if (ad.isType().expandedTextAd()) {
  let expandedTextAd = ad.asType().expandedTextAd();
  let headlinePart1 = expandedTextAd.getHeadlinePart1();
  let headlinePart2 = expandedTextAd.getHeadlinePart2();
}

报告

除了常规统计信息外,ad_group_ad 视图还可用于查询特定类型的广告字段,例如 ad_group_ad.expanded_text_ad.headline_part1。 以下代码段展示了如何检索所有在广告标题 1 中包含“折扣促销”的加大型文字广告的统计信息:

const results = AdsApp.search(
  "SELECT ad_group_ad.ad_group.id, " +
         "ad_group_ad.id, " +
         "ad_group_ad.expanded_text_ad.headline_part1, " +
         "ad_group_ad.expanded_text_ad.headline_part2, " +
         "metrics.clicks, " +
         "metrics.impressions, " +
         "metrics.cost" +
  "FROM ad_group_ad " +
  "WHERE ad_group_ad.expanded_text_ad.headline_part1 = 'Discount Sales' " +
    "AND segments.date DURING LAST_7_DAYS");

while (results.hasNext()) {
  let row = results.next();
  let headlinePart1 = row.adGroupAd.expandedTextAd.headlinePart1;
  let headlinePart2 = row.adGroupAd.expandedTextAd.headlinePart2;
  ...
}

如需详细了解如何在脚本中生成报告,请参阅报告指南