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
レポートがサポートされています。報告について詳しくは、報告ガイドをご覧ください。