SEO Tools with Google App Script

Simple SEO Tools on Google Sheets with Google App Script

Google Sheets have many restrictions, quota limits however they can still be helpful in daily SEO tasks especially if you have a small website or else if you want to check SEO data quickly on small number of URLs.

Please find below 5 javascript functions written with Google App Script which can be used as Simple SEO Tools on Google Sheets.

The source code is also available at https://github.com/aysunakarsu/Simple/blob/master/SEOTools.js

function Response(uri)
{
  try {
  var options = {
  followRedirects : false
 };
  var response = UrlFetchApp.fetch(uri, options);
  return response.getResponseCode() ;  
  }  
   catch (error) {        
     return "Error";
    }
}
function Title(uri) {
  var html = getContents(uri);
  var title = html.match('<title>([^\<]+)</title>')[1];
  return title;
}
function H1(uri) {
  var html = getContents(uri);
  var h1 = html.match('<h1[^\>]*>([^\<]+)</h1>')[1];
  return h1;
}
function ILinks(uri) {
  var base_uri = uri.split('://')[0] + "://" + uri.split('://')[1].split('/')[0];
  var inner_links = [];
  inner_links=getInnerLinks(uri,base_uri);
  return inner_links.length;
}

function getInnerLinks(get_uri,getbase_uri) {
  try  {
    var html = getContents(get_uri);
    if (html.indexOf('</head>') !== -1 ) {
        html = html.split('</head>')[1];    
        if (html.indexOf('</body>') !== -1 ) {
           html = html.split('</body>')[0] + '</body>';
           var inner_links_arr= [];
           var linkRegExp = /href="([^\"#]+)"/gi; // regex href
           var extractLinks = linkRegExp.exec(html);
           var item=getbase_uri;
           var pagesVisited = {};
           while (extractLinks != null) {
              if ((extractLinks[0].indexOf("href=")==0))
                  if (extractLinks[1].indexOf(getbase_uri)==0)  item= extractLinks[1];                 
                  else if (extractLinks[1].indexOf("http")!==0) item= getbase_uri+extractLinks[1];
                  if (!(item in  pagesVisited)) {
                    pagesVisited[item]=true;
                    inner_links_arr.push(item);
                    }
              extractLinks = linkRegExp.exec(html); }
           return inner_links_arr;  
           }
       }
  }
  catch (e) {
   return "Error"
  }
}
function ELinks(uri) {
  var base_uri = uri.split('://')[0] + "://" + uri.split('://')[1].split('/')[0];
  var external_links = [];
  external_links=getExternalLinks(uri,base_uri);
  if (typeof external_links !== 'undefined' && external_links.length > 0 )
      return external_links.length;
 return 0;       
}
function getExternalLinks(get_uri,getbase_uri) {
  try  {
    var html = getContents(get_uri);
    if (html.indexOf('</head>') !== -1 ) {
        html = html.split('</head>')[1];    
        if (html.indexOf('</body>') !== -1 ) {
           html = html.split('</body>')[0] + '</body>';
           var external_links_arr= [];
           var linkRegExp = /href="(http[^"#]+)"/gi; // regex href
           var extractLinks = linkRegExp.exec(html);
           var pagesVisited = {};
           while (extractLinks != null) {
              if (extractLinks[0].indexOf("href=")==0)  {
                  if (extractLinks[1].indexOf(getbase_uri) != 0 &&  (!(extractLinks[1] in  pagesVisited)) ){
                    pagesVisited[extractLinks[1]]=true;
                    external_links_arr.push(extractLinks[1]);}
                    }
              extractLinks = linkRegExp.exec(html); }
           return external_links_arr;  
           }
       }
  }
  catch (e) {
    return e;
  }
}
function getContents(uri) {
  var result = UrlFetchApp.fetch(uri);
  var contents = result.getContentText();
  return contents;
}

How to use SEO Tools functions?

  1. Go to Google Drive and create a new spreadsheet, name it , e. g. simpleseotools
  2. Add the URLs you want to check in the first column, one URL per row.
  3. From the Tools menu choose Script Editor.
  4. Paste the script code from above into the file and save it.
  5. Go to the spreadsheet, and call the function you select on each row in the next columns, for example

   For the URL in the first column first row; call =Response($A1) or else =Title($A1) or else =H1($A1) or else  =ILinks($A1) or else =Elinks($A1) on the same row in the next column. Repeat the same process on the following rows and columns. 

For more information on Quota Limits please see the table on the Google Apps Script Dashboard.  

  

 

Have comments, questions or feedback about this article? Please do share them with us here.

If you like this article

Follow Me on Twitter

Follow Searchdatalogy on Twitter

Comments

About Us

Our objective is bringing all our experience and expertise together to deliver solid technology solutions that can take your search traffic acquisition to the next level. Our main goal is to assist you in building and maintaining your search marketing analytics platforms. Our will is to leverage your marketing and IT teams search knowledge while bridging the gap between two.

Legal Terms Privacy

Recent Posts

BrightonSEO 5 days, 10 hours ago
1 Million #SEO Tweets 7 months, 2 weeks ago
SEO, Six Blind Men & An Elephant 8 months, 2 weeks ago
Best SEO Conferences In 2017 8 months, 4 weeks ago
SEO Hero 2017 9 months, 2 weeks ago
3 Ways For Free HTTPS 11 months ago
Crawl Dictionary 11 months, 3 weeks ago
Bing Strikes 1 year ago
HTTPS On Top Sites 1 year ago
SEO Web Server Log Files 1 year, 1 month ago
SEO, Web Server Logs And Science 1 year, 1 month ago
HTTP2 On Top Sites 1 year, 1 month ago

Recent Tweets