/* README BEFORE EXECUTING THIS SCRIPT. Note the line numbers may change if you delete
a line.
* Recommend to leave the line blank to avoid confusion. Modify this script only after
copying to your local account.
* Modify the Code line 13 to set the correct email id where to receive the report
* Modify the Code line 21. (Set it to true if your account is an MCC account, else set it to
false).
*
* FUNCTIONALITY: Checks the broken links for the destination URLs in Keywords whose
response codes are
* other than 200 (SUCCESS), 301 & 302 (URL Redirection)
* Modify the Code line 43 if you would like to change above response codes. URL Auditor
functionality.
*/
//Use commas to seperate recipients
//Broken Urls list will be mailed to this id with attachment
var email_recipients = 'vikramreddy.tekmal@netelixir.com';
var email_recipients = 'sinivas.iyer@netelixir.com';
var email_recipients = 'dheeraj.sr@netelixir.com';
/** This Script is used to test the condition of keywords destination urls only
* If any one need more customized please contact Tech Team.
* The validations are done to only active Keywords under active AdGroup and Campaign Only
*/
//Is your account a MCC account
var IS_MCC_ACCOUNT = false;
function main() {
if(!IS_MCC_ACCOUNT){
brokenLinkChecker();
brokenAdsLinkChecker();
}else{
var childAccounts = getManagedAccounts();
while(childAccounts .hasNext()) {
var childAccount = childAccounts.next()
AdsManagerApp.select(childAccount);
brokenLinkChecker();
brokenAdsLinkChecker();
}

}
}
function getManagedAccounts() {
var accountSelector = AdsManagerApp.accounts();
return accountSelector.get();
}

function brokenLinkChecker() {
//HTTP Response codes which are treated as good/correct links
var validCodes = [200,301,302];
var urls = [];
var badUrls = [];
var badCodes = [];
var keywordIterator = AdsApp.keywords()
.withCondition("Status = ENABLED")
.withCondition("AdGroupStatus = ENABLED")
.withCondition("CampaignStatus = ENABLED")
.get();
while(keywordIterator.hasNext()){
var keyword = keywordIterator.next();
var destinationUrl = keyword.urls().getFinalUrl(); //old API keyword.getDestinationUrl()
if(destinationUrl !== null && destinationUrl !== ""){
var url = destinationUrl.split('?')[0];
if(urls.indexOf(url) === -1){
urls.push(url);
}
}
}
var urlFetchOptions = {muteHttpExceptions: true};

for(var x = 0; x < urls.length; x++){
try{
var response = UrlFetchApp.fetch(urls[x],urlFetchOptions);
var code = response.getResponseCode();
} catch(err){
Logger.log("The Url " + urls[x] + " could not be processed");
}
if(validCodes.indexOf(code) === -1){

Logger.log(code);
badUrls.push(urls[x]);
badCodes.push(code);
}
}
var total= [];
var temp;
temp = "URL"+"\t - \t"+"Status Code";
total.push(temp);
for(var i = 0 ; i < badUrls.length; i++){
temp = badUrls[i]+"\t - \t"+badCodes[i];
total.push(temp);
}
Logger.log("badUrls ength: "+badUrls.length);
if(badUrls.length !== 0){
Logger.log(badUrls);
var accountName = AdsApp.currentAccount().getName();
var subject = accountName + " - Keyword Broken Destination URLs";
var body = "The following are broken URLs in the account " + accountName +
". Also attached is a CSV file containing the URLs. \n\n" + badUrls.join("\n");
var options = {
attachments: [Utilities.newBlob(total.join("\n"), "text/csv", accountName +
" - Keyword Broken destination URLs.csv")]
};
Logger.log("keywords mail sent to: "+email_recipients);
MailApp.sendEmail (email_recipients,subject,body,options);
}
}
function brokenAdsLinkChecker() {
//HTTP Response codes which are treated as good/correct links
var validCodes = [200, 301, 302];
var adUrls = [];
var badAdUrls = [];
var badAdCodes = [];
var addCopiesIterator = AdsApp.ads()
.withCondition("Status = ENABLED")
.withCondition("AdGroupStatus = ENABLED")

.withCondition("CampaignStatus = ENABLED")
.get();
while(addCopiesIterator.hasNext()){
var adcopy = addCopiesIterator.next();
var destinationUrl = adcopy.urls().getFinalUrl(); //old API adcopy.getDestinationUrl()
if(destinationUrl !== null && destinationUrl !== ""){
var url = destinationUrl.split('?')[0];
if(adUrls.indexOf(url) === -1){
adUrls.push(url);
}
}
}
var urlFetchOptions = {muteHttpExceptions: true};

for(var x = 0; x < adUrls.length; x++){
try{
var response = UrlFetchApp.fetch(adUrls[x],urlFetchOptions);
var code = response.getResponseCode();
} catch(err){
Logger.log("The Url " + adUrls[x] + " could not be processed");
}
if(validCodes.indexOf(code) === -1){
Logger.log(code);
badAdUrls.push(adUrls[x]);
badAdCodes.push(code);
}
}
var total= [];
var temp;
temp = "URL"+"\t - \t"+"Status Code";
total.push(temp);
for(var i = 0 ; i < badAdUrls.length; i++){
temp = badAdUrls[i]+"\t - \t"+badAdCodes[i];
total.push(temp);
}
Logger.log("badAdUrls ength: "+badAdUrls.length);
if(badAdUrls.length !== 0){
Logger.log(badAdUrls);
var accountName = AdsApp.currentAccount().getName();
var subject = accountName + " - Adcopies Broken Destination URLs";

var body = "The following are broken URLs in the account " + accountName +
". Also attached is a CSV file containing the URLs. \n\n" + badAdUrls.join("\n");
var options = {
attachments: [Utilities.newBlob(total.join("\n"), "text/csv", accountName +
" - Adcopies Broken destination URLs.csv")]
};
Logger.log("Ad copies mail sent to: "+email_recipients);
MailApp.sendEmail (email_recipients,subject,body,options);
}
}