Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. */
  2.  
  3. //Configuration
  4. var CAMPAIGN_LABEL = 'Check'; // Looks for campaigns with that label
  5. //End of configuration
  6.  
  7. //Calculate time in Poland
  8. var time = new Date();
  9. time = (time.getUTCHours());
  10. if (time === 22) {time = "00";} else if (time === 23) {time = 01;} else {time = time + 2;}
  11.  
  12.  
  13. function main() {
  14. checkPausedCampaigns();
  15. checkSpend();
  16. }
  17.  
  18. //Check the spend
  19. function checkSpend(){
  20. var allCampaigns = AdsApp.campaigns()
  21. .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "']")
  22. .forDateRange("TODAY")
  23. .get();
  24.  
  25. while (allCampaigns.hasNext()) {
  26. var iterator = allCampaigns.next();
  27. var campaignBudget = iterator.getBudget();
  28. var campaignCost = iterator.getStatsFor('TODAY').getCost();
  29.  
  30. //Is the spend too high?
  31. if (campaignCost > campaignBudget) {
  32.  
  33. //Pause the campaign
  34. iterator.pause();
  35.  
  36. //Log the name of the paused campaign
  37. Logger.log("The campaign: " + iterator.getName() + " has exceeded the daily budget - time to pause it!");
  38.  
  39. }
  40. }
  41. }
  42.  
  43. //Is it the time to unpause the campaigns?
  44. function checkPausedCampaigns(){
  45.  
  46. //Just to be safe, the script tries 3 times to unpause the campagins
  47. if (time === 0 || time === 01 || time === 02){
  48. var pausedCampaigns = AdsApp.campaigns()
  49. .withCondition("LabelNames CONTAINS_ANY ['" + CAMPAIGN_LABEL + "']")
  50. .withCondition("Status = PAUSED")
  51. .get();
  52.  
  53. while (pausedCampaigns.hasNext()) {
  54. var iterator = pausedCampaigns.next();
  55.  
  56. //Unpause the campaign
  57. iterator.enable();
  58.  
  59. //Log the name of the unpaused campaign
  60. Logger.log("It's a new day for campaign: " + iterator.getName() + " time to unpause it!");
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement