广告类型

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

恣意创作

脚本可以在 AdGroup 实例上使用 newAd() 方法制作广告。这将返回一个 AdBuilderSpace,可为支持的广告类型创建构建器

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

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();
}

Reporting(报告)

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

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;
  ...
}

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