Last active
March 5, 2016 11:34
-
-
Save vhsu/1269f41d7d2082f72deb to your computer and use it in GitHub Desktop.
Check if sitelinks are broken : Adwords account level script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title : SiteLink 404 Checker Account Level for Mcc level : https://goo.gl/P0SKk3 | |
// Author : Vincent Hsu @suisseo | |
// Website : http://www.suisseo.ch/en | |
// Checks sitelinks in campaigns and adgroups with at least 1 impression during the last 30 days | |
// Broken links are simply logged in the adwords console. An e-mail is sent to you when the script is executed. | |
//You should add your e-mail here. | |
var emailAlert= "[email protected]"; | |
function main() { | |
checkSiteLinks(); | |
sendSimpleTextEmail(); | |
} | |
function checkSiteLinks() { | |
var urlArray = new Array(); | |
var badUrlArray = new Array(); | |
var account = AdWordsApp.currentAccount(); | |
var sitelinkSelector = AdWordsApp.extensions().sitelinks(); | |
var sitelinkIterator = sitelinkSelector.get(); | |
while (sitelinkIterator.hasNext()) { | |
var sitelink = sitelinkIterator.next(); | |
checkAndAdd(sitelink.urls().getFinalUrl(), urlArray); | |
checkAndAdd(sitelink.urls().getMobileFinalUrl(), urlArray); | |
} | |
checkUrlArray(urlArray, badUrlArray) | |
printArr(badUrlArray, account.getName()); | |
} | |
// check if url is already in the array and add it if not | |
function checkAndAdd(url, arr) { | |
if (url !== null) { | |
var found = arr.some(function(el) { | |
return el === url; | |
}); | |
if (!found) { | |
arr.push(url); | |
} | |
} | |
} | |
// Log a 1 dimensional array | |
function printArr(arr, accountName) { | |
Logger.log(accountName + ' : Checking SiteLinks...'); | |
for (i = 0; i < arr.length; i++) { | |
Logger.log(arr[i]); | |
} | |
if (arr.length == 0) { | |
Logger.log('Everything seems ok for this Account'); | |
} | |
} | |
// Check if url is valid | |
//(if you think 301 and 302 should be reported just change the condition) | |
//arr is the array to test , badarr is the array used to store invalid urls | |
function checkUrlArray(arr, badarr) { | |
for (i = 0; i < arr.length; i++) { | |
var response = UrlFetchApp.fetch(arr[i], { | |
muteHttpExceptions: true | |
}); | |
if (response.getResponseCode() == 302 || response.getResponseCode() == 301 || response.getResponseCode() == 200) { | |
//ok so do nothing | |
} else { | |
checkAndAdd(arr[i], badarr) | |
} | |
} | |
} | |
//send e-mail | |
function sendSimpleTextEmail() { | |
MailApp.sendEmail(emailAlert, | |
'Site links were checked', | |
'Please log in adwords to check any broken urls found'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment