Ad URL Validator

// This script works at the MCC (Manager) level. It goes through each ad Final URL of the selected Manager child
// accounts in order to validate that the landing page is up and running. It will send a report by email if a
// landing page returns an error code. For best results, schedule this script to run hourly.
var SEND_TO = ['email@website.com']; // Email(s) where to send the report
var SUBJECT = 'Url Report - ' + getDateString(); // Email Title
// The accounts under the MCC that will have their queries analyzed. One per array element.
var MCC_ACCOUNT_IDS = [
"123-456-7890" // Example
];
function main() {
var a = [];
getDateString();
for (var d = {muteHttpExceptions:!0}, b = {}, c = [], e = [], m = MccApp.accounts().withIds(MCC_ACCOUNT_IDS).get(); m.hasNext();) {
var k = m.next();
MccApp.select(k);
for (var f = 400; 599 >= f; f++) {
a.push(f);
}
f = AdWordsApp.ads().withCondition("Status = 'ENABLED'").withCondition("AdGroupStatus = 'ENABLED'").withCondition("CampaignStatus = 'ENABLED'").get();
for (Logger.log("Number of items: " + f.totalNumEntities()); f.hasNext();) {
var h = f.next(), g = h.urls().getFinalUrl(), p = h.getCampaign();
g.indexOf("{");
if (!b[g]) {
try {
Logger.log("Testing url: '" + g + "' of: '" + p + "'");
var n = UrlFetchApp.fetch(g, d).getResponseCode();
} catch (l) {
Logger.log("Error details:\nName: " + l.name + "\nMessage: " + l.message + "\nStack: " + l.stack), e.push({e:h, code:-1, account:k});
}
0 <= a.indexOf(n) && c.push({e:h, code:n, account:k});
b[g] = !0;
}
}
}
d = "Type Account CampaignName AdGroupName Id Headline ResponseCode FinalUrl".split(" ");
a = d.join(",") + "\n";
d = d.join(",") + "\n";
for (var q in c) {
a += formatResults(c[q], ",");
}
for (var r in e) {
d += formatResults(e[r], ",");
}
if (0 < c.length) {
e = {attachments:[Utilities.newBlob(a, "text/csv", "bad_urls_" + getDateString() + ".csv")]};
c = "There are " + c.length + " urls that are broken. See attachment for details.";
for (var t in SEND_TO) {
MailApp.sendEmail(SEND_TO[t], SUBJECT, c, e);
}
}
}
function formatResults(a, d) {
var b = a.e, c = a.code, e = a.account;
return [b.getEntityType(), e.getCustomerId(), b.getCampaign().getName(), b.getAdGroup().getName(), b.getId(), b.getHeadline(), c, b.urls().getFinalUrl()].join(d) + "\n";
}
function getDateString() {
return Utilities.formatDate(new Date, AdWordsApp.currentAccount().getTimeZone(), "yyyy-MM-dd");
}