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 メソッドを使用します。このアプローチは、入札単価管理のために特定の商品グループを選択する際に、商品グループの階層全体を走査するよりも簡単です。次のコード スニペットは、先月のクリック数が 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 レポートをサポートしています。レポートについて詳しくは、レポートガイドをご覧ください。