Check to see if campaigns are enabled before performing tasks

55 views
Skip to first unread message

Josh Babbitt

unread,
Jul 12, 2021, 6:34:24 PM7/12/21
to Google Ads Scripts Forum
I have a script that I am using to pause the campaigns in an account to help control cost, and once it reaches the requirements the script sends me an email to let me know that the campaigns have been paused.  That works great.  I have it set to run hourly.
The only problem is that it keeps sending me an email every hour to let me know that the campaigns are paused.
What I would like it to do is first check to see if any campaign in the account is enabled, and if so, then run the rest of the script, if none are enabled stop, and not run the rest of the script.

I'm not a programmer by any stretch of the imagination, but I think it has something to do with the AdsApp.​CampaignIterator to see if a campaign is enabled, but I'm not sure, and not exactly sure where to start, so any assistance would be greatly appreciated!

Google Ads Scripts Forum Advisor

unread,
Jul 13, 2021, 1:28:12 AM7/13/21
to adwords...@googlegroups.com
Hi Josh,

Thanks for reaching out. Harry here, from the Google Ads Scripts Team.

You can leverage on filtering the CampaignSelector and CampaignIterator, then use the totalNumEntities method to count the selected entities. I have no full context on how your script was implemented, but you can refer to the sample code below:
   var campaignIterator = AdsApp.campaigns().withCondition("Status = ENABLED").get();
   var numOfEnabledCampaigns = campaignIterator.totalNumEntities();
   
   // check if any campaigns are enabled
   if(numOfEnabledCampaigns > 1) {
     // run rest of the script
   }
Let me know how I can assist you further.

Thanks,
Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2Kbhzv:ref

Josh Babbitt

unread,
Jul 13, 2021, 12:55:09 PM7/13/21
to Google Ads Scripts Forum
Thank you!  I will give that a try.

Josh Babbitt

unread,
Jul 13, 2021, 4:48:35 PM7/13/21
to Google Ads Scripts Forum
Does that only check for Search/Display campaigns?  Or would I need to use something like var campaignIterator = AdsApp.shoppingCampaigns().withCondition("Status = ENABLED").get(); and var campaignIterator = AdsApp.videoCampaigns().withCondition("Status = ENABLED").get(); to have it check the shopping and video campaigns?

On Monday, July 12, 2021 at 10:28:12 PM UTC-7 adsscripts wrote:

Google Ads Scripts Forum Advisor

unread,
Jul 13, 2021, 11:35:56 PM7/13/21
to adwords...@googlegroups.com
Hi Josh,

Thanks for coming back. You are absolutely right. The standard campaign selector (AdsApp.campaigns().get()) will filter out video and shopping campaigns from the results. Use the dedicated campaign selectors instead: Please see this link for your reference. You may also want to check out the Code Snippets page.

Let me know of anything else.

Josh Babbitt

unread,
Jul 14, 2021, 11:59:47 AM7/14/21
to Google Ads Scripts Forum
Thank you for clarifying that.

So, based on the code you provided on July 12th, if I wanted the script to check for any active campaign (Search/Display, Video and Shopping) would it look like this:

     var campaignIterator = AdsApp.campaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns = campaignIterator.totalNumEntities();
  var campaignIterator = AdsApp.videoCampaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns = campaignIterator.totalNumEntities();
  var campaignIterator = AdsApp.shoppingCampaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns = campaignIterator.totalNumEntities();

   // check if any campaigns are enabled
   if(numOfEnabledCampaigns > 1) {
     // run rest of the script
}

Or would it need to look like this:

     var campaignIterator = AdsApp.campaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns = campaignIterator.totalNumEntities();
  var campaignIterator = AdsApp.videoCampaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns2 = campaignIterator.totalNumEntities();
  var campaignIterator = AdsApp.shoppingCampaigns().withCondition("Status = ENABLED").get();
  var numOfEnabledCampaigns3 = campaignIterator.totalNumEntities();

   // check if any campaigns are enabled
   if(numOfEnabledCampaigns + numOfEnabledCampaigns2 + numOfEnabledCampaign3 > 1) {
     // run rest of the script
}

Basically, when I .get() the number of enabled campaigns for each type of campaign can I keep adding it to the numOfEnabledCampaigns, or do I need to create a different one for each type, then add them together at the end?

Thank you so much for your help!

Google Ads Scripts Forum Advisor

unread,
Jul 15, 2021, 2:29:21 AM7/15/21
to adwords...@googlegroups.com
Hi Josh,

I work along with Harry. Let me do the best I can to assist you in this.

With regard to your concern, you need to get totalNumEntities() of each iterator, so your 2nd code snippet is the right approach. 

Please let me know if you have other inquiry related to Google Ads Scripts.

Regards,
Google Logo
Teejay Wennie Pimentel
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2Kbhzv:ref
Reply all
Reply to author
Forward
0 new messages