Facebook & Bing Ads Link Checker Script

Google has its own link checker script to monitor broken URLs for AdWords Ads. However, when it comes to Facebook Ads and other channels, it could be a bit challenging to find out broken links, essentially, that websites evolve, new pages get added, old pages are taken down, and links get broken and fixed. Keeping your campaigns in sync with the website is an ongoing battle for many advertisers, especially if you are working on large accounts.

To solve this issue for our clients, we’ve written a script that iterates through all of your ads URLs and notifies you through an email if any of your ads has any broken URLs. The script returns the status code for the fetched URLs, it considers only 200 a valid response code and the rest of the response codes get highlighted in red.

404 shouldn’t be the only invalid response code that you are looking at. 301 and 302 redirects usually lead to drop the UTMs that you use to track FB campaigns performance on Google Analytics. If the links UTMs are getting dropped, Facebook traffic will be attributed to the Direct source on GA. This is critical if you want to make sure you are basing your decisions on accurate data.

You can use the script to scan any kind of URLs; Facebook, Bing, Twitter, LinkedIn or Yahoo ads. So, let’s get down to business and discover how to use the script:

How to use the script?

1- Fetch The Ads URLs into a Google Sheet

You can do this manually, but this is not recommended. What’s highly recommend is you use an add-on that can connect to the ads platform (Facebook, Twitter, .. etc) and you can schedule it on a regular basis to fetch all of the URLs into a Google Sheet.
You can use Supermetrics, it’s a really powerful add-on that every marketer must have, it saves so much time, especially when it comes to reporting. You can schedule a URL performance report that refreshes every day at a certain time. In parallel, we will be scheduling the script to scan the refreshed URLs and sends an email if any errors found.

2- Connect Google App Scrips to your Google Sheet

Click Tools then “Script Editor”:-

Now, the script editor is open:-

Delete any code in the script editor and paste the code below and don’t forget to name and save your project.

/***************************************************
* Facebook Link Checker Script
* Version 1.0
* Created By: Ahmed Ali
*https://optimizationup.com/
****************************************************/

function myFunction() {

var email = "Add your Email Here";
var urlColumn = "A";// Change if you want to change the URL column
var resultsColumn = "E"; // Change if you want to change the result column

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetURL = ss.getUrl();
var lastRow = sheet.getLastRow();
var data = sheet.getRange( urlColumn + 1 + ":" + urlColumn + lastRow).getValues();

sheet.getRange(resultsColumn + 1 + ":" + resultsColumn + lastRow).clearContent().clearFormat();
sheet.getRange(resultsColumn + 1 ).setValue("Status Code");

var results = [];
var infected = [];

for (i in data) {

 var [urls] = data[i];

if (i == 0) {
 continue;
 }

if (urls == "") {
 break;
}

 else {

 results.push(statuscode(urls));
 }
}


for ( var i = 0; i < results.length; i++) {

var r = i + 2;

sheet.getRange(resultsColumn + r).setValue(results[i]);

 if (results[i] != 200) {
 
 sheet.getRange(resultsColumn + r).setBackground("red");
 
 infected.push(results[i]);

 }
}

var numOfErrors = infected.length; 

 if(infected.length > 0){

 MailApp.sendEmail( email , "Facebook URLs Checker Script! ", "Your Ads have " + numOfErrors + " URLs with errors. Please visit the spreadsheet below for more details: " + sheetURL);

 } 
}

function statuscode(url) {
 try {
 var response = UrlFetchApp.fetch(url, {
 muteHttpExceptions: true,
 followRedirects: false,

 });
 return response.getResponseCode();
 } catch (error) {

 return "Error";
 }
}

3- Configure the Script

You need to configure the script based on your needs to determine which column has the URLs and which column to write the results to.

facebook ads link checker script

In the screenshot above, the URLs that we want to check are present in column A, that’s why we set the urlColumn equal to A

var urlColumn = "A";

We also want to return the results to column  “E”, so we assigned the value E to the resultsColumn variable

var resultsColumn = "E";

Finally, you can add your email:-

var email = "Add your Email Here";

4- Schedule the Script

Schedule the script hourly or daily to iterate through the ads URLs. Please make sure that the script only runs after the add-on schedule that you will be using to fetch new URLs every time. 

Each time the script runs, it automatically sends an email with the number of errors and the spreadsheet URL as see below:-

Feel free to leave any comments below or contact me, if you have any questions.

 

mm

Ahmed launched his first digital campaign in 2013 through participating in Google Online Marketing Challenge. He competed with over 12,000 students from 80 countries to win the first place in Africa & MENA region and one of the best five campaigns in the world. Today, he works as an Associate Digital Director at Keyade Middle East.

2 Comments

  1. Zoya Hashmi

    This is amazing! Very straight to the point and easy to follow (just as the rest of your other blogs). Finally we can leverage using the Link Checker for platforms other than Google. Thank you Mr.Ahmed 🙂 Bravo

Leave a Reply

Your email address will not be published. Required fields are marked *