<?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; Scripting</title>
	<atom:link href="http://www.alstevens.co.uk/category/scripting/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>Glitchy and jumpy jQuery/jCarousel animation</title>
		<link>http://www.alstevens.co.uk/glitchy-and-jumpy-jqueryjcarousel-animation/</link>
		<comments>http://www.alstevens.co.uk/glitchy-and-jumpy-jqueryjcarousel-animation/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 13:04:26 +0000</pubDate>
		<dc:creator>Al Stevens</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[jQuery jCarousel css troubleshooting]]></category>

		<guid isPermaLink="false">http://www.alstevens.co.uk/?p=300</guid>
		<description><![CDATA[I came across a particularly bizarre bug when working on the new Fluke website. It seemed to appear from nowhere mid-project and manifested itself in a rather odd animation glitch.
I had set up a couple of jCarousel arrays and found that one one page (which had it&#8217;s won unique styling) the carousel animation seemed to [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a particularly bizarre bug when working on the new <a href="http://flukelondon.co.uk">Fluke website</a>. It seemed to appear from nowhere mid-project and manifested itself in a rather odd animation glitch.</p>
<p>I had set up a couple of jCarousel arrays and found that one one page (which had it&#8217;s won unique styling) the carousel animation seemed to glitch, dragging the background with it in a jumpy, jaggedy manner.</p>
<p><span id="more-300"></span></p>
<h3>The solution</h3>
<p>Having searched online I couldn&#8217;t find any other instances of this. After half a day of de-constructing the page I finally found that the css file was to blame. It wasn&#8217;t the contents, but the file itself, which must have somehow become slightly corrupt or contained some hidden characters.</p>
<p>Either way I managed to find an earlier source file, save a new version and copy all the styles over.</p>
<p>Strange, but true and hopefully this post might save someone out there a great deal of time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alstevens.co.uk/glitchy-and-jumpy-jqueryjcarousel-animation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>An accessible &#8220;Starkers&#8221; Wordpress theme</title>
		<link>http://www.alstevens.co.uk/starkers-accessibility-wordpress-idea/</link>
		<comments>http://www.alstevens.co.uk/starkers-accessibility-wordpress-idea/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 13:13:21 +0000</pubDate>
		<dc:creator>Al Stevens</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[accessibility dev code wordpress css]]></category>

		<guid isPermaLink="false">http://www.alstevens.co.uk/?p=158</guid>
		<description><![CDATA[When I came to putting together this blog I was really happy to find the Starkers Wordpress Theme. However with some simple additions – which can be easily implemented – from high contrast layouts and access keys to skip menus &#8211; we could make a massive difference in terms of accessibility to all sorts of [...]]]></description>
			<content:encoded><![CDATA[<h3>When I came to putting together this blog I was really happy to find the <a href="http://elliotjaystocks.com/blog/archive/2008/free-starkers-wordpress-theme/">Starkers Wordpress Theme</a>. However with some simple additions – which can be easily implemented – from high contrast layouts and access keys to skip menus &#8211; we could make a massive difference in terms of accessibility to all sorts of users. This is the concept behind the creation of an Accessible Starkers Theme.<span id="more-158"></span></h3>
<p>So why use Starkers? The Starkers theme is a brillant starting point from any Web Designer/Developer point of view since it gives us the clean starting point from which to style up the website. It starts by stripping out all <abbr title="cascading style sheets">css</abbr>, and implements a universal reset <a href="http://meyerweb.com/eric/thoughts/2007/04/12/reset-styles/">like this one</a>.This is fabulous since it never is easy modifying other peoples <abbr title="cascading style sheets">css</abbr>. More importantly it cleans up the code generated with the result being clean, <a href="http://en.wikipedia.org/wiki/HTML#Semantic_HTML">semantic markup</a> which in theory makes the website more accessible, googleable, maintainable, and more visible by multiple devices. SO in theory, all the building blocks are in place for a highly accessible website.</p>
<p>Yesterday we went to the <a href="http://www.shaw-trust.org.uk/">Shaw Trust</a> in Wales to get our company website audited from an accessibility point of view. The Shaw Trust is a national charity that provides training and work opportunities for people who are disadvantaged in the labour market due to disability, ill health or other social circumstances. They offer a Web Accessibility Accreditation service to businesses which is designed to be the most comprehensive service available and endorsed by GAWDS (The Guild of Accessible Web Designers), it combines a full technical audit with rigorous disability user testing. Every member of the testing team is an experienced assistive technology user and understands the frustrations of not being able to access websites that don&#8217;t consider the needs of disabled users.</p>
<p>It was a really interesting day spending time with real people with differing needs, using the website on an array of different devices, from switches to screen readers. I have been involved with much discussion about accessibility &#8211; normally amongst colleagues and the wider online community, but had never spent time with real people accessing websites on all of these devices. Seeing all the little improvements we could make to our website &#8211; which would make a big difference for people of differing needs made me realise that I have kind of put accessibility no the back seat for a while. Resting happy that my semantic markup would be easily accessible to a screen reader &#8211; I had forgotten about the useful devices which can easily be implemented, from high contrast layouts and access keys to skip menus &#8211; which make a massive difference to all sorts of users.</p>
<p>Suffice to say it will be a while until we can implement these changes to our website but that got me thinking that it might be nice to include some of those accessibility features &#8211; as standard &#8211; in a Wordpress theme.</p>
<h3>The Starkers Accessibility Theme</h3>
<p>So I got to thinking that maybe I could build no the great work done by Elliot Jay Stocks by adding in some standard accessibility components into the Starkers theme. That&#8217;s where I am at the moment and I am wondering what features I would like to include. Off the top of my head I might add in:</p>
<ol>
<li>Skip menu and access keys &#8211; hidden by <abbr title="cascading style sheets">css</abbr> but accessible to keyboard and screen reader users</li>
<li>Style sheet switcher &#8211; to enable a high contrast layout</li>
<li>An accessibility guide for the website</li>
</ol>
<h3>Your feedback</h3>
<p>Is there anything you think should be included, or not included in such a  theme. I&#8217;d love to hear your feedback</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alstevens.co.uk/starkers-accessibility-wordpress-idea/feed/</wfw:commentRss>
		<slash:comments>5</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>31</slash:comments>
		</item>
	</channel>
</rss>
