My First script: How to make it run every hour

107 views
Skip to first unread message

Mirko Norz

unread,
Jul 19, 2022, 12:32:58 PM7/19/22
to Google Ads Scripts Forum
I created a script that analyzes and sends emails and logs based on the total budget level spent per month.

This script works perfectly if I run it by me. It means I click on preview or run and it does what I want.

I want this script work by itself. It must activate at least every hour.


/*
* Check costi mensili - email e log alert
*/

var MONTHLY_BUDGET = 1600;
var CLIENTE = AdsApp.currentAccount().getName();
//var EMAIL_ADDRESS = "mi...@hub.norz.it,ser...@norz.it,pa...@norz.it";
var EMAIL_ADDRESS = "mi...@hub.norz.it";
var EMAIL_SUBJECT = "[GAds Script Alert] "+CLIENTE+" : Budget mensile superato";
var EMAIL_BODY =
"\n\n"+
"Cliente: "+CLIENTE+
"\nBudget mensile: "+MONTHLY_BUDGET;
var GIORNI2=new Date();
var GIORNI=new Date();
var testDate=new Date();



var OGGI=Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");

function main() {
MONTHLY_BUDGET50 = MONTHLY_BUDGET/100*50;
MONTHLY_BUDGET80 = MONTHLY_BUDGET/100*80;


var query =
"SELECT " +
"metrics.cost_micros " +
"FROM customer "+
"WHERE segments.date DURING THIS_MONTH";

try {
var result = AdsApp.search(query);
while (result.hasNext()) {
var row = result.next();
var adSpend = convertMicrosToCurrency(row.metrics.costMicros, true);
}
} catch (e) {
Logger.log("### ERROR: "+e);
}

//Alert budget superato
if (adSpend>MONTHLY_BUDGET) {
Logger.log("Budget mensile: "+MONTHLY_BUDGET);
Logger.log("Spesa mensile effettiva: "+adSpend+"\n");
DIFFERENZA=adSpend-MONTHLY_BUDGET;
Logger.log("Budget mensile superato di €"+DIFFERENZA);
EMAIL_SUBJECT = "[GAds Script Alert] "+CLIENTE+" : 100% del Budget mensile superato";
MailApp.sendEmail(EMAIL_ADDRESS,EMAIL_SUBJECT, EMAIL_BODY+"\nSpesa mensile effettiva: "+adSpend+"\n Budget mensile superato di €"+DIFFERENZA);
}


//Alert 80% del budget superato
else if (adSpend>MONTHLY_BUDGET80) {
Logger.log("Budget mensile: "+MONTHLY_BUDGET);
Logger.log("Spesa mensile effettiva: "+adSpend+"\n");
DAY_BUDGET=MONTHLY_BUDGET/30;
DIFFERENZA=MONTHLY_BUDGET-adSpend;
DAY_LEFT=~~(DIFFERENZA/DAY_BUDGET);
GIORNI2.setDate(testDate.getDate()+DAY_LEFT);
GIORNI=Utilities.formatDate(GIORNI2, "GMT+0", 'dd/MM/yyyy');
Logger.log("Budget rimasto €"+DIFFERENZA+" che potrebbe finire entro "+DAY_LEFT+" giorni in data "+GIORNI);
EMAIL_SUBJECT = "[GAds Script Alert] "+CLIENTE+" : 80% del Budget mensile superato";
MailApp.sendEmail(EMAIL_ADDRESS,EMAIL_SUBJECT, EMAIL_BODY+"\nSpesa mensile effettiva: €"+adSpend+"\n Budget rimasto €"+DIFFERENZA+" che potrebbe finire entro "+DAY_LEFT+" giorni in data "+GIORNI);
}


//Alert 50% del budget superato
else if (adSpend>MONTHLY_BUDGET50) {
Logger.log("Budget mensile: €"+MONTHLY_BUDGET);
Logger.log("Spesa mensile effettiva: €"+adSpend+"\n");
DAY_BUDGET=MONTHLY_BUDGET/30;
DIFFERENZA=MONTHLY_BUDGET-adSpend;
DAY_LEFT=~~(DIFFERENZA/DAY_BUDGET);
GIORNI2.setDate(testDate.getDate()+DAY_LEFT);
GIORNI=Utilities.formatDate(GIORNI2, "GMT+0", 'dd/MM/yyyy');
Logger.log("Budget rimasto €"+DIFFERENZA+" che potrebbe finire entro "+DAY_LEFT+" giorni in data "+GIORNI);
EMAIL_SUBJECT = "[GAds Script Alert] "+CLIENTE+" : 50% del Budget mensile superato";
MailApp.sendEmail(EMAIL_ADDRESS,EMAIL_SUBJECT, EMAIL_BODY+"\nSpesa mensile effettiva: €"+adSpend+"\n Budget rimasto €"+DIFFERENZA+" che potrebbe finire entro "+DAY_LEFT+" giorni in data "+GIORNI);
}

}


function convertMicrosToCurrency(micros, rounded) {
if (rounded == true) {
return Number(Math.round(micros/1000000+'e2')+'e-2'); // returns currency value with 2 decimals
} else if (rounded == false) {
return micros/1000000; // returns currency value without any rounding
}
}



Google Ads Scripts Forum Advisor

unread,
Jul 20, 2022, 1:00:39 AM7/20/22
to adwords...@googlegroups.com

Hello Mirko,

I’m James from the Google Ads script support team. Thank you for raising your concern to us. You may follow this guide under the `Scheduling a script` section on how you can schedule the execution frequency of your script on an hourly basis.

Let me know if you have any further questions.

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


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