แคมเปญ Performance Max ที่ใช้ AdsApp

สคริปต์ Google Ads มีไว้สำหรับการจัดการบางอย่างของแคมเปญ Performance Max คุณสามารถใช้สคริปต์เพื่อดึงข้อมูลแคมเปญ Performance Max จัดการกลุ่มชิ้นงาน และเรียกใช้รายงานได้ แต่จะใช้สคริปต์เพื่อสร้างแคมเปญ Performance Max ไม่ได้ หากต้องการดำเนินการขั้นสูงเพิ่มเติม โปรดดูส่วนที่เหลือของคู่มือนี้ ซึ่งสาธิตวิธีการกว้างๆ โดยใช้ mutate

การดึงข้อมูลแคมเปญ 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 จะใช้ชิ้นงานต่างๆ เช่น วิดีโอ รูปภาพ บรรทัดแรก และคำอธิบาย ทั้งที่คุณสร้างขึ้นเองหรือสร้างขึ้นโดยอัตโนมัติ ดูภาพรวมทั้งหมดเกี่ยวกับประเภทชิ้นงานที่ต้องใช้ได้ในคู่มือชิ้นงาน Google Ads API Performance Max

ชิ้นงานสําหรับแคมเปญ Performance Max จะรวมกันเป็นกลุ่มชิ้นงาน 1 กลุ่ม และแคมเปญ 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 เพื่อปรับแต่งผลการค้นหาเพิ่มเติม ใช้เครื่องมือสร้างคำค้นหาสำหรับรายงานนี้เพื่อสร้างคำค้นหาของคุณเอง

เมื่อมีชิ้นงานเป้าหมายแล้ว ให้โทรหา remove ในกลุ่มชิ้นงานเพื่อนําชิ้นงานออกจากกลุ่มชิ้นงาน

// Let's assume at least one asset is returned. We'll just remove the first
// asset, whatever it is. In your code, customize this to choose the right asset.
const row_info = results.next().asset;
assetGroup.remove(row_info.asset.resource_name, row_info.asset_group_asset.field_type);