Skip to content

Instantly share code, notes, and snippets.

@russorat
Created November 13, 2015 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save russorat/671117adc4c7c2801399 to your computer and use it in GitHub Desktop.
Save russorat/671117adc4c7c2801399 to your computer and use it in GitHub Desktop.
/***********
* Collects the reporting results from all accounts
* and generates a nicely formatted email. If there
* are errors for an account, it includes those
* in the email as well since an error in one account
* won't stop the entire script.
***********/
function generateReport(results) {
var NOTIFY = ['your_email@example.com'];
var total_deleted = 0;
var total_keywords = 0;
// This is some explanation that will go in the body of the email.
var email_html = '<p>This is a summary of the keywords that '+
'are in danger of or have already been deleted. '+
'You can stop the deletion process by removing '+
'the countdown label and applying the "Save" '+
'label to the keyword.</p>';
var htmlTables = [];
var errors = [];
for (var i = 0; i < results.length; i++) {
// If there was an error in the account, let's add it to the email
if(results[i].getStatus() != 'OK') {
errors.push(results[i].getCustomerId() + ': ' + results[i].getError());
continue;
}
// Otherwise, we pull out the results and format them as needed.
var object = JSON.parse(results[i].getReturnValue());
if(!object) { continue; }
htmlTables.push(generateHtmlTable(object));
// Here we are keeping track of the totals to use
// in the subject line later.
total_keywords += object.total_keywords;
total_deleted += object.keywords_deleted;
}
if(errors) {
email_html += '<p>The following accounts returned an error</p>';
email_html += '<p>'+errors.join('<br/>')+'</p>';
}
email_html += htmlTables.join('');
email_html += '<p>Generated on: '+
Utilities.formatDate(new Date(),AdWordsApp.currentAccount().getTimeZone(),'MMMM dd, yyyy @ hh:mma z')+
' by the Keyword Cleanup Script.</p>';
// Let's build our subject line as needed
var subject = 'Keyword Cleanup Script';
if(total_deleted) {
subject += ' - ' + total_deleted+' KWs Deleted';
}
if(total_keywords) {
subject += ' - ' + total_keywords+' Will Be Deleted Soon';
}
if(errors) {
subject += ' - ' + errors.length +' Account Errors';
}
// And finally, we send the emails.
for(var i in NOTIFY) {
MailApp.sendEmail(NOTIFY[i], subject, 'See html body.', { htmlBody : email_html });
}
}
// This helper function takes a set of results
// and returns nicely formatted HTML for a table.
// You can change the formatting as needed.
function generateHtmlTable(results) {
var retVal = [];
retVal.push('<p>');
retVal.push('<table border="1" width="50%" style="border-collapse:collapse;">');
retVal.push('<tr>')
retVal.push('<td width="40%">'+results.account_id+'</td><td>'+results.account_name+'</td>');
retVal.push('</tr>')
retVal.push('<tr>')
retVal.push('<td width="40%">Deleted Keywords</td><td>'+results.keywords_deleted+'</td>');
retVal.push('</tr>')
var labels = Object.keys(results.label_stats).sort();
for(var i in labels) {
retVal.push('<tr>')
retVal.push('<td width="40%">'+labels[i]+'</td><td>'+results.label_stats[labels[i]]+'</td>');
retVal.push('</tr>')
}
retVal.push('</table>');
retVal.push('</p>');
return retVal.join('');
}
/***********
* This function returns a summary of the changes
* we made to the account which can then be formatted
* and emailed as needed.
***********/
function generateSummaryData(changes_to_make) {
var summaryData = {
// Account information
account_id: AdWordsApp.currentAccount().getCustomerId(),
account_name: AdWordsApp.currentAccount().getName(),
// Keywords we deleted (if any)
keywords_deleted: changes_to_make.kw_to_delete.length,
// Keywords that will soon be deleted (if any)
total_keywords: 0,
// Counts for each label
label_stats : {}
};
for(var i in changes_to_make.labels_to_add) {
var label_name = changes_to_make.labels_to_add[i].label;
if(!summaryData.label_stats[label_name]) { summaryData.label_stats[label_name] = 0; }
summaryData.label_stats[label_name]++;
summaryData.total_keywords++;
}
return summaryData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment