With all the different campaign types that are more or less supported in scripts, I often use bulk uploads for these sort of tasks.
In this instance: Fetch cost data via an api report and if cost > xx, create and upload a spreadsheet with instructions to pause all campaigns in the account. And also send an email when campaigns are paused.
Something like this approach
const settings = {
maxSpend:50000, // pause all campaigns if cost is above this amount during this month
notify: { // send email to these people when campaigns are paused
'Sigurd':'sig...@example.com',
'Sigurd 2':'sigurd-@@example.com',
'Sigurd 3':'sigu...@example.com'
}
}
function main() {
const today = new Date();
const timeZone = AdsApp.currentAccount().getTimeZone();
const startDate = Utilities.formatDate(today,timeZone,'yyyy-MM')+'-01';
const endDate = Utilities.formatDate(today,timeZone,'yyyy-MM-dd');
const query = `
SELECT
customer.id,
campaign.id,
campaign.name,
metrics.cost_micros
FROM
campaign
WHERE
segments.date BETWEEN '${startDate}' AND '${endDate}'
AND campaign.status = ENABLED
AND campaign.serving_status = SERVING`;
const response = AdsApp.search(query);
const campaigns = [];
const campaignNames = [];
let totalCost = 0;
console.log(`Spend during ${startDate} - ${endDate}:`);
while (response.hasNext()) {
const row = response.next();
campaigns.push([row.customer.id,row.campaign.id,row.campaign.name])
campaignNames.push(row.campaign.name)
const cost = parseFloat(row.metrics.costMicros)/1000000
totalCost += cost;
console.log(`"${row.campaign.name}": ${cost.toFixed(2)}`);
}
if (totalCost > settings.maxSpend) {
console.log(`\nTotal spend: ${totalCost.toFixed(0)}. Max was ${settings.maxSpend}, so ${(totalCost-settings.maxSpend).toFixed(0)} overspend`);
pauseCampaigns(campaigns);
sendEmail(settings.notify,campaignNames,totalCost,settings.maxSpend,startDate+' - '+endDate);
}
else {
console.log(`\nTotal spend ${totalCost.toFixed(0)}. Max is ${settings.maxSpend}, so still have ${(settings.maxSpend-totalCost).toFixed(0)} left`);
}
}
function pauseCampaigns(campaigns) {
const bulkUpload = AdsApp.bulkUploads().newCsvUpload([
'Action',
'Customer id',
'Campaign id',
'Campaign',
'Campaign status'
]);
for (i=0;i<campaigns.length;i++) {
bulkUpload.append({
'Action':'edit',
'Customer id':campaigns[i][0],
'Campaign id':campaigns[i][1],
'Campaign':campaigns[i][2],
'Campaign status':'paused'
})
}
bulkUpload.setFileName(`Spend limit reached: Pause ${campaigns.length} campaign(s)`);
bulkUpload.apply(); // change to .preview() to review changes in the bulk uploads section before applying
console.log(`Spend limit reached: ${campaigns.length} campaign(s) paused. See details in bulk upload log`);
}
function sendEmail(recipients,campaigns,totalCost,maxSpend,dates) {
const accountName = AdsApp.currentAccount().getName();
for (const [name,email] of Object.entries(recipients))
MailApp.sendEmail(
email,`The spend limit of ${maxSpend} has been reached and ${campaigns.length} campaign(s) have been paused`,
`Hi ${name},\n\nThe spend limit of ${maxSpend} in the "${accountName}" Google Ads account has been reached. Total spend for the period ${dates} reached ${totalCost.toFixed(0)}.
\nThe campaign(s) listed below have now been paused:
\n"${campaigns.join('"\n"')}"`
);
} Sigurd