Hello all, unfortunately we sometimes run into technical issues and conversions don't fire back (3rd party tracking). It has happened twice so far that we didnt notice until a couple hours later so we want to put an alert script into place where we get an alert if there were no conversions in the past hour.
The script we have now is:
function main() {
// Set your email address
var emailAddress = "
mye...@email.com";
// Get the conversion stats for today
var stats = AdsApp.currentAccount().getStatsFor("TODAY");
// Filter the statistics to include only data for the last hour
var conversionsLastHour = filterConversionsLastHour(stats);
// Check if there are no conversions in the last hour
if (conversionsLastHour === 0) {
// Send an email alert
sendEmailAlert(emailAddress, "No conversions recorded in the past hour.");
}
}
// Function to filter conversions for the last hour
function filterConversionsLastHour(stats) {
var now = new Date();
var oneHourAgo = new Date(now.getTime() - (1 * 3600 * 1000)); // 1 hour ago
var iterator = stats.conversions().forDateRange(oneHourAgo, now).get();
var count = 0;
while (iterator.hasNext()) {
var conversion = iterator.next();
count++;
}
return count;
}
// Function to send email alert
function sendEmailAlert(email, message) {
// Send email
MailApp.sendEmail({
to: email,
subject: "Google Ads Alert: No Conversions",
body: message
});
}
So the reason why it is a bit convoluted is because initially we got this error:
Ga: Invalid input: LAST_HOUR.
Expected one of ["TODAY","YESTERDAY","LAST_7_DAYS","LAST_14_DAYS","LAST_30_DAYS","LAST_BUSINESS_WEEK","LAST_WEEK_SUN_SAT","LAST_WEEK_MON_SUN","THIS_WEEK_SUN_TODAY","THIS_WEEK_MON_TODAY","THIS_MONTH","LAST_MONTH","ALL_TIME"]
at main (Code:6:39)
So we added this workaround to still have it check hourly. Unfortunately now we get this error: Ba: Could not read from Google Ads
at main (Code:6:51)
My question is twofold:
1. Does the script look oke, or does anyone have an simpler solution for receiving alerts when there are no conversions in the past hour?
2. If so, how can we solve the error?
Your help is much appreciated!
Kind regards,
SB