Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AdWords script for checking ads with broken URL.
/******************************************
* Advanced Broken Url Checker
*
* Version 1.1
*
* Created by: Dmytro Tonkikh
*
* m.me/dmytro.tonkikh
* t.me/dtonkikh
* t.me/adwordsscripts
******************************************/
function main() {
var urls = {};
var report = AdsApp.report(
"SELECT CreativeFinalUrls, Id, AdGroupId " +
"FROM AD_PERFORMANCE_REPORT " +
"WHERE AdNetworkType1 = SEARCH " +
"AND AdType = EXPANDED_TEXT_AD " +
"AND CampaignStatus = ENABLED " +
"AND AdGroupStatus IN [ENABLED, PAUSED] " +
"AND Status = ENABLED"
);
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
//Logger.log(row.formatForUpload());
var url = JSON.parse(row["CreativeFinalUrls"])[0];
var id = row.Id;
var AdGroupId = row.AdGroupId;
// Logger.log(url)
if (urls[url] == undefined) {
urls[url] = []
};
urls[url].push([AdGroupId,id]);
}
var urlData = Object.keys(urls);
var badIds = [];
var goodIds = [];
urlData.forEach(function (item) {if (checkUrl(item)) {urls[item].forEach(function(el){badIds.push(el)})} else {urls[item].forEach(function(el){goodIds.push(el)})}});
var changedArray = AdGroupsToChange(goodIds, badIds);
changedArray.forEach(function (item) {if (item[1] == "paused") {item[0].pause()} else {item[0].enable()}});
}
function checkUrl (url) {
var params = {muteHttpExceptions:true, followRedirects: false};
var resp = UrlFetchApp.fetch(url, params).getResponseCode();
if (resp !== 200) {
return true;
} else {
return false;
}
}
function AdGroupsToChange (goodIds, badIds) {
var changedArray = [];
var pausedIter = AdsApp.ads().withIds(badIds).withCondition("Status = ENABLED").get();
while (pausedIter.hasNext()) {
var ad = pausedIter.next();
changedArray.push([ad,"paused"]);
}
var enableIter = AdsApp.ads().withIds(goodIds).withCondition("Status = PAUSED").get();
while (enableIter.hasNext()) {
var ad = enableIter.next();
changedArray.push([ad,"enable"]);
}
return changedArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.