<?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>The Not Simple Solutions &#187; Javascript</title>
	<atom:link href="http://thenotsimple.myfavoritepaintings.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://thenotsimple.myfavoritepaintings.com</link>
	<description>Just not so simple</description>
	<lastBuildDate>Mon, 05 Nov 2007 19:05:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Click Tracking or How Do I Track My Links?</title>
		<link>http://thenotsimple.myfavoritepaintings.com/2007/10/18/click-tracking-or-how-do-i-track-my-links/</link>
		<comments>http://thenotsimple.myfavoritepaintings.com/2007/10/18/click-tracking-or-how-do-i-track-my-links/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 07:30:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://thenotsimple.com/2007/10/18/click-tracking-or-how-do-i-track-my-links/</guid>
		<description><![CDATA[In this tutorial you will learn how to track different user actions on the site using AJAX. It&#8217;s no longer the problem to know what the user doing.
Here you can find a few very useful solutions to track link clicking or whatever-action-you-want-to-track.

Basic solutions
 This file was downloadedtimes.
The first solution &#8211; to redeclare global default functions:

document.onclick [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you will learn how to track different user actions on the site using AJAX. It&#8217;s no longer the problem to know what the user doing.</p>
<p>Here you can find a few very useful solutions to track link clicking or whatever-action-you-want-to-track.</p>
<p><span id="more-17"></span></p>
<h2>Basic solutions</h2>
<p><a CLASS="download" HREF="http://thenotsimple.myfavoritepaintings.com/wp-content/plugins/download-monitor/download.php?id=1"></a> This file was downloaded 738 times.</p>
<p>The first solution &#8211; to redeclare global default functions:</p>
<pre CLASS="javascript_code">
document.onclick = function(e) {
	e = e || event;
	var el = e.target || e.srcElement;
	if (el.tagName=='A')
	{
		alert('The link was clicked');
	}
}</pre>
<p>Pretty simple, right?<br />
Let&#8217;s improve it to make useful stats:</p>
<pre CLASS="javascript_code">
document.onclick = function(e) {
	e = e || event;
	var el = e.target || e.srcElement;
	//el - clicked element
	if (el.tagName=='A' &amp;&amp; el.target=='_blank')
	{
		//Here we can send ajax response to our stat
		//script to collect the outbound link anchors
		SendResponse('stat.php', el.innerHTML);
		/*Keep in mind - you can do whatever you want
		with that data -
		store time, referer, page name or other
		details
		You can even build click map of single page
		or entier web-site*/
	}
}</pre>
<p>This solution works great in IE6-7, FF 1.5-2.0 and Opera 9<br />
Another solution &#8211; dynamically add onclick events to target elements<br />
Here&#8217;s the code:</p>
<pre CLASS="javascript_code">
function addEventListener(instance, eventName, listener) {
	var listenerFn = listener;
	if (instance.addEventListener)
	{
		instance.addEventListener(eventName, listenerFn, false);
	} else if (instance.attachEvent)
	{
		listenerFn = function() {listener(window.event);}
		instance.attachEvent("on" + eventName, listenerFn);
	} else
	{
		throw new Error("Event registration not supported");
	}
	return
	{
		instance: instance,
		name: eventName,
		listener: listenerFn ;
	}
}
function AddOnclickToAllLinks() {
	for (i=0; i&lt;document.links.length; i++)
	{
		//Check for outbound parameter - target
		if (document.links[i].target == '_blank')
		addEventListener(document.links[i],
					"click",
		function() {alert("You clicked me!");});
	}
}</pre>
<p>Let&#8217;s add onload=&#8221;AddOnclickToAllLinks();&#8221; to &lt;body&gt; tag.<br />
You see &#8211; now you have onclicks on every outbound link.</p>
<p>Ok, let&#8217;s learn now how to implement this code and solutions in real tasks</p>
<h2>Sample script : Collecting exit-pages</h2>
<p>I&#8217;m sure you have hard-structured web-site, or may be not, anyway &#8211; some template system should be in use.</p>
<h3>So the first step &#8211; is to paste .js library code to your output HTML</h3>
<p>Don&#8217;t forget to save your Javascript code to independent file (in example js-code was saved to /js_library/ directory and named &#8220;tracker.js&#8221;)<br />
It doesn&#8217;t matter what solution will you choose (first with onclick redeclare or second with dynamic onclick declare)</p>
<p>it should look like this:</p>
<p>&#8230;&#8230;</p>
<p>&lt;meta name=&#8221;robots&#8221; content=&#8221;index, follow&#8221; /&gt;</p>
<p>&lt;script src=&#8221;/js_library/tracker.js&#8221; mce_src=&#8221;/js_library/tracker.js&#8221; type=&#8221;"&gt;&lt;/script&gt;</p>
<p>&lt;/head&gt;</p>
<p>&#8230;&#8230;</p>
<p>Here&#8217;s what I&#8217;ve saved to .js file:</p>
<pre CLASS="javascript_code">
document.onclick = function(e){
	e = e || event;
	var el = e.target || e.srcElement;
	if (el.tagName=='A' &amp;&amp; el.target=='_blank')
	{
		makeRequest('stat.php');
	}
}

function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest)
{
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType)
	{
		http_request.overrideMimeType('text/xml');
	}
} else if (window.ActiveXObject)
	{
		try
	        {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
		try
	        {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e)
		{}
	}
}
if (!http_request)
{
	return false;
}
	http_request.onreadystatechange = function()
	{};
	http_request.open('GET', url, true);
	http_request.send(null);
}</pre>
<p>I&#8217;ve used the simplest way to send GET HTTP request (function makeRequest) &#8211; you are able to use your favourite AJAX-framework.</p>
<h3>Collecting the stats on the server-side</h3>
<p>The simpliest way &#8211; not to use any frameworks or classes, keep in mind &#8211; the more visitors you have &#8211; the more stats will collect your script. Be aware of server&#8217;s overload.</p>
<h3>Mysql table</h3>
<p>Create &#8220;stats&#8221; table in PhpMyAdmin or whatever you use to manage your MySql server.</p>
<pre CLASS="javascript_code">
CREATE TABLE `stats` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`ip` VARCHAR( 20 ) NOT NULL ,
`page` VARCHAR( 512 ) NOT NULL ,
`time` INT( 11 ) NOT NULL
) ENGINE = MYISAM ;</pre>
<p>Now we have the table.<br />
The next code is just a sample of tiny stats collector written on PHP:</p>
<pre CLASS="javascript_code">
&lt;?php
	//Config
	$db_host = "localhost";
	$db_user = "root";
	$db_password = "";
	$db_basename = "tracker";
	$db_debug = false;

	//Tiny DB-lib
	include('lib_db.php');
	//Connecting to DB
	sql_establish();

	//Current time
	$tracker_time = time();
	//Tracked page
	$tracker_page = mysql_real_escape_string(
	substr(getenv("HTTP_REFERER"), 0, 512));
	//IP of the visitor
	$tracker_ip = getenv("REMOTE_ADDR");

	//Checking for double
	$double_check = sql_first_query(
		"SELECT id FROM stats WHERE ip = '$tracker_ip'
					AND page = '$tracker_page'
					AND time = $tracker_time");

	if (!is_array($double_check) || empty($double_check))
	{
		//Inserting new query
		sql_db_query("INSERT INTO stats
		VALUES('', '$tracker_ip', '$tracker_page', $tracker_time)");
	}
?&gt;</pre>
<p>That&#8217;s it!. You&#8217;ve just created a simple exit-page tracker.</p>
<h2>P.S. For lazy guys</h2>
<p>You can easily track your link by pasting onclick events on every link manually.<br />
<a CLASS="download" HREF="http://thenotsimple.myfavoritepaintings.com/wp-content/plugins/download-monitor/download.php?id=1"></a> This file was downloaded 738 times.</p>
]]></content:encoded>
			<wfw:commentRss>http://thenotsimple.myfavoritepaintings.com/2007/10/18/click-tracking-or-how-do-i-track-my-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
