วิธีที่ดีที่สุดในการจัดการแคมเปญ Performance Max ด้วยสคริปต์ Google Ads คือการใช้กลยุทธ์การเปลี่ยนแปลงเพื่อจัดการแคมเปญ Performance Max โดยตรง แคมเปญ Performance Max สร้างได้โดยใช้ฟังก์ชัน mutate เท่านั้น อย่างไรก็ตาม คุณยังสามารถใช้ออบเจ็กต์ AdsApp อื่นๆ เพื่อดึงข้อมูลแคมเปญ Performance Max, จัดการกลุ่มชิ้นงาน และเรียกใช้รายงานได้หากต้องการ
การดึงข้อมูลแคมเปญ Performance Max
แคมเปญ Performance Max พร้อมใช้งานผ่านคอลเล็กชัน performanceMaxCampaigns ของออบเจ็กต์ AdsApp คุณสามารถเรียกดูข้อมูลดังกล่าวได้ตามปกติโดยทำดังนี้
const campaignName = "My Performance Max campaign";
const campaignIterator = AdsApp.performanceMaxCampaigns()
.withCondition(`campaign.name = "${campaignName}"`)
.get();
for (const campaign of campaignIterator) {
...
}
แคมเปญ Performance Max แตกต่างจากแคมเปญประเภทอื่นๆ ตรงที่ไม่มีกลุ่มโฆษณาหรือออบเจ็กต์โฆษณาที่คุณเห็นได้ โดยระบบจะจัดการทุกอย่างที่เกี่ยวข้องกับแนวคิดเหล่านี้สําหรับแคมเปญอื่นๆ ให้คุณโดยอัตโนมัติตามกลุ่มชิ้นงานที่คุณตั้งไว้
ชิ้นงานและกลุ่มชิ้นงาน
โฆษณาจากแคมเปญ Performance Max จะทำงานโดยใช้ชิ้นงาน เช่น วิดีโอ รูปภาพ บรรทัดแรก และคำอธิบาย ไม่ว่าคุณจะระบุเองหรือระบบสร้างขึ้นโดยอัตโนมัติ ดูภาพรวมทั้งหมดของประเภทชิ้นงานที่จำเป็นได้ที่คู่มือชิ้นงาน Performance Max ของ Google Ads API
ชิ้นงานสําหรับแคมเปญ Performance Max จะรวมเข้าด้วยกันเป็นกลุ่มชิ้นงาน และแคมเปญ Performance Max แต่ละแคมเปญต้องมีกลุ่มชิ้นงานอย่างน้อย 1 กลุ่ม คุณไม่สามารถสร้างกลุ่มชิ้นงานเหล่านี้ในสคริปต์โดยตรง แต่สามารถเพิ่มและนําชิ้นงานออกจากกลุ่มชิ้นงานที่มีอยู่ได้
เพิ่มชิ้นงานลงในกลุ่มชิ้นงาน
ขั้นแรก ให้สร้างชิ้นงานโดยทำดังนี้
const imageUrl = "http://www.example.com/example.png";
const imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
const assetOperation = AdsApp.adAssets().newImageAssetBuilder()
.withName("new asset name")
.withData(imageBlob)
.build();
const imageAsset = assetOperation.getResult();
จากนั้นเพิ่มชิ้นงานที่เพิ่งสร้างลงในกลุ่มชิ้นงานที่มีอยู่โดยทำดังนี้
// First, fetch the Performance Max campaign we want to operate on.
const campaignIterator = AdsApp.performanceMaxCampaigns()
.withCondition(`campaign.name = '${campaignName}'`)
.get();
let campaign;
if (campaignIterator.hasNext()) {
campaign = campaignIterator.next();
} else {
throw `No campaign found with name ${campaignName}.`
}
// Then, get that campaign's asset groups.
const assetGroupIterator = campaign.assetGroups().get();
// The campaign must have at least one asset group, so we can just assume so here.
const assetGroup = assetGroupIterator.next();
// Add the asset from the previous step.
assetGroup.addAsset(imageAsset, 'MARKETING_IMAGE');
โปรดทราบว่าคุณต้องระบุประเภทชิ้นงานในขั้นตอนสุดท้าย ดูรายการประเภทชิ้นงานทั้งหมดได้ในเอกสารประกอบของ Google Ads API
หากต้องการใช้ชิ้นงานที่มีอยู่ ให้สร้างตัวเลือกชิ้นงานก่อน โดยทำดังนี้
const assetSelector = AdsApp.adAssets().assets();
จากนั้นใช้ตัวกรอง withCondition เพื่อจำกัดขอบเขตให้เหลือเฉพาะชิ้นงานที่ต้องการดำเนินการ ดูตัวเลือกตัวกรองทั้งหมดได้ที่ AssetSelector
สุดท้าย ให้ดึงข้อมูลตัวทำซ้ำและทำซ้ำเช่นเดียวกับเอนทิตีอื่นๆ ดังนี้
const assetIterator = assetSelector.get();
for (const asset of assetIterator) {
...
}
ชิ้นงานข้อความ
ชิ้นงานข้อความจะทำงานแตกต่างออกไปเล็กน้อยตรงที่คุณไม่จำเป็นต้องสร้างชิ้นงานไว้ล่วงหน้า คุณเพียงแค่ระบุข้อความแทนชิ้นงาน แล้วระบบจะสร้างชิ้นงานให้คุณโดยอัตโนมัติ หากข้อความซ้ำกับชิ้นงานข้อความที่มีอยู่ ระบบจะใช้ชิ้นงานที่มีอยู่ซ้ำแทน
ตัวอย่างวิธีสร้างชิ้นงานบรรทัดแรกมีดังนี้
assetGroup.addAsset('asset text here', 'HEADLINE');
นำชิ้นงานออกจากกลุ่มชิ้นงาน
นอกจากนี้ คุณยังนําชิ้นงานออกจากกลุ่มชิ้นงานได้ด้วย แต่โปรดทราบว่ากลุ่มชิ้นงานแต่ละกลุ่มต้องมีชิ้นงานบางประเภทตามจํานวนขั้นต่ำเพื่อให้แคมเปญใช้งานได้
วิธีนําชิ้นงานที่เพิ่มในตัวอย่างก่อนหน้านี้ออกมีดังนี้
assetGroup.removeAsset(imageAsset, 'MARKETING_IMAGE');
นอกจากนี้ คุณยังดูรายการชิ้นงานในกลุ่มชิ้นงานที่ระบุได้ด้วยฟังก์ชัน search ดังนี้
// The resource name is a unique identifier for this asset group.
const assetGroupName = assetGroup.getResourceName();
results = AdsApp.search(
`SELECT asset.resource_name, asset_group_asset.field_type
FROM asset_group_asset
WHERE asset_group.resource_name = '${assetGroupName}'`
);
ซึ่งจะเลือกชื่อทรัพยากรของชิ้นงานเป็นตัวระบุที่ไม่ซ้ำกัน คุณยังเลือกช่องอื่นๆ ได้ด้วย เช่น asset.type หรือ asset.text_asset.text เพื่อปรับแต่งผลลัพธ์เพิ่มเติม ใช้Query Builder สำหรับรายงานประเภทนี้เพื่อสร้างคำค้นหาของคุณเอง
เมื่อมีชิ้นงานเป้าหมายแล้ว ให้เรียกใช้ remove ในกลุ่มชิ้นงานเพื่อนําชิ้นงานออกจากกลุ่มชิ้นงาน ดังนี้
// This example assumes at least one asset is returned. We'll remove the first
// asset, whatever it is. In your code, customize this to choose the right
// asset to be removed.
const row_info = results.next().asset;
assetGroup.remove(row_info.asset.resource_name, row_info.asset_group_asset.field_type);