Facebook & Microsoft Ads Link Checker Script

Google has its own link checker script to monitor broken URLs for Google 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.

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 used 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.

 

5 Comments

  1. 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

  2. Hi Ahmed,
    This is exactly what I was looking for (although I’m ashamed to admit I know nothing at all about scripts). I’m now using the Adwords script, and want to be able to check Bing & Yahoo URLs, too.
    Alas, when I tried it out on a test list of about 200 URLs, it timed out. When I shorten the list to a dozen or so rows, it runs, but gives me errors on URLs that have UTM codes. In your experience, do these need to be stripped out first? And do you have any thoughts on the time-out issue?
    Thanks very much!

  3. Hi, I am getting the following error “Range not found (line 11, file “Code”)Dismiss”

    can you please help me