<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>{5} Setfive - Talking to the World &#187; tracking users</title>
	<atom:link href="http://shout.setfive.com/tag/tracking-users/feed/" rel="self" type="application/rss+xml" />
	<link>http://shout.setfive.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 18:48:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>FOSS Fridays &#8211; Tracking Your Users</title>
		<link>http://shout.setfive.com/2009/08/28/foss-fridays-tracking-your-users/</link>
		<comments>http://shout.setfive.com/2009/08/28/foss-fridays-tracking-your-users/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 18:35:31 +0000</pubDate>
		<dc:creator>Matt Daum</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[foss]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[tracking mouse movement]]></category>
		<category><![CDATA[tracking users]]></category>

		<guid isPermaLink="false">http://shout.setfive.com/?p=258</guid>
		<description><![CDATA[The other night I thought it&#8217;d be helpful to see how people browse your site.  I think you can probably learn a lot about how a user moves over your site.  You can tell a lot about your user&#8217;s experience by watching their mouse.  You can see where they look on the page for specific [...]]]></description>
			<content:encoded><![CDATA[<p>The other night I thought it&#8217;d be helpful to see how people browse your site.  I think you can probably learn a lot about how a user moves over your site.  You can tell a lot about your user&#8217;s experience by watching their mouse.  You can see where they look on the page for specific information.  I&#8217;ve created a <a href="http://code.setfive.com/trackingdemo/" target="_blank">demo</a> of the tracking.</p>
<p>I know that there are some products out there that already do user session tracking and replays.  Also there are click heat maps which are interesting when you are looking on your site to see what links the user clicks the most.  I decided to just rebuild the session replay just out of curiousity on how difficult it&#8217;d be to do.  It was fairly simple and took me only 15-20 minutes. There are a number of improvements you could make to the script such as the window.unload handling is not 100% depending on your browser.  You could also do much more parsing on the client side of the information by using JSON.  If you wanted to store more than one page of tracking data you could quickly modify the script to pass the name page which it was tracking and to store the data separately for each page.</p>
<p>To use the script all you need to do is add a little Javascript on the bottom of the page you want to track a user.   The script tracks a users mouse movement as soon as they open the site.  It keeps track of time so that during the replay you can get the proper mouse movement at the right times to replay the users session.  While the user moves their mouse it continues to store all the data client side.  Once the window is closed it sends all the information to the server.  The server simply parses the data string.  For session replay it is done via setTimeout and it moved an image(of a cursor) at different intervals to simulate the users session.</p>
<p>While the script is not very pretty it was written very quickly and just as a proof of concept.  It goes to show you can easily track your user&#8217;s session without having to purchase expensive products, and that it can be done fairly simply.</p>
<p>The code is below with descriptions of what each snippet does. The script uses <a href="http://jquery.com/">jQuery</a>.  To deploy this on numerous pages all you really would need to add would be a script tag that pulls in the tracking javascript.</p>
<p>Tracking the users movements and sending the server the information javascript:</p>
<pre name="code" class="javascript">
var points='';
var timeSeconds=0;
$(document).ready(function(){
  $('body').append('<span id="trackingid"></span>');
  timer();
   $().mousemove(function (e){
    points=points+e.pageX+","+e.pageY+","+timeSeconds+"|";
  });
  $(window).unload(function(){
    sendData();
  });
});
function timer()
{
  timeSeconds=parseInt(timeSeconds)+1;
  setTimeout("timer()",10);
}
function sendData(){
    $.post('index.php','data='+points);
}
</pre>
<p>To parse the information on the server:</p>
<pre  name="code"  class="php">if($_SERVER['REQUEST_METHOD']=='POST')
{
  $fp=fopen($_SERVER['REMOTE_ADDR'].'tracking.dat','w+');
   fwrite($fp,$_POST['data']);
  fclose($fp);
  exit(1);
}</pre>
<p>To replay the session first get the data:</p>
<pre  name="code"  class="php">if(file_exists($_SERVER['REMOTE_ADDR'].'tracking.dat'))
{
 $data=explode("|",file_get_contents($_SERVER['REMOTE_ADDR'].'tracking.dat'));
}</pre>
<p>The moving of the cursor image javascript:</p>
<pre  name="code" class="javascript">function moveMouse(x,y){
 $('#cursor').attr('style','position:absolute;left:'+x+"px;top:"+y+"px;");
}</pre>
<p>Create different calls for each time the mouse was moved and have them execute at the times the user moved the mouse:</p>
<pre  name="code"  class="javascript">foreach($data as $d)
{
  $parts=explode(',',$d);
  if(count($parts)==3)
  echo 'setTimeout("moveMouse('.$parts[0].','.$parts[1].')",'.($parts[2]*10).");\n";
}</pre>
<p>And you are all set.  As I said the script is only for proof of concept and not too pretty.  Let me know if you have any questions. </p>
]]></content:encoded>
			<wfw:commentRss>http://shout.setfive.com/2009/08/28/foss-fridays-tracking-your-users/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
