Skip to main content

ตัวอย่างสคริปต์

สนิปเป็ตสำเร็จรูปสำหรับงานที่พบบ่อย คัดลอกอันใดก็ได้ไปวางในตัวแก้ไข ปรับเกณฑ์ให้เหมาะกับคุณ แล้วทดสอบด้วยปุ่ม ▶ Run วิธีการทำงานของคำสั่ง SDK และกำหนดการอธิบายไว้ในส่วน การเขียนสคริปต์

ข้อควรระวัง

ตัวอย่างเหล่านี้มีการกระทำจริง (pause) ก่อนเปิดใช้งานสคริปต์ตามกำหนดการ ให้ทดสอบกับข้อมูลจริงก่อน — หากจำเป็น ให้คอมเมนต์บรรทัดที่มี pause() ออกชั่วคราวและเหลือไว้เพียง console.log

หยุดตาม ROAS ต่ำ

หยุดโฆษณาที่ใช้จ่ายไปมากกว่า $20 ตลอดทั้งวัน แต่ให้ ROAS ต่ำกว่า 0.5

JavaScript
function main() {
const ads = QubixApp.ads()
.withCondition('spend_24h > 20 AND roas_24h < 0.5')
.get()

for (const ad of ads) {
console.log('pause', ad.name, 'roas24h=', ad.roas_24h)
ad.pause({ duration: '24h', reason: 'low roas' })
}
}

หยุดเมื่อไม่มียอดฝากแม้ใช้จ่ายสูง

หยุดโฆษณาที่ไม่มียอดฝากแม้แต่รายการเดียวตลอดทั้งวัน ในขณะที่ค่าใช้จ่ายเกินยอดจ่ายเฉลี่ยของ geo ไปแล้ว — นั่นคือ เงินกำลังออกไปแต่ไม่มีผลลัพธ์

JavaScript
function main() {
const ads = QubixApp.ads()
.withCondition('deps_24h = 0 AND spend_24h > geo_avg_payout')
.get()

for (const ad of ads) {
console.log('pause (no deps)', ad.name, 'spend24h=', ad.spend_24h)
ad.pause({ duration: '24h', reason: 'no deposits' })
}
}

ความจำระหว่างการรัน

จดจำโฆษณาที่ประมวลผลแล้วใน ctx.state และข้ามโฆษณาเหล่านั้นในการรันครั้งถัดไป เพื่อไม่ให้แตะตัวเดิมซ้ำ

JavaScript
function main() {
const seen = ctx.state.get('seen') || []

const ads = QubixApp.ads()
.withCondition('roas_24h < 0.5 AND spend_24h > 20')
.get()

for (const ad of ads) {
if (seen.includes(ad.ad_id)) continue
ad.pause({ duration: '24h', reason: 'low roas' })
seen.push(ad.ad_id)
console.log('paused', ad.name)
}

ctx.state.set('seen', seen)
}

การจัดเรียงและจำกัดการเลือก

ดึงโฆษณาห้ารายการที่มีค่าใช้จ่ายสูงสุดตลอดทั้งวัน และเพียงพิมพ์ออกมาที่คอนโซล — เป็นพื้นฐานสำหรับรายงานและการตรวจสอบ

JavaScript
function main() {
const top = QubixApp.ads()
.orderBy('spend_24h', 'desc')
.withLimit(5)
.get()

for (const ad of top) {
console.log(ad.name, 'spend24h=', ad.spend_24h, 'roas24h=', ad.roas_24h)
}
}

การทำงานในระดับแคมเปญ

คำสั่งเหล่านี้ใช้ได้ไม่เพียงกับโฆษณาเท่านั้น แต่ยังใช้กับ ad set (QubixApp.adsets()) และแคมเปญ (QubixApp.campaigns()) ได้ด้วย จากแคมเปญ คุณสามารถดึง ad set และโฆษณาที่อยู่ภายในออกมาได้

JavaScript
function main() {
const campaigns = QubixApp.campaigns()
.withCondition('roas_24h < 0.3')
.get()

for (const c of campaigns) {
console.log('weak campaign', c.name)
for (const ad of c.getAds()) {
ad.pause({ duration: '12h', reason: 'weak campaign' })
}
}
}

ขั้นตอนต่อไป