Skip to content

Instantly share code, notes, and snippets.

@abfo
Created September 30, 2018 21:42
Show Gist options
  • Select an option

  • Save abfo/77293f726cd9440118ced9bcb10d5ef4 to your computer and use it in GitHub Desktop.

Select an option

Save abfo/77293f726cd9440118ced9bcb10d5ef4 to your computer and use it in GitHub Desktop.
var urlToMonitor = '';
var alertEmail = '';
var runTestKey = '';
var runTestUrl = 'https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=';
function mobileFriendlyMonitor() {
try {
var postBody = {
'url' : urlToMonitor
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(postBody)
};
var response = UrlFetchApp.fetch(runTestUrl + runTestKey, options);
var json = response.getContentText();
var mobileFriendlyResult = JSON.parse(json);
if (mobileFriendlyResult.mobileFriendliness != 'MOBILE_FRIENDLY') {
sendEmail('Mobile friendly test failed for ' + urlToMonitor + ', check https://search.google.com/search-console/mobile-friendly for details');
}
} catch (e) {
sendEmail('mobileFriendlyMonitor failed for: ' + urlToMonitor + ' with error: ' + e.message);
}
}
function sendEmail(msg) {
MailApp.sendEmail(alertEmail, 'Mobile Friendly Monitor Alert on ' + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'"), msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment