How to Include the visitor.js Script on a Website

Embedding the visitor.js Script

To embed the visitor.js script into your web page, add the following line to the <head> tag of the HTML code (for instance, right before </head>):

<script type="text/javascript" src="http://www.visitorjs.com/visitor.js?key=YOUR_KEY"></script>

Please replace "YOUR_KEY" with your personal API key. If you don't have an API key yet, sign up for a visitor.js account first.


Using the Script

After including a link to the script, you can start writing JavaScript code and using the visitor object. Here's a very basic example that outputs the visitor's IP address (available via visitor.ip.address):

<script type="text/javascript">
  // Make sure the visitor object is available
  if (visitor) {
    // Show a message box with the visitor's IP address
    alert("Your IP address: " + visitor.ip.address);
  }
</script>

Open Example Page


It's recommended to always check whether the visitor object is available (as demonstrated in the above example). This ensures that your code doesn't throw an error when the visitor.js script isn't available or when your account has been disabled or suspended.


A More Advanced Example

We recommend using the visitor.js script together with jQuery, a widely used library that makes working with JavaScript much more pleasant. The easiest way to include jQuery into a page is to call the script from Google's servers:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

The following script example uses visitor.js and jQuery to localize a welcome message for Spanish-, French-, German- and Italian-speaking visitors:

<script type="text/javascript">
  // Run the code once the page is ready for manipulation
  $(document).ready(function() {
    // Make sure the visitor object is available
    if (visitor) {
      var welcomeMsg = null;

      // Get a welcome message depending on the browser language
      switch (visitor.locale.languageCode) {
        case 'es':  // Spanish
          welcomeMsg = "Bienvenido a nuestro sitio web.";
          break;
        case 'fr':  // French
          welcomeMsg = "Bienvenue sur notre site.";
          break;
        case 'de':  // German
          welcomeMsg = "Willkommen auf unseren Webseiten.";
          break;
        case 'it':  // Italian
          welcomeMsg = "Benvenuti sul nostro sito.";
          break;
      }

      // Output the welcome message to the #welcome-message div
      if (welcomeMsg) {
        $('#welcome-message').text(welcomeMsg);
      }
    }
  });
</script>

In addition to the script code above that should be placed right before the </head> tag, you also need to place the following code somewhere in your page's body:

<div id="welcome-message">Welcome to our website.</div>

Open Example Page