Skip to main content

Script examples

Ready-made snippets for common tasks. Copy any of them into the editor, adjust the thresholds for yourself and test with the Run button. How the SDK commands and the schedule work is covered in the Writing a script section.

Attention

The examples contain real actions (pause). Before enabling a script on a schedule, test it on live data — if needed, temporarily comment out the lines with pause() and leave only console.log.

Pause by low ROAS

Stops ads that spent more than $20 over the day but give a ROAS below 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' })
}
}

Pause with no deposits at high spend

Stops ads that have not a single deposit over the day, while spend has already exceeded the average payout for the geo — that is, money is going out but there is no result.

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' })
}
}

Memory between runs

Remembers processed ads in ctx.state and skips them on subsequent runs, so as not to touch the same ones again.

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)
}

Sorting and limiting the selection

Takes the five ads with the highest spend over the day and simply prints them to the console — a basis for reports and checks.

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)
}
}

Working at the campaign level

The commands are available not only for ads, but also for ad sets (QubixApp.adsets()) and campaigns (QubixApp.campaigns()). From a campaign you can get the nested ad sets and ads.

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' })
}
}
}

What's next