Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- */
- //Configuration
- var CAMPAIGN_LABEL = 'Check'; // Looks for campaigns with that label
- //End of configuration
- //Calculate time in Poland
- var time = new Date();
- time = (time.getUTCHours());
- if (time === 22) {time = "00";} else if (time === 23) {time = 01;} else {time = time + 2;}
- function main() {
- checkPausedCampaigns();
- checkSpend();
- }
- //Check the spend
- function checkSpend(){
- var allCampaigns = AdsApp.campaigns()
- .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "']")
- .forDateRange("TODAY")
- .get();
- while (allCampaigns.hasNext()) {
- var iterator = allCampaigns.next();
- var campaignBudget = iterator.getBudget();
- var campaignCost = iterator.getStatsFor('TODAY').getCost();
- //Is the spend too high?
- if (campaignCost > campaignBudget) {
- //Pause the campaign
- iterator.pause();
- //Log the name of the paused campaign
- Logger.log("The campaign: " + iterator.getName() + " has exceeded the daily budget - time to pause it!");
- }
- }
- }
- //Is it the time to unpause the campaigns?
- function checkPausedCampaigns(){
- //Just to be safe, the script tries 3 times to unpause the campagins
- if (time === 0 || time === 01 || time === 02){
- var pausedCampaigns = AdsApp.campaigns()
- .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "']")
- .withCondition("Status = PAUSED")
- .get();
- while (pausedCampaigns.hasNext()) {
- var iterator = pausedCampaigns.next();
- //Unpause the campaign
- iterator.enable();
- //Log the name of the unpaused campaign
- Logger.log("It's a new day for campaign: " + iterator.getName() + " time to unpause it!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement