Last active
August 23, 2018 10:14
-
-
Save nakitadog/fcc4b04091af140ef62243893ab84dc7 to your computer and use it in GitHub Desktop.
Checks for AdWords ad groups with fewer than 2 ads per ad group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RECIPIENT_EMAIL = '[email protected]'; | |
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 + " 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