Skip to content

Instantly share code, notes, and snippets.

@derekmartinla
Created April 14, 2015 16:25
Show Gist options
  • Save derekmartinla/75decfcd2da85dbfeb6a to your computer and use it in GitHub Desktop.
Save derekmartinla/75decfcd2da85dbfeb6a to your computer and use it in GitHub Desktop.
Use Twilio & Google Docs To Check For Disapproved Ads
/***************************************************************************************
* AdWords Account Audit -- Check Ads for disapprovals -- text if there are open issues
* Version 1.0
* Created By: Derek Martin
* DerekMartinLA.com
****************************************************************************************/
// This script was heavily inspired by Russell Savage so all credit where its due!
// Sign up at Twilio.com and get an API key (sid) and Auth code and place here
// Url: https://www.twilio.com/
var sid = 'ACxxxxxxx'; // looks like this:
var auth = 'xxxxxxxx';
var to_number = '+1234567890'; // put your phone number here
var from_number = '+1234567890'; // this is the phone number given to you by Twilio
var email_address = '[email protected]'; // put the email you want to send to here
function main() {
var campIter = AdWordsApp.campaigns().withCondition('Status = ENABLED').get();
var disapprovedList = [];
while (campIter.hasNext()) {
var camp = campIter.next();
adsIter = camp.ads().withCondition('Status = ENABLED').withCondition('ApprovalStatus = DISAPPROVED').get();
info('Checking ' + camp.getName());
numDisapproved = adsIter.totalNumEntities();
if (numDisapproved > 0) { // there are disapproved ads
while (adsIter.hasNext()) {
var ad = adsIter.next();
warn('Ad ' + ad.getId() + ' : Headline : ' + ad.getHeadline() + ' is currently DISAPPROVED.');
disapproved = new disapprovedAd(camp.getName(), ad.getAdGroup().getName(), ad.getHeadline(), ad.getDescription1(), ad.getDescription2(), ad.getDisplayUrl(), ad.getDestinationUrl(), ad.getDisapprovalReasons());
// info(camp.getName() + ' ' + ad.getAdGroup().getName() + ' ' + ad.getHeadline() + ' ' + ad.getDescription1() + ' ' + ad.getDescription2() + ' ' + ad.getDisapprovalReasons());
disapprovedList.push(disapproved);
} // dont do anything if disapproved ads are zero
// info('hitting the else statement');
// ads are collected, text message
// prepare spreadsheet and email and then text msg
info('Collected all disapproved ads, expect an email with report results momentarily');
var fileUrl = createSpreadsheet(disapprovedList);
info(' ');
info('Or you can find it here: ' + fileUrl);
var clientName = AdWordsApp.currentAccount().getName().split("-");
sendAnEmail(clientName[0], disapprovedList.toString(), fileUrl);
// The third parameter is what you want the text or voice message to say
var client = new Twilio(sid,auth);
var clientName = AdWordsApp.currentAccount().getName().split();
var warningMsg = 'ALERT: ' + clientName[0] + ' has ' + numDisapproved + ' disapproved ads in campaign: ' + camp.getName() + '\n' + '\n';
warningMsg += 'Click here: ' + fileUrl + ' to review the disapproved list.';
client.sendMessage(to_number,from_number,warningMsg);
} // end of campaign iteration
}
}
function createSpreadsheet(results) {
var newSS = SpreadsheetApp.create('disapprovedAds', results.length, 26);
var sheet = newSS.getActiveSheet();
var resultList = results;
var columnNames = ["Campaign", "Ad Group","Headline", "Description1","Description2","Display URL","Destination URL","Disapproval Reason"];
var headersRange = sheet.getRange(1, 1, 1, columnNames.length);
headersRange.setValues([columnNames]);
var i = 0;
for each (headline in resultList) {
var campaign, adgroup, headline, description1, description2, displayUrl, destinationUrl, disapprovalReasons;
campaign = resultList[i].campaign;
adgroup = resultList[i].adgroup;
headline = resultList[i].headline;
description1 = resultList[i].d1;
description2 = resultList[i].d2;
displayUrl = resultList[i].displayUrl;
destinationUrl = resultList[i].destinationUrl;
disapprovalReasons = resultList[i].disapprovalReasons;
sheet.appendRow([campaign, adgroup, headline, description1, description2, displayUrl, destinationUrl, disapprovalReasons]);
// Sets the first column to a width which fits the text
sheet.setColumnWidth(1, 300);
sheet.setColumnWidth(2, 300);
sheet.setColumnWidth(3, 300);
sheet.setColumnWidth(4, 300);
sheet.setColumnWidth(5, 300);
sheet.setColumnWidth(6, 300);
sheet.setColumnWidth(7, 300);
sheet.setColumnWidth(8, 300);
i++;
}
return newSS.getUrl();
}
function sendAnEmail (clientName, results, fileUrl) {
var data = Utilities.parseCsv(results, '\t');
var clientName = AdWordsApp.currentAccount().getName().split();
var today = new Date();
today = today.getMonth() + today.getDate() + today.getFullYear();
var filename = clientName[0] + 'disapproved-ads' + today;
// Send an email with Search list attachment
var blob = Utilities.newBlob(results, 'text/html', '');
MailApp.sendEmail(email_address, clientName + ' Disapproved Ad Results ', 'You can find the list of disapproved ads at the following URL:' + fileUrl, {
name: ' Client Disapproved Ads'
});
}
function disapprovedAd(campaign, adgroup, headline, d1, d2, displayUrl, destinationURL, reason) {
this.campaign = campaign;
this.adgroup = adgroup;
this.headline = headline;
this.d1 = d1;
this.d2 = d2;
this.displayUrl = displayUrl;
this.destinationUrl = destinationURL;
this.reason = reason;
}
// HELPER FUNCTIONS
function warn(msg) {
Logger.log('WARNING: '+msg);
}
function info(msg) {
Logger.log(msg);
}
// TWILIO ACCESS
function Twilio(e,t){function n(e){return{Authorization:"Basic "+Utilities.base64Encode(e.ACCOUNT_SID+":"+e.AUTH_TOKEN)}}this.ACCOUNT_SID=e;this.AUTH_TOKEN=t;this.MESSAGES_ENDPOINT="https://api.twilio.com/2010-04-01/Accounts/"+this.ACCOUNT_SID+"/Messages.json";this.CALLS_ENDPOINT="https://api.twilio.com/2010-04-01/Accounts/"+this.ACCOUNT_SID+"/Calls.json";this.sendMessage=function(e,t,r){var i={method:"POST",payload:{To:e,From:t,Body:r},headers:n(this)};var s=UrlFetchApp.fetch(this.MESSAGES_ENDPOINT,i).getContentText();return JSON.parse(s)["sid"]};this.makeCall=function(e,t,r){var i="http://proj.rjsavage.com/savageautomation/twilio_script/dynamicSay.php?alert="+encodeURIComponent(r);var s={method:"POST",payload:{To:e,From:t,Url:i},headers:n(this)};var o=UrlFetchApp.fetch(this.CALLS_ENDPOINT,s).getContentText();return JSON.parse(o)["sid"]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment