The first script run will throw an ERROR, as you have to enable the Analytics API via the cloud console.
Click on the link provided in your script log and hit the ENABLE button, then RERUN the script.
/***** START CONFIGURATION ******/ | |
var MONITORED_GA_PROFILES = [[11111111, "Shop 1"],[2222222222, "Shop 2"],[333333333, "Shop 3"]]; | |
var RECIPIENTS = ["name@email.com","name2@email.com"]; | |
var SCRIPT_NAME = "nrHourlyRevenueChecker"; | |
/***** END CONFIGURATION ********/ | |
// IMPORTANT: ENABLE THE GOOGLE ANALYTICS API in the ADVANCED APIs DROPDOWN | |
function main() { | |
for(var i=0;i<MONITORED_GA_PROFILES.length;i++){ | |
try{ | |
var results = getReportDataForProfile(MONITORED_GA_PROFILES[i][0]); | |
if(checkIfAlertNecessary(results) === true) sendAlertMail(MONITORED_GA_PROFILES[i], results); | |
} | |
catch(error) {Logger.log(error.message)}; | |
} | |
} | |
function checkIfAlertNecessary(results){ | |
var alertNecessary = false; | |
var lastHour = new Date().getUTCHours()-1; | |
if (lastHour>7 && lastHour<22) if(results.rows[lastHour][1] == 0 && results.rows[lastHour+1][1] == 0) alertNecessary = true; | |
Logger.log("Hour : " + results.rows[lastHour][0] + " | Revenue : " + results.rows[lastHour-1][1]); | |
Logger.log("Hour : " + results.rows[lastHour+1][0] + " | Revenue : " + results.rows[lastHour+1][1]);Logger.log(""); | |
Logger.log("alertNecessary? " + alertNecessary); | |
return alertNecessary; | |
} | |
function sendAlertMail(ProfileArray, results) { | |
var subject = 'WARNING: ' + SCRIPT_NAME +' | ' + ProfileArray[1] + ' | NO GA REVENUE for at least 2 hours!'; | |
var body = subject; | |
var htmlBody = '<html><body><br> >>> WARNING for ' + ProfileArray[1] + ' | NO Google Analytics (GA) Revenue for at least 2 hours!';; | |
htmlBody += '<br><br> GA Account: <b>' + ProfileArray[1] + '</b> [ProfileID:' + ProfileArray[0] + ']<br>'; | |
htmlBody += '<br> >>> PLEASE check your GA ecommerce tracking implementation <b>IMMEDIATELY!</b><br><br><br>-----'; | |
htmlBody += "<br> See today's <b>hourly revenue</b> from GA below:"; | |
htmlBody += '<table border="1" width="20%" style="border-collapse:collapse;">'; | |
htmlBody += '<tr><td align="center"><b>Hour</b></td>'; | |
htmlBody += '<td align="center"><b>Revenue</b></td><tr>'; | |
var lastHour = new Date().getHours()-1; | |
for(var i=0; i<results.rows.length; i++) { | |
if(i>lastHour) continue; | |
htmlBody += "<tr>"; | |
for(var j=0; j<results.rows[i].length; j++){ | |
htmlBody += "<td>"+results.rows[i][j]+"</td>"; | |
} | |
htmlBody += "</tr>"; | |
} | |
htmlBody += '<br/><br/></table></html></body>'; | |
var options = { htmlBody : htmlBody }; | |
for(var i in RECIPIENTS) { | |
MailApp.sendEmail(RECIPIENTS[i], subject, body, options); | |
Logger.log('Email sent to ' + RECIPIENTS[i]); | |
} | |
} | |
function getReportDataForProfile(GA_PROFILE_ID) { | |
var profileId = GA_PROFILE_ID; | |
var tableId = 'ga:' + profileId; | |
var startDate = getLastNdays(0); | |
var endDate = getLastNdays(0); // Today. | |
var optArgs = { | |
'dimensions': 'ga:nthHour', // Comma separated list of dimensions. | |
'sort': 'ga:nthHour', | |
'start-index': '1', | |
'max-results': '50000' | |
}; | |
// Make a request to the API. | |
var results = Analytics.Data.Ga.get( | |
tableId, // Table id (format ga:xxxxxx). | |
startDate, // Start-date (format yyyy-MM-dd). | |
endDate, // End-date (format yyyy-MM-dd). | |
'ga:transactionRevenue', // Comma seperated list of metrics. | |
optArgs); | |
if (results.getRows()) {return results;} else {throw new Error('No views (profiles) found');} | |
} | |
function getLastNdays(nDaysAgo) { | |
var today = new Date(); | |
var before = new Date(); | |
before.setDate(today.getDate() - nDaysAgo); | |
return Utilities.formatDate(before, 'GMT', 'yyyy-MM-dd'); | |
} |
The first script run will throw an ERROR, as you have to enable the Analytics API via the cloud console.
Click on the link provided in your script log and hit the ENABLE button, then RERUN the script.