Get Current Day for Adwords Bulk Script

google-adwordsgoogle-adwords-scripts

I have a script I am running to pause ads on Adwords once they go over a certain amount.

function main() {

  // GET THE TOTAL SPEND OF THE ACCOUNT
 var costReport = AdWordsApp.report("Select Cost from ACCOUNT_PERFORMANCE_REPORT DURING TODAY");

  var reportRow = costReport.rows().next();
  var totalCost = reportRow["Cost"].replace(',', ''); // Remove thousand separator

  // PAUSE THE ACTIVE CAMPAIGNS IF THE BUDGET IS REACHED. REPLACE "XXXX" WITH YOUR BUDGET.
  if (totalCost > 700){
    var campaignIterator = AdWordsApp.campaigns().withCondition("Status = ENABLED").get();

    while (campaignIterator.hasNext()){
      var campaign = campaignIterator.next();
      campaign.pause();
    }
  }
}

I need to make this script run only if it is a certain day. So something like:

 if (totalCost > 700 && day == 'Monday){


var campaignIterator = AdWordsApp.campaigns().withCondition("Status = ENABLED").get();

        while (campaignIterator.hasNext()){
          var campaign = campaignIterator.next();
          campaign.pause();
        }

Best Answer

There are two solutions if you want an AdWords Script to only run on Mondays.

The first solution is to do it with code. To do that, add two variable definitions:

var d = new Date();
var n = d.getDay();

Add that above where you have this:

// PAUSE THE ACTIVE CAMPAIGNS IF THE BUDGET IS REACHED. REPLACE "XXXX" WITH YOUR BUDGET.

Now, the variable "n" will contain a number from 0 to 6, where Sunday is 0, Monday is 1, and so on.

If you only want your code to run on Monday, you would start with this IF statement in your existing code:

if (totalCost > 700){

And add an AND statement for the day of the week:

if ( ( totalCost > 700) && ( n == 1) ) {

Just replace your line with my line, and the code should run (although only on Mondays).

The second solution is to keep the exact code you have, but then schedule that AdWords Script to run Weekly on Mondays at a specific time you set.