Can anyone fix this Google Ads Script?

163 views
Skip to first unread message

Sanal John

unread,
Jan 13, 2020, 12:29:56 AM1/13/20
to Google Ads Scripts Forum
Hi Team,

I am using this script from the last 4,5 days and this script is for pausing the campaigns once it achieved the daily budget. Unfortunately for some reason, this script is not working and the campaigns are not pausing. Can anyone fix if any issues in this script?

=========================

var MAX_TOTALS = 1200;
var TODAYS_SPEND = 0;
 
function main() {
    processTodaysSpend();
    processCampaignPause();
}
 
function processTodaysSpend() {
    //Get Ad Spend
    TODAYS_SPEND += getTodaysCosts(AdsApp.campaigns().get());
    //Get Video Spend
    TODAYS_SPEND += getTodaysCosts(AdsApp.videoCampaigns().get());
}
 
function processCampaignPause() {
 
    if (TODAYS_SPEND > MAX_TOTALS) {
   
        var adsSelector = AdsApp.campaigns()
        .withCondition("LabelNames CONTAINS_NONE ['Active']")
        .get();
       
        var videoSelector = AdsApp.videoCampaigns()
        .withCondition("LabelNames CONTAINS_NONE ['Active']")
        .get();
   
        //Pause Ads
        pauseCampaigns(adsSelector);
        //Pause Videos
        pauseCampaigns(videoSelector);
    }
}
 
function getTodaysCosts(selector) {
    var totals = 0;
 
    while(selector.hasNext()) {
        var campaign = selector.next();
        var stats = campaign.getStatsFor("TODAY");
        totals += stats.getCost();
    }
      return totals;
}
 
function pauseCampaigns(selector) {
 
    while(selector.hasNext()) {
        var campaign = selector.next();
        campaign.pause();
    } 
}
Reply all
Reply to author
Forward
0 new messages