Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakitadog/27b3ccf7f1692709dc0a1919e310ba30 to your computer and use it in GitHub Desktop.
Save nakitadog/27b3ccf7f1692709dc0a1919e310ba30 to your computer and use it in GitHub Desktop.
This script should be run daily to check all your client AdWords accounts within your MCC account for automated ads by Google.
var RECIPIENT_EMAIL = 'YOUR@EMAILADDRESS.com';
var MAX_ACCOUNTS = 20;
var EMAIL_SUBJECT = "Automated ads by Google - Checked monitored accounts";
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 report = AdWordsApp.report(
'SELECT CampaignName, Clicks, Impressions, Cost, Automated, HeadlinePart1, HeadlinePart2, Description, Id, AdType ' +
'FROM AD_PERFORMANCE_REPORT ' +
'WHERE Automated = true ' +
'AND CampaignStatus = ENABLED ' +
'AND AdGroupStatus = ENABLED ' +
'AND Status = ENABLED ' +
'DURING LAST_7_DAYS');
var rows = report.rows();
var rowCount = 0;
while (rows.hasNext()) {
var row = rows.next();
var campaignName = row['CampaignName'];
var clicks = row['Clicks'];
var impressions = row['Impressions'];
var cost = row['Cost'];
var automated = row['Automated'];
var HeadlinePart1 = row['HeadlinePart1'];
var HeadlinePart2 = row['HeadlinePart2'];
var Description = row['Description'];
var Id = row['Id'];
var AdType = row['AdType'];
//Logger.log(campaignName + ',' + clicks + ',' + impressions + ',' + cost + ',' + automated + ',' + HeadlinePart1 + ',' + Id + ',' + AdType);
rowCount++;
}
var bHadAutomatedAds = false;
if (rowCount === 0){
//Logger.log(accountName+" (" + accountId + ")" + " - NO Automated ads within this account");
bHadAutomatedAds = false;
} else {
//Logger.log(accountName+" (" + accountId + ")" + " - YES there were " + rowCount + " Automated ads within this account");
bHadAutomatedAds = true;
}
var jsonObj = {
"CheckedClientAccount": accountName + "(" + accountId + ")",
"bHadAutomatedAds": bHadAutomatedAds,
"NumberOfAutomatedAds": rowCount,
"AllLogData": Logger.getLog()
};
// return a result, as a text.
return JSON.stringify(jsonObj);
} catch(e) {
MailApp.sendEmail(RECIPIENT_EMAIL,EMAIL_SUBJECT, e + "\n\n" + results + "\n\n");
}
}
function afterProcessAllClientAccounts(results) {
var bHadAutomatedAds = false;
var NumberOfAutomatedAds = 0;
var AllLogData = [];
try{
for (var i = 0; i < results.length; i++) {
var resultObj = JSON.parse(results[i].getReturnValue());
var CheckedClientAccount = "";
//Store which accounts we just checked:
CheckedClientAccount = resultObj.CheckedClientAccount
bHadAutomatedAds = resultObj.bHadAutomatedAds;
NumberOfAutomatedAds = resultObj.NumberOfAutomatedAds;
if (bHadAutomatedAds == false){
CheckedClientAccount = CheckedClientAccount + " - NO Automated ads within this account.";
Logger.log(CheckedClientAccount);
} else {
CheckedClientAccount = CheckedClientAccount + " - YES there were " + NumberOfAutomatedAds + " Automated ads within this account.";
Logger.log(CheckedClientAccount);
}
AllLogData.push(CheckedClientAccount);
}
var myBody = "I just scanned all accounts with the label Monitor for Google's Automated ads.\n\n" +
"Here is what I found:\n\n" +
AllLogData.sort().join("\n\n");
// 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(RECIPIENT_EMAIL,EMAIL_SUBJECT, myBody + "\n\n");
}
} 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