<?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>Al Stevens – Art director, interactive and user experience designer. &#187; javascript</title>
	<atom:link href="http://www.alstevens.co.uk/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alstevens.co.uk</link>
	<description>I work and play at TUI Ski where I spend my days endeavoring to craft delightful interactive experiences. At home I like country pubs, listening to the eclectic delights of French radio FIP and am daddy to a clutch of cheeky little monsters. Any opinions expressed here are mine and do not represent the opinions of any company I work for.</description>
	<lastBuildDate>Tue, 23 Feb 2010 18:05:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tracking javascript events with google analytics</title>
		<link>http://www.alstevens.co.uk/tracking-javascript-events-google-analytics/</link>
		<comments>http://www.alstevens.co.uk/tracking-javascript-events-google-analytics/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 19:35:45 +0000</pubDate>
		<dc:creator>Al Stevens</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[DOM scripting]]></category>
		<category><![CDATA[google analytics]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[tracking]]></category>

		<guid isPermaLink="false">http://www.alstevens.co.uk/?p=165</guid>
		<description><![CDATA[One of the key things I want tracked on my website is those who click on the rss icon and add me to their feed reader. When someone actually clicks on subscribe &#8211; hey that means that they like what they have seen and want to read more. I am humbled. I am overjoyed. I [...]]]></description>
			<content:encoded><![CDATA[<h3>One of the key things I want tracked on my website is those who click on the rss icon and add me to their feed reader. When someone actually clicks on subscribe &#8211; hey that means that they like what they have seen and want to read more. I am humbled. I am overjoyed. I have a conversion!<span id="more-165"></span></h3>
<p>In <a href="http://www.google.com/analytics/">google analytics</a> you can set up conversion goals, and therefore can quickly see on entry whether you have achieved any of your goals in the last set time period. Normally a goal will be a page, like a thank-you page, but in my case I want to track a link to an xml/rss file, which cannot be tracked by javascript.</p>
<p>Looking around online there are some examples of tracking rss &#8211; although many of them refer to google analytics&#8217; old script which calls the function urchin. I need one which calls the newer pageTracker._trackPageview arguement. Most recommend the use of an inline javascript which fires the page tracker onclick. But I have several rss links on my website and I don&#8217;t want to relpicate this javascript behaviour across all of that code. Otherwise when it comes to changing the code I will need to manually go and update every link with the onclick. Also I want to seperate the behaviour of this javascript from the content of the website.</p>
<h3>DOMScripting to the rescue</h3>
<p>The solution to this problem is to use a <a href="http://en.wikipedia.org/wiki/DOM_Events">DOM event</a> to call the function. By adding a class to any links which are used to &#8216;Subscribe&#8217; we can write a DOMScript which creates the javascript event on page load.</p>
<h3>Tracking javascript events</h3>
<p>So this is what you need to do:</p>
<ol>
<li>add a class of &#8220;rss&#8221; to each instance of the RSS link you want to track</li>
<li>add the following code to your javascript library</li>
</ol>
<pre>/* track links with the class of rss */
function trackRss(){
if (!document.getElementsByTagName) return false; //added to test the browsers DOM compatibility
if (!document.getElementsByTagName('a')) return false; //added to test for any divs
var divs = document.getElementsByTagName('a');
 for(var i=0,j=divs.length;i&lt;j;i++){
  if (divs[i].className.match('rss')){ // .match to deal with multiple class names
   divs[i].onclick = function (){
		var pageTracker = _gat._getTracker("UA-XXXXXX-X"); //Enter your Google Analytics account ID here
		pageTracker._initData();
		pageTracker._trackPageview("/rss/subscribe/");
   }
  }
 }
}</pre>
<p>Note, you will have to add in your urchin tracker ID within the main function at the end of this code. This whole function will now need to be called on page load. I would recommend using an addLoadEvent script like the one below.</p>
<pre><code class="javascript">function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(trackRss);
</code></pre>
<p>The result of all of this is that a click on any link with the class &#8220;rss&#8221; will fire google analytics, and in this example it will read the page as &#8220;rss/subscribe&#8221; (from the trackPageview)</p>
<h3>Extensibility</h3>
<p>I think there may be a way of making this more scaleable by getting one function to handle multiple tracking links. Could it use a rel attribute? So that you assign a class to identify it is a tracked link, and a rel to tell it what to call the link.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alstevens.co.uk/tracking-javascript-events-google-analytics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Larger, more useable click areas using clickBox</title>
		<link>http://www.alstevens.co.uk/clickable-divs/</link>
		<comments>http://www.alstevens.co.uk/clickable-divs/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 11:49:41 +0000</pubDate>
		<dc:creator>Al Stevens</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[DOM scripting]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.alstevens.co.uk/wordpress/?p=99</guid>
		<description><![CDATA[Enlarging the click area of an element has obvious benefits for a person using your website, making it easier to get from one page to another without having to target small links with their cursor. Using this simple DOMscript we can easily make our click areas much bigger and easier for people to click.
From both [...]]]></description>
			<content:encoded><![CDATA[<h3>Enlarging the click area of an element has obvious benefits for a person using your website, making it easier to get from one page to another without having to target small links with their cursor. Using this simple DOMscript we can easily make our click areas much bigger and easier for people to click.</h3>
<p>From both a marketing and usability point of view it is always important to make sure that people using your website can get from A to B as easily as possible. <span id="more-99"></span></p>
<p>But how can we achieve this clickable-ness without reverting back to making our banners out of a single, inaccessible image? And how do we do it in a way that will help, rather than hinder our natural search listings?</p>
<p>Well we could:</p>
<ol>
<li>Make an image with all our text and graphics. But this is at the cost of scalability and accessibility, and maybe more importantly will not be recognised as well by google.</li>
<li>We could create our nice sematic html, get it to render correctly. Apply a link to the key phrase using an h tag (thumbs up for google), and apply an onClick event to the div.</li>
</ol>
<p>That&#8217;s genius. Google can see our h tag link and rank it appropriately. The html is well formed and accessible AND we can make the whole area clickable. and it could look something like this.</p>
<pre>&lt;div class="myBannerAd"&gt;
&lt;h3&gt;&lt;a href="http://www.mygoodproduct.com/trackingurl/1789425311" onclick="document.location.href='http://www.mygoodproduct.com/trackingurl/1789425311'"&gt;Buy it, its good&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;It won't let you down - ever&lt;/p&gt;
&lt;p class="button"&gt;Click here to buy it now&lt;/p&gt;
&lt;/div&gt;</pre>
<p>But wait a minute &#8211; there&#8217;s a fly in the ointment. Not only do we have to apply an inline javascript &#8211; breaking the golden rule of seperating content and behaviour, but we also have to duplicate the url. Not so bad in itself &#8211; but opening us up to errors if we have lots of these types of links. Furthermore, of we do have lots of these types of links, we are replicating the document.location.href behaviour over and over again</p>
<h3>Enter clickBox();</h3>
<p>clickBox is a handy little domscript which applies this behaviour on the <a href="http://en.wikipedia.org/wiki/DOM_Events">DOM event</a> of page load. By applying a class name of clickBox to a div you want to be clickable it will turn your core html from this:</p>
<pre>&lt;div class="myBannerAd clickBox"&gt;
&lt;h3&gt;&lt;a href="http://www.mygoodproduct.com/trackingurl/1789425311"&gt;Buy it, its good&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;It won't let you down - ever&lt;/p&gt;
&lt;p class="button"&gt;Click here to buy it now&lt;/p&gt;
&lt;/div&gt;</pre>
<p>into this</p>
<pre>&lt;div class="myBannerAd clickBox"&gt;
&lt;h3&gt;&lt;a href="http://www.mygoodproduct.com/trackingurl/1789425311" onclick="document.location.href='http://www.mygoodproduct.com/trackingurl/1789425311'"&gt;Buy it, its good&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;It won't let you down - ever&lt;/p&gt;
&lt;p class="button"&gt;Click here to buy it now&lt;/p&gt;
&lt;/div&gt;</pre>
<h3>The script</h3>
<p>I have to thank <a href="http://www.leveret.co.uk/blog/">Jay Bishop</a> for this script, who developed this script to implement clickBox on the <a href="http://www.firstchoice.co.uk">First Choice Holidays</a> website.</p>
<p>It works by traversing the DOM and locating each div with a class of clickBox. It then looks at it&#8217;s children for any a tags and takes the first a tag and applies the onclick event to it. Whats even better is that although the whole box will be clickable, secondary or tertiary links will still maintain their correct link, so there are no problems with over-riding other links.</p>
<pre>/* makes all of div clickable, linking to the first href found within the row. You simply need to add the class 'clickBox' */

function clickableDivs(){

if (!document.getElementsByTagName) return false; //added to test the browsers DOM compatibility

if (!document.getElementsByTagName('div')) return false; //added to test for any divs
var divs = document.getElementsByTagName('div');
 for(var i=0,j=divs.length;i&lt;j;i++){
  if (divs[i].className.match('clickBox')){ // .match to deal with multiple class names
  if (!divs[i].getElementsByTagName('a')[0]) continue; // to ensure no errors if href is not present
  divs[i].style.cursor = "pointer"; // change the cursor to a pointer if onclick is applied
   divs[i].onclick = function (){
     window.location = this.getElementsByTagName('a')[0].href; //regular link
   }
  }
 }
}</pre>
<h3>Implementing the script</h3>
<p>In order to call the funtion on page load we use an addLoadEvent script adapted from <a href="http://simonwillison.net/2004/May/26/addLoadEvent/">Simon Willison&#8217;s original.</a> In our case we created a library of DOM scripts which includes both the addLoadEvent script and the clickBox script.</p>
<p>Insert the <a href="http://www.alstevens.co.uk/js/clickbox.js">DOM Scripts Library</a> into the head of your document.</p>
<pre>&lt;script type="text/javascript" src="/js/clickbox.js"&gt;&lt;/script&gt;</pre>
<p>Add the class to any element which you want to make clickable</p>
<pre>&lt;div class="myBannerAd <strong>clickBox</strong>"&gt;
&lt;h3&gt;&lt;a href="http://www.mygoodproduct.com/trackingurl/1789425311"&gt;Buy it, its good&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;It won't let you down - ever&lt;/p&gt;
&lt;p class="button"&gt;Click here to buy it now&lt;/p&gt;
&lt;/div&gt;</pre>
<p>and were done.</p>
<h3>Extensibility</h3>
<p>Adding any behaviours to these elements in the future (ie add an icon in there, put in a background colour etc) is also a synch. All we need to do is add the required functionality into the script and it will automatically be applied to all of the clickBox elements. Yes, it really is that easy!</p>
<h3>Update</h3>
<p>I have removed some unneccessary functions from my original clickBox code which were proprietry to our website code. Thanks <a href="http://www.leveret.co.uk/blog/">Jay</a> for pointing these out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alstevens.co.uk/clickable-divs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making google analytics unobtrusive</title>
		<link>http://www.alstevens.co.uk/a-less-obtrusive-google-analytics-script/</link>
		<comments>http://www.alstevens.co.uk/a-less-obtrusive-google-analytics-script/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 14:43:37 +0000</pubDate>
		<dc:creator>Al Stevens</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[DOM scripting]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.alstevens.co.uk/wordpress/?p=77</guid>
		<description><![CDATA[Whenever I implement a new tracking script it always strikes me how obtrusive the scripts tend to be.
They tend to put the script either at the start or the end of the document body, and they tend to use nasty document.writes. I love google analytics but sadly on this count it is no different. In [...]]]></description>
			<content:encoded><![CDATA[<h3>Whenever I implement a new tracking script it always strikes me how obtrusive the scripts tend to be.</h3>
<p>They tend to put the script either at the start or the end of the document body, and they tend to use nasty document.writes. I love google analytics but sadly on this count it is no different. In this article I look at implementing google analytics in a less obtrusive way.<span id="more-77"></span></p>
<p>You may wonder why I would want to look at the implementation of something which does wholly what it sets out to do, so I will start by explaining just a little about how I think the implementation should work.</p>
<ol>
<li>In order to create a manageable code base I want to make sure the javascript is located in just one place. That is to say, if I want to modify the code at some point, I want to do it in just one place</li>
<li>In order to re-use the code I want it to be easily transferrable, without heavy code re-writes</li>
<li>To maintain the integrity of my website I want the behaviour (javascript) to be seperated from the content (html), just in the same way that we do with layout (css) and content (html)</li>
<li>Should heaven forbid, google&#8217;s servers fail to respond quickly I don&#8217;t want it to delay the loading of the whole webpage. This could be a point of contention for some people as if the webpage loads without the analytics script, and a person navigates away to another page, then we have not tracked them. Not good. I would question those worried about this scenario as follows: Which is more important in helping the person fulfill their objectives on your website, the tracking script, or the ability to use your pages as they are meant to be used without waiting. I think the answer is clear.NOTE: Your tracking script provider might have some issue with this. I have heard of in some cases the preference for the tracking script to be after the opening body tag. I think however that you need to think about the people who use your website rather than the ability to track their movements. Having the script in line might also help providers maintain a 99.9% uptime record, since if their server is down their script will stop your page loading. This in turn will make you less likely to navigate to a new page, which will mean that in theory at least, the server has been down less.</li>
</ol>
<h3>So how do we achieve this</h3>
<p>Simple really, using DomScripting methods we will</p>
<ul>
<li>seperate the javascript into it&#8217;s own file (This fulfills 1,2 and 3)</li>
<li>call the javascript on page load instead of in line (This fulfills point 4)</li>
</ul>
<h3>Step 1 &#8211; grab your starting code</h3>
<p>In my case I was provided with the following code by Google Analytics</p>
<pre>&lt;script type="text/javascript"&gt;
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
var pageTracker = _gat._getTracker("UA-999999-1");
pageTracker._initData();
pageTracker._trackPageview();
&lt;/script&gt;</pre>
<p>There are two parts to this script. The first actually loads an external .js file from google using the nasty document.write. The reason it loads it in using javascript is that the script is determining whether it resides on a secure server or not. This resolves any issues with &#8216;mixed&#8217; security error messages on secure servers.</p>
<p>The second part assigns your unique user id and calls a couple of functions from the tracking code</p>
<h3>Step 2 &#8211; putting it all in one script</h3>
<p>So the first thing we need to do is create a script and insert it into the head of your document. (Or wherever you are importing the rest of your scripts)</p>
<pre>&lt;script type="text/javascript" src="/wordpress/wp-content/themes/default/js/googleAnalytics.js"&gt;&lt;/script&gt;</pre>
<h3>Step 3 &#8211; heres the codey bit</h3>
<p>The first thing we need in our code is a function that will call the Google Analytics function on the <a href="http://en.wikipedia.org/wiki/DOM_Events">DOM event</a> of page load. There are lots of references out on the web detailing scripts which allow multiple onLoad functions to be called, and I believe that the one used here is based on <a href="http://simonwillison.net/2004/May/26/addLoadEvent/">Simon Willisons addLoadEvent script</a>. This script is a function that that you can call, to in turn call other functions on page load:</p>
<pre>/* Simplifies onload, you will no longer have to add an onload event call just call addLoadEvent */
function addLoadEvent(func,arg){

if (!arg){
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
}
}
  else{/*if the onload event has an argument/parameter cater for that*/
  if (arg){
  oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func(arg);
    }
     window.onload = function() {
      oldonload();
      func(arg);
    }
  }
}
}</pre>
<p>Now we have a function which can call our analytics function on load we just need to convert the javascript to make it a littel less obtrusive. The more tricly bit involves loading the external js file which I mentioned earlier in part one. For this we create a function which appends the script to the head of the document. This script is called immediately</p>
<pre>function loadGAScript(){
    /*Check browser for Dom compatibility*/
if (!document.getElementsByTagName) return false;
/*Determines whether the page is using a secure or unsecure protocol*/
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
/*Writes in the script to the document head*/
var gaScript = document.createElement("script");
gaScript.setAttribute("src",gaJsHost +"google-analytics.com/ga.js");
gaScript.setAttribute("type","text/javascript");
var domHead = document.getElementsByTagName("head")[0]
domHead.appendChild(gaScript);
    }
    loadGAScript();</pre>
<p>We then just need to call &#8216;part two&#8217; of the script on page load. SO we create a function out of it:</p>
<pre>/*Calls the analytics function*/
function callGA(){   
var pageTracker = _gat._getTracker("UA-999999-1");
pageTracker._initData();
pageTracker._trackPageview();
}</pre>
<p>and then once the dom has loaded we will call this function</p>
<pre>addLoadEvent(callGA);</pre>
<h3>Putting it all together</h3>
<p>So heres the easy bit.</p>
<ul>
<li>Download the <a href="http://www.alstevens.co.uk/downloads/googleAnalytics.js">javascript file</a></li>
<li>Add a reference to the script in your document</li>
<li>Customise your user id</li>
</ul>
<p>and you&#8217;re done</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alstevens.co.uk/a-less-obtrusive-google-analytics-script/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>
