Hi all,
I'm not sure if this has been covered recently, as I couldn't find a post covering it?
I've been trying to find a solution for my ad script not picking up disapproved ads for enabled ads? When I've run a preview and run the script, it pings an email back after a couple of minutes with spreadsheets, but with 0 disapproved ads. To test it, we had hidden an ad with "Destination not working" & "unacceptable spacing" errors in one of the accounts, which Adwords is showing as disapproved, however the script hadn't found it this morning
I'm fairly new to ad scripts and have a basic understanding of JavaScript, so I was wondering if I've missed something somewhere, whether it's in the searching section or writing to email/spreadsheet bit?
It was run at MCC level and is a Russell Savage script from FreeAdwordScripts.com.
Script below:
Thanks,
Matthew
var SCRIPT_NAME = 'TB - Disapproved ads overview - MCC level';
var NOTIFY = [' ',' ',' ']; //removed email addresses for this post
var ACCOUNTS_FILE_NAME = 'AdWords-Scripts-DisapprovedAds-AccountList.json';
var SPREADSHEET_PREFIX = 'AdWords-DisapprovedAds-';
function main() {
var accountsList = readJSONFile(ACCOUNTS_FILE_NAME);
if(!accountsList) { accountsList = []; }
if(accountsList.length === 0) {
var acctIter = MccApp.accounts().get();
while(acctIter.hasNext()) {
var acct = acctIter.next();
accountsList.push({ id : acct.getCustomerId(),
lastChecked : null });
}
}
accountsList.sort(function(a,b) {
if(a.lastChecked < b.lastChecked) {
return -1;
} else if(a.lastChecked > b.lastChecked) {
return 1;
} else {
return 0;
}
});
writeJSONFile(ACCOUNTS_FILE_NAME,accountsList);
var toCheck = [];
for(var i in accountsList) {
toCheck.push(accountsList[i].id);
if(toCheck.length==50) {
break;
}
}
Logger.log('Checking the following accounts: '+JSON.stringify(toCheck));
MccApp.accounts().withIds(toCheck).executeInParallel('checkAdDisapprovalReasons', 'reportResults');
}
function checkAdDisapprovalReasons() {
Logger.log('Processing account: '+AdWordsApp.currentAccount().getName());
var adIter = AdWordsApp.ads().withCondition('CampaignStatus = ENABLED')
.withCondition('AdGroupStatus = ENABLED')
.withCondition('Status = ENABLED')
.withCondition('ApprovalStatus = DISAPPROVED')
.withCondition("Impressions > 1")
.forDateRange("LAST_30_DAYS")
.get();
var results = [];
while(adIter.hasNext()) {
var ad = adIter.next();
results.push({
camp:ad.getCampaign().getName(),
ag:ad.getAdGroup().getName(),
headline:ad.getHeadlinePart1(),
desc1:ad.getHeadlinePart2(),
desc2:ad.getDescription(),
displayUrl:ad.getPath1(),
destUrl:(ad.urls())?ad.urls().getFinalUrl():'',
disapprovalReasons:ad.getDisapprovalReasons().join('. ')
});
}
return JSON.stringify({
accountId : AdWordsApp.currentAccount().getCustomerId(),
accountName : AdWordsApp.currentAccount().getName(),
disapprovedAdsCount : results.length,
disapprovedAds : results
});
}
function reportResults(responses) {
var accountsList;
var indexMap = {};
accountsList = readJSONFile(ACCOUNTS_FILE_NAME);
for(var i = 0; i < accountsList.length; i++) {
indexMap[accountsList[i].id] = i;
}
var summaryEmailData = [];
var dateTimeStr = Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), 'yyyy-MM-dd HH:m:s');
var spreadsheetName = SPREADSHEET_PREFIX+' - '+dateTimeStr;
for(var i in responses) {
if(!responses[i].getReturnValue()) { continue; }
var res = JSON.parse(responses[i].getReturnValue());
var sheetUrl = writeResultsToSpreadsheet(res,spreadsheetName);
summaryEmailData.push({accountId:res.accountId,
accountName:res.accountName,
disapprovedAdsCount:res.disapprovedAdsCount,
sheetUrl:sheetUrl});
accountsList[indexMap[res.accountId]].lastChecked = dateTimeStr;
}
if(summaryEmailData.length > 0) {
sendSummaryEmail(summaryEmailData);
}
writeJSONFile(ACCOUNTS_FILE_NAME,accountsList);
}
function sendSummaryEmail(summaryEmailData) {
var subject = SCRIPT_NAME+' Summary Results';
var body = subject;
var htmlBody = '<html><body>'+subject;
htmlBody += '<br/ ><br/ >';
htmlBody += '<table border="1" width="95%" style="border-collapse:collapse;">';
htmlBody += '<tr>';
htmlBody += '<td align="left"><b>Acct Id</b></td>';
htmlBody += '<td align="left"><b>Acct Name</b></td>';
htmlBody += '<td align="center"><b>Disapproved Ads Found</b></td>';
htmlBody += '<td align="center"><b>Full Report</b></td>';
htmlBody += '</tr>';
for(var i in summaryEmailData) {
var row = summaryEmailData[i];
htmlBody += '<tr><td align="left">'+ row.accountId +
'</td><td align="left">' + row.accountName +
'</td><td align="center">' + row.disapprovedAdsCount +
'</td><td align="left"><a href="'+row.sheetUrl+'">' + 'Show Details' +
'</a></td></tr>';
}
htmlBody += '</table>';
htmlBody += '<br/ >';
htmlBody += Utilities.formatDate(new Date(),AdWordsApp.currentAccount().getTimeZone(),'MMMM dd, yyyy @ hh:mma z');
htmlBody += '. Completed. '+Object.keys(summaryEmailData).length+' Accounts checked.<br/ >';
htmlBody += '</body></html>';
var options = { htmlBody : htmlBody };
for(var i in NOTIFY) {
MailApp.sendEmail(NOTIFY[i], subject, body, options);
}
}
function writeResultsToSpreadsheet(res,name) {
var file = getFile(name,true);
var spreadsheet;
var maxRetries = 3;
while(maxRetries > 0) {
try {
spreadsheet = SpreadsheetApp.openById(file.getId());
break;
} catch(e) {
maxRetries--;
Utilities.sleep(1000);
}
}
if(!spreadsheet) { throw 'Could not open file: '+name; }
if(spreadsheet.getSheetByName('Sheet1')) {
spreadsheet.getSheetByName('Sheet1').setName(res.accountId);
}
var sheet = spreadsheet.getSheetByName(res.accountId);
if(!sheet) {
sheet = spreadsheet.insertSheet(res.accountId, spreadsheet.getSheets().length);
}
var toWrite = [['Disapproval Reasons','Campaign','AdGroup','Headline 1','Headline 2','Description','Path 1','Final URL']];
for(var i in res.disapprovedAds) {
var row = res.disapprovedAds[i];
toWrite.push([row.disapprovalReasons,
row.camp,
row.ag,
row.headline,
row.desc1,
row.desc2,
row.displayUrl,
row.destUrl]);
}
var lastRow = sheet.getLastRow();
var numRows = sheet.getMaxRows();
if((numRows-lastRow) < toWrite.length) {
sheet.insertRowsAfter(lastRow,toWrite.length-numRows+lastRow);
}
var range = sheet.getRange(lastRow+1,1,toWrite.length,toWrite[0].length);
range.setValues(toWrite);
if((sheet.getMaxColumns() - sheet.getLastColumn()) > 0) {
sheet.deleteColumns(sheet.getLastColumn()+1, sheet.getMaxColumns() - sheet.getLastColumn());
}
file = DriveApp.getFileById(spreadsheet.getId());
try {
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
} catch(e) {
file.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);
}
return (spreadsheet.getUrl() + '#gid=' + sheet.getSheetId());
}
function writeJSONFile(fileName,toWrite) {
var file = getFile(fileName,false);
file.setContent(JSON.stringify(toWrite));
}
function readJSONFile(fileName) {
var file = getFile(fileName,false);
var fileData = file.getBlob().getDataAsString();
if(fileData) {
return JSON.parse(fileData);
} else {
return null;
}
}
function getFile(fileName,isSpreadsheet) {
var maxRetries = 3;
var errors = [];
while(maxRetries > 0) {
try {
var fileIter = DriveApp.getFilesByName(fileName);
if(!fileIter.hasNext()) {
Logger.log('Could not find file: '+fileName+' on Google Drive. Creating new file.');
if(isSpreadsheet) {
return SpreadsheetApp.create(fileName);
} else {
return DriveApp.createFile(fileName,'');
}
} else {
return fileIter.next();
}
} catch(e) {
errors.push(e);
maxRetries--;
Utilities.sleep(1000);
}
}
if(maxRetries === 0) {
throw errors.join('. ');
}
}
* @author Russell Savage <russel...@gmail.com>
--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/72b72208-f764-4e05-ab67-2a27ed4841d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.