Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checks for AdWords ad groups with fewer than 2 ads per ad group
var RECIPIENT_EMAIL = 'YOUR@EMAILADDRESS.COM';
var MAX_ACCOUNTS = 20;
var EMAIL_SUBJECT = "Checking for ad groups with fewer than 2 ads.";
function main() {
var accountSelector = MccApp.accounts();
accountSelector
.withLimit(MAX_ACCOUNTS)
.withCondition("LabelNames CONTAINS 'Monitor'")
.executeInParallel('processAccount', 'afterProcessAllClientAccounts');
}
function processAccount() {
try{
var accountName = AdWordsApp.currentAccount().getName();
var accountId = AdWordsApp.currentAccount().getCustomerId();
Logger.log(accountName + " (" + accountId + ")" + " - Checking account");
var results = "";
var agIterator = AdWordsApp.adGroups()
.withCondition("CampaignStatus = ENABLED")
.withCondition("Status = ENABLED")
.get();
var bFewerThanTwoAds = false;
var strNotes = "";
while (agIterator.hasNext()) {
var ag = agIterator.next();
var agName = ag.getName();
var ads = ag.ads().withCondition("Status = ENABLED").get();
var numAds = ads.totalNumEntities();
if (numAds < 2) {
Logger.log(accountName + " (" + accountId + ") - fewer than two ads in this campaign and ad group: " + ag.getCampaign().getName() + " -> " + agName);
strNotes = strNotes + "&nbsp;&nbsp;&nbsp;&nbsp;fewer than two ads in this campaign and ad group: <strong><i>" + ag.getCampaign().getName() + " -> " + agName + "</i></strong><br />";
bFewerThanTwoAds = true;
}
}
Logger.log(strNotes);
// return a result, as a text.
var jsonObj = {
"CheckedClientAccount": accountName + " (" + accountId + ")",
"bFewerThanTwoAds": bFewerThanTwoAds,
"Notes": strNotes,
"AllLogData": Logger.getLog()
};
return JSON.stringify(jsonObj);
} catch(e) {
MailApp.sendEmail(RECIPIENT_EMAIL,EMAIL_SUBJECT, e + "\n\n" + results + "\n\n");
}
}
function afterProcessAllClientAccounts(results) {
var AllLogData = [];
try{
for (var i = 0; i < results.length; i++) {
var resultObj = JSON.parse(results[i].getReturnValue());
var strCheckedClientAccount = ""
var bFewerThanTwoAds = false;
var strNotes = "";
//Store which accounts we just checked:
strCheckedClientAccount = resultObj.CheckedClientAccount
bFewerThanTwoAds = resultObj.bFewerThanTwoAds;
strNotes = resultObj.Notes;
if (bFewerThanTwoAds == false){
strCheckedClientAccount = strCheckedClientAccount + " - NO - Could not find any ad groups that contained fewer than two ads per ad group.<br />";
Logger.log(strCheckedClientAccount);
} else {
strCheckedClientAccount = strCheckedClientAccount + " - <strong><font color=\"red\">YES</font></strong> - there were ad groups that contained fewer than two ads:<br />" + strNotes;
Logger.log(strCheckedClientAccount);
}
AllLogData.push(strCheckedClientAccount);
}
var myBody = "I just scanned all accounts with the label Monitor for ad groups that contain fewer than two ads.<br /><br />" +
"Here is what I found:<br /><br />" +
AllLogData.sort().join("<br />");
// Process your client account here.
if (RECIPIENT_EMAIL != '') {
//now send.
Logger.log("Sending email to %s this is the body: %s",RECIPIENT_EMAIL, myBody);
MailApp.sendEmail({
to: RECIPIENT_EMAIL,
subject: EMAIL_SUBJECT,
htmlBody: myBody
});
}
} catch(e) {
MailApp.sendEmail(RECIPIENT_EMAIL,EMAIL_SUBJECT, e + "\n\n" + results + "\n\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment