Last active
March 5, 2016 11:35
-
-
Save vhsu/5b1013c5c5d3b1f3f22e to your computer and use it in GitHub Desktop.
Sitelinks Check ( MCC 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 Mcc level script for Single Account go here : https://goo.gl/QyuuG2 | |
// 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 | |
// for 50 first accounts with over than 100 impressions during the last 30 days in your MCC | |
// 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() { | |
MccApp.accounts().withCondition("Impressions > 100").forDateRange("LAST_30_DAYS").orderBy("Impressions DESC").withLimit(50).executeInParallel('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 unvalid any broken urls found'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment