Google 広告スクリプトでは、ショッピング キャンペーンの一部を管理できます。スクリプトを使用すると、既存のショッピング キャンペーンの操作、商品グループ階層の作成と管理、ショッピング レポートの実行を行うことができます。ただし、スクリプトを使用してショッピング キャンペーンを作成したり、キャンペーン単位でショッピング プロパティ(キャンペーンの優先順位、商品フィルタなど)を設定したり、Merchant Center アカウントをリンクしたりすることはできません。
ショッピング キャンペーンと広告グループを取得する
ショッピング キャンペーンは、AdsApp
オブジェクトの shoppingCampaigns
コレクションを通じて利用できます。スクリプトを使用して通常どおりに取得できます。
const campaignName = "My first shopping campaign";
const campaignIterator = AdsApp.shoppingCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
for (const campaign of campaignIterator) {
...
}
キャンペーンを取得したら、同様の方法で広告グループを取得できます。これは、キャンペーンとその広告グループの両方に対してアクションを実行する必要がある場合にのみ推奨されます。
const adGroupIterator = campaign.adGroups()
.withCondition(`ad_group.name = "${adGroupName}"`)
.get();
for (const adGroup of adGroupIterator) {
...
}
特定の広告グループのみを操作する場合は、AdsApp.shoppingAdGroups()
メソッドを使用して、キャンペーンを最初に取得せずに広告グループを取得できます。
const adGroupIterator = AdsApp.shoppingAdGroups()
.withCondition(`campaign.name = "${campaignName}"`)
.withCondition(`ad_group.name = "${adGroupName}"`)
.get();
for (const adGroup of adGroupIterator) {
...
}
商品広告
Google 広告スクリプトを使用すると、ShoppingAdGroup
の ads()
メソッドを使用して商品広告を取得できます。ShoppingAdGroup
の newAdBuilder()
メソッドを使用して、新しい商品広告を作成できます。
商品グループの階層を反復処理する
ShoppingAdGroup
の rootProductGroup
メソッドを使用して、商品グループ階層のルートにアクセスできます。その後、children
メソッドを使用して、子商品グループを反復処理し、商品グループの階層をトラバースできます。各ノードは ProductGroup
オブジェクトです。getDimension
メソッドを使用すると、商品グループの実際のタイプを特定できます。対応するキャスト メソッド(asBrand
など)を使用して、より具体的な型(ProductBrand
など)にキャストすることもできます。次のコード スニペットは、商品グループの階層を再帰的にトラバースする方法を示しています。
walkTree(shoppingAdGroup.rootProductGroup(), 1);
function walkTree(root, level) {
// Logger.log(root.getDimension());
let description = "";
switch (root.getDimension()) {
case "ROOT":
description = "Root";
break;
case "CATEGORY":
description = root.asCategory().getName();
break;
case "BRAND":
description = root.asBrand().getName();
break;
// Handle more types here.
...
}
if (root.isOtherCase()) {
description = "Other";
}
const padding = new Array(level + 1).join('-');
console.log("%s, %s, %s, %s, %s, %s",
padding,
description,
root.getDimension(),
root.getMaxCpc(),
root.isOtherCase(),
root.getId().toFixed());
const children = root.children().get();
for (const child of children) {
walkTree(child, level + 1);
}
}
特定の商品グループを選択する
AdsApp
、ShoppingCampaign
、または ShoppingAdGroup
インスタンスの productGroups
メソッドを使用して、商品グループ階層内の特定の商品グループを選択できます。このアプローチは、入札単価管理の目的で特定の商品グループを選択する場合に、商品グループの階層全体をトラバースするよりも簡単です。次のコード スニペットは、過去 1 か月間にクリック数が 5 回を超え、クリック率が 0.01
を超えるすべての商品グループを選択し、入札単価を 0.01
引き上げる方法を示しています。
function main() {
const productGroups = AdsApp.productGroups()
.withCondition("metrics.clicks > 5")
.withCondition("metrics.ctr > 0.01")
.forDateRange("LAST_MONTH")
.get();
for (const productGroup of productGroups) {
productGroup.setMaxCpc(productGroup.getMaxCpc() + 0.01);
}
}
商品グループの階層を更新する
newChild
メソッドを使用して、既存の商品グループに子商品グループを追加できます。これにより、ProductGroupBuilderSpace
オブジェクトが取得され、このオブジェクトを使用して適切な商品アイテム グループを構築できます。次のコード スニペットでは、ルートの下に「cardcow」ブランドのサブディビジョンを追加し、さらに新品と再生品に分割しています。
const root = shoppingAdGroup.rootProductGroup();
// Add a brand product group for a "cardcow" under root.
const brandProductGroup = root.newChild()
.brandBuilder()
.withName("cardcow")
.withBid(1.2)
.build()
.getResult();
// Add new conditions for New and Refurbished cardcow brand items.
const newItems = brandProductGroup.newChild()
.conditionBuilder()
.withCondition("New")
.withBid(1.5)
.build()
.getResult();
// Refurbished items will use the bid from "cardcow" product group.
const refurbishedItems = brandProductGroup.newChild()
.conditionBuilder()
.withCondition("Refurbished")
.build()
.getResult();
同様に、ProductGroup
の remove
メソッドを使用して、サブディビジョンを削除できます。削除する商品グループの下にある商品グループの階層全体も削除されます。
スクリプトにより、各商品グループの作成後に商品グループの階層が一貫した状態になるため、商品グループの階層構造を更新する際に「その他すべて」に対応する商品グループを作成または削除する必要はありません。
「Everything else」商品グループ
ショッピング商品グループの階層には、各レベルに [その他すべて]([その他])商品グループが含まれています。この商品グループは、商品グループの階層で作成したカスタム条件に一致しない商品を処理するために使用されます。isOtherCase
メソッドを使用すると、追加した通常の商品グループと「その他」の商品グループを区別できます。
次のコード スニペットは、ルート商品グループ階層の下にある「その他」の商品グループを取得し、その入札単価を出力します。
const root = shoppingAdGroup.rootProductGroup();
const childProductGroups = root.children().get();
let everythingElseProductGroupFound = false;
for (const childProductGroup of childProductGroups) {
if (childProductGroup.isOtherCase()) {
console.log("'Everything else' product group found. Type of the " +
"product group is %s and bid is %s.",
childProductGroup.getDimension(),
childProductGroup.getMaxCpc());
everythingElseProductGroupFound = true;
break;
}
}
if (!everythingElseProductGroupFound) {
console.log("No 'Everything else' product group found under root " +
"product group.");
}
リーフ商品グループを細分化すると、商品グループの階層が有効な状態を維持するために、「その他」の商品グループがスクリプトによって自動的に作成されます。「その他」の商品グループは、親の商品グループの入札単価を継承します。
新しいショッピング広告グループを作成する
Google 広告スクリプトでは、ShoppingCampaign
の newAdGroupBuilder
メソッドを使用して、新しいショッピング広告グループを作成できます。ShoppingAdGroup
を作成したら、その createRootProductGroup
メソッドを使用して新しい商品グループ階層を作成できます。
レポート
Google 広告スクリプトは、ショッピング キャンペーンのパフォーマンスをトラッキングするのに役立つ product_group_view
レポートと shopping_performance_view
レポートをサポートしています。レポートについて詳しくは、レポートガイドをご覧ください。