Visitor API is REST API that delivers website visitor’s IP address, location, currencies, languages, and device information for the website’s JavaScript to use the data to personalize the user experience.
Load Visitor API
Copy and paste the following code snippet to your web pages which requires the visitor data.
var VisitorAPI=function(t,e,a){var s=new XMLHttpRequest;s.onreadystatechange=function(){var t;s.readyState===XMLHttpRequest.DONE&&(200===(t=JSON.parse(s.responseText)).status?e(t.data):a(t.status,t.result))},s.open("GET","https://api.visitorapi.com/api/?pid="+t),s.send(null)};
Call Visitor API
You can then call Visitor API with the syntax below:
new VisitorAPI(projectID, successHandler, errorHandler);
The function accepts three parameters:
Parameter |
Description |
Example Value |
projectID |
This is the unique identifier of the project you created in the VisitorAPI interface. |
"om61tWZOjuBBPxTdDlpy" |
successHandler |
This is a function that process the visitor data when the API call is successfully. See response data format here. |
function(data){console.log(data)},
|
errorHandler |
This is a function that handles the error code and error message when the API call returns an error. The function can have two parameters: error code and error message. |
function(errorCode, errorMessage){console.log(errorCode, errorMessage)}
|
Below is a full example of loading VisitoAPI and calling the API to output the visitor data in the console when the API call is successful, or to output the error code and error message when the API call returns an error.
var VisitorAPI=function(t,e,a){var s=new XMLHttpRequest;s.onreadystatechange=function(){var t;s.readyState===XMLHttpRequest.DONE&&(200===(t=JSON.parse(s.responseText)).status?e(t.data):a(t.status,t.result))},s.open("GET","https://api.visitorapi.com/api/?pid="+t),s.send(null)};
VisitorAPI(
"om61tWZOjuBBPxTdDlpy",
function(data){console.log(data)},
function(errorCode, errorMessage){console.log(errorCode, errorMessage)}
);