<?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>Travis Beck &#187; analytics</title>
	<atom:link href="http://travisjbeck.com/blog/tag/analytics/feed/" rel="self" type="application/rss+xml" />
	<link>http://travisjbeck.com/blog</link>
	<description>The Modern Web</description>
	<lastBuildDate>Fri, 06 Apr 2012 19:33:20 +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>Tracking Ajax calls in Google Analytics</title>
		<link>http://travisjbeck.com/blog/javascript/tracking-ajax-calls-in-google-analytics/</link>
		<comments>http://travisjbeck.com/blog/javascript/tracking-ajax-calls-in-google-analytics/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 01:40:58 +0000</pubDate>
		<dc:creator>Travis</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[analytics]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[mootools]]></category>

		<guid isPermaLink="false">http://travisjbeck.com/blog/?p=45</guid>
		<description><![CDATA[I&#8217;m addicted to analytics. I check them constantly. Like most people, I use Google Analytics. Google Analytics is a great, free, incredibly powerful, analytics platform. It offers many advanced tracking options and data, most of which, few people will ever use. As great as Google Analytics is, it does not track ajax request out-of-the-box. But [...]]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;m addicted to analytics.  I check them constantly. Like most people, I use <a href="http://www.google.com/analytics/">Google Analytics</a>.  Google Analytics is a great, free, incredibly powerful, analytics platform.  It offers many advanced tracking options and data, most of which, few people will ever use.
</p>
<p><span id="more-45"></span></p>
<p>
As great as Google Analytics is, it does not track ajax request out-of-the-box.  But don&#8217;t fear, tracking Ajax calls with Google Analytics is easier than you think.  I&#8217;m not going to go over how to set up Google&#8217;s tracking code on your site, I&#8217;m going to assume you&#8217;ve already got analytics up and running.
</p>
<h2>Method 1<br />
<h2>
<h3>The fast and dirty way</h3>
<p>
First off, I&#8217;m going to assume you&#8217;re using some sort of JavaScript framework. I prefer <a href="http://mootools.net">MooTools</a>, but this method should work with any framework. Every framework&#8217;s Ajax request methods have a built in &#8220;successful request&#8221; callback.  This method is usually and appropriately called <code>onSuccess</code>. This callback method means that your request has completed and returned a successful response. You&#8217;re probably already familiar with Google Analytic&#8217;s <code>pageTracker._trackPageview();</code> code, since you have it in every page you are already tracking. That method also takes an optional string argument, usually the name of the page you are tracking.  By default that argument is the current page that the code is on.  We&#8217;re going to use that optional argument in our <code>onSuccess</code> callback to track our request. Here&#8217;s a simplified version of the Ajax request code from the contact form on this site using MooTools 1.2.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'send'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">//a successful request</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
   onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">//a failed request</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
For this particular example we want to track a successful contact form submittal using a unique string that will show up in our analytics page. I decided to use &#8220;/contactFormSubmitted&#8221;.  This is the name of the &#8220;page&#8221; that will show up in Google Analytics. You can use the actual page name that the request is submitted to or any identifier you want. So lets add the code to our success callback
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'send'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">//a successful request</span>
      pageTracker._trackPageview<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/contactFormSubmitted'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
   onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">//a failed request</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
Now anytime someone successfully fills out and submits the contact form on my site, i&#8217;ll see it show up in the content section on Google Analytics, as seen here.<br />
<img src="http://travisjbeck.com/blog/wp-content/uploads/2008/09/analytics-ajax.gif" alt="" title="analytics-ajax" class="aligncenter size-full wp-image-47" /><br />
I know, the numbers are overwhelming, but the point is, my Ajax request is being correctly tracked.
</p>
<h2>Method 2</h2>
<h3>Extending the Request Class to automatically track the requested url</h3>
<p>
So another way to do this would be to extend the Request class itself and have it track the URL automatically after a successful request. Once again, this is specific to MooTools 1.2. What i want to do is overwrite the default <code>Request.onSuccess</code> method with our new functionality while maintaining the original functionality. This method eliminates the need to add the tracking code to every single success callback in you code base.  Very useful for retrofitting large sites with lots of Ajax calls or just getting free functionality.
</p>
<p>
To implement, simply add the following lines somewhere in your JavaScript file.  Just make sure the file you&#8217;re adding it to is embedded after MooTools.
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Request.<span style="color: #660066;">implement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">//google pageview code</span>
      pageTracker._trackPageview<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">//default onSuccess code</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'complete'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'success'</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">callChain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
This overwrites the default <code>onSuccess</code> method for the Request Class, keeping the original functionality in tact but adding the tracking code for every Ajax call in your application. The URL of the request will show up on your analytics page.</p>
]]></content:encoded>
			<wfw:commentRss>http://travisjbeck.com/blog/javascript/tracking-ajax-calls-in-google-analytics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

