Login

Visitor Parser Js


Lightweight javascript-based to analyze visitors with user-agent & ip geo location parser.

Usage

<script src="visitor-parser.js"></script>
<script>
  var visitor = new visitorParser();   
  var info = visitor.getInfo();  // get ua and geo
  console.log(info);   
</script>

Default of result:

{
   "geo": {
     "ip": empty,
     "provider": empty,
     "city": empty,
     "lat": empty,
     "lon": empty,
     "country": empty,
     "country_code": empty,
     "region": empty,
     "region_code": empty,
     "timezone": empty,
     "zip": empty
   },
   "ua": {
     "platform": {
       "name": empty,
       "version": undefined
     },
     "browser": {
       "name": empty,
       "version": undefined
     },
     "resolution": {
       "width": integer,
       "height": integer
     },
     "engine": {
       "name": empty,
       "version": undefined
     },
     "processor": {
       "name": empty
     },
     "mobile": {
       "name": empty,
       "version": undefined
     },
     "robot": {
       "name": empty
     },
     "date": string,
     "referring": {
       "url": empty,
       "host": empty
     }
   }
}

Methods

  • getInfo()
    Result geo:{object} and ua:{object} like the example above.
  • getGeo()
    Result like:
    {
      "ip": "127.0.0.1",
      "provider": "Three Indonesia",
      "city": "Bandung",
      "lat": -6.9039,
      "lon": 107.6186,
      "country": "Indonesia",
      "country_code": "ID",
      "region": "West Java",
      "region_code": "JB",
      "timezone": "Asia/Jakarta",
      "zip": ""
    }
  • getUa()
    Result like:
    {
      "platform": {
         "name": "Android",
         "version": "4.0.3"
      },
      "browser": {
         "name": "Firefox",
         "version": "50.0"
      },
      "resolution": {
         "width": 1366,
         "height": 662
      },
      "engine": {
         "name": "Gecko",
         "version": "20100101"
      },
      "processor": {
         "name": "x64"
      },
      "mobile": {
         "name": "Samsung Galaxy Nexus"
      },
      "robot": {
         "name": ""
      },
      "date": "2016-12-05T01:10:45.825Z",
      "referring": {
         "url": "https://www.google.com/search?q=ibacor&ie=utf-8&oe=utf-8&client=firefox-b",
         "host": "www.google.com"
      }
    }
  • getUas()
    Result user-agent string like Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0.

Options

var visitor = new visitorParser({
   setUa: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36", // set user-agent. string
   getOpt: true, // get all options result of parser. true or false
   geoAPI: 'http://freegeoip.net/json/' // set ip geo location api. url
});

var result = visitor.getUa().browser;
console.log(result);

If getOpt: false return object:

{
   "name": "Firefox",
   "version": "50.0"
}

If getOpt: true return array:

[
   {
  "name": "Firefox",
  "version": "50.0"
   },
   {
  "name": "Mozilla",
  "version": "5.0"
   }
]

Demo Fork me on GitHub