Tracking Ip Addresses And Other User Data

Tracking information about your user such as how they got to your site (directly, a search engine, referral link, etc) is a critical piece in gathering the needed data to properly analyze your traffic. This data can help you make better decisions to help improve your overall user experience. Today I'll show you how to get the data. Later we will discuss how to build some simple reports.

Your IP Address

127.0.0.1

On the surface this may not seem very useful, but it does have value. First this enables you to remove data about you hitting your own page. This will give you a view of just you visitors. Another great use is matching duplicate IPs. Unique IPs and Repeating IPs are to very different sets of data.

Host Details (Your ISP)

localhost

This let's us know who our users ISP is. I really haven't much of a use for this, but I could see where some may find it useful.

Browser Details

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36

This is very valuable. Personally I build to W3C standards and get it working in those browsers (FireFox, Opera, Safari, etc). Then I'll make 2 copies of that style sheet ie6.css and ie7.css then get to work making those work. In this case I use the browser details to decide which stylesheet or front end hack I need to serve up.

To me, this also answers the question; Do we really need to support that browser? IE8 will be coming out soon, this has many developers rejoicing. "Finally we can kill IE6 support!" Not so fast. I see this killing IE7 support before IE6 support (legacy intranet and all that). This answers that question for you. If IE6 is still >20% of your traffic. I'd say you have to support it, <5% I'd probably consider killing support for it, but not before then.

Referrer (How you got here)

This tells us how the user got here. This is great for tracking ad campaigns, referral links, and your traffics origin in general. By analyizing the urls from search engines you can even determine the key words used to find your site.

<?php
  /* Let's start with an array to hold the user data */
  $you = array();
  /*PHP does most of the work for us. We just need to reference the predefined functions and variables.*/
  $you['ip'] = $_SERVER['REMOTE_ADDR'];
  $you['hostaddress'] = gethostbyaddr($you['ip']);
  $you['browser'] = $_SERVER['HTTP_USER_AGENT'];
  $you['referer'] = $_SERVER['HTTP_REFERER'];
?>
<!--Now that we have the user data let's loop the array and display the data -->
<?php foreach($you as $key => $value): ?>
  <strong><?php echo $key; ?></strong><br>
  <p><?php echo $value; ?></p>
<?php endforeach; ?>

The next article in this series will discuss building traffic reports


LinkedInGitHubTwitter