<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Update: Tracking outbound clicks with Google Analytics and jQuery</title>
	<atom:link href="http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/</link>
	<description>Adventures in front-end consulting</description>
	<lastBuildDate>Wed, 17 Mar 2010 19:27:37 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Vesa Piittinen</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-923</link>
		<dc:creator>Vesa Piittinen</dc:creator>
		<pubDate>Thu, 29 Oct 2009 13:08:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-923</guid>
		<description>Wouldn&#039;t it be more efficient to match wanted links using a CSS selector?

$(&#039;a[href~=&quot;http&quot;]&#039;).click(function() {}

Noticed that a forum page with hundreds of links got sluggish when matching every single link. After this fix it was back to normal.</description>
		<content:encoded><![CDATA[<p>Wouldn&#8217;t it be more efficient to match wanted links using a CSS selector?</p>
<p>$(&#8217;a[href~="http"]&#8216;).click(function() {}</p>
<p>Noticed that a forum page with hundreds of links got sluggish when matching every single link. After this fix it was back to normal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 8 Tips for Tracking With Google</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-863</link>
		<dc:creator>8 Tips for Tracking With Google</dc:creator>
		<pubDate>Wed, 26 Aug 2009 20:34:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-863</guid>
		<description>[...] both a goal and an event in analytics. Thanks to  these contributers listed- for this idea &#8212; rebeccamurphey , think2loud and [...]</description>
		<content:encoded><![CDATA[<p>[...] both a goal and an event in analytics. Thanks to  these contributers listed- for this idea &#8212; rebeccamurphey , think2loud and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: H. Bulter</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-849</link>
		<dc:creator>H. Bulter</dc:creator>
		<pubDate>Wed, 19 Aug 2009 00:06:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-849</guid>
		<description>am gonna try to implement this tomorrow, Rebecca. Thanks!</description>
		<content:encoded><![CDATA[<p>am gonna try to implement this tomorrow, Rebecca. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-645</link>
		<dc:creator>A</dc:creator>
		<pubDate>Wed, 21 Jan 2009 22:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-645</guid>
		<description>Your code uses jQuery to add tracking to all types of links in the document, but seems to still imply that GA&#039;s &quot;pageTracker&quot; is initialized according to Google&#039;s standard instructions (straight script before the ending BODY tag) and not also within a jQuery document.ready().

If using jQuery at all, it would make sense to want to call pageTracker within a domready, like so:




    $(document).ready(function() { 
        var gaTrackCode = &quot;UA-****&quot;;
        var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
            $.getScript(gaJsHost + &quot;google-analytics.com/ga.js&quot;, function() { 
               try
               {
                   pageTracker = _gat._getTracker(gaTrackCode);
                   pageTracker._trackPageview();

                  // THIS LINE I will discuss below
               }
               catch(err)
               {
                   if(window.console) console.log(&#039;Failed to load Google Analytics:&#039; + err);

               }
            }); // end getScript

        }); // end ready



Doing that much will get GA to start counting your pages, and will not block the DOM from rendering while GA gets going.

The problem I ran into with calling GA this way was that the &quot;pageTracker&quot; variable is basically undefined outside of that domready callback function.

Even if you try to make pageTracker a global variable inside the domready so that you could try to use it in other functions, or in other domreadys, it will always be &quot;undefined&quot; because of the asynchronous way that the ga.js script is obtained. That is, other functions and other subsequent domreadys will begin executing BEFORE that first domready that is trying to create pageTracker finishes.

This is a problem because you might want to conditionally inject some javascript into the page with server side programming, but dont want to mess up a nice, neat standard original domready.

The solution for me was to use server side programming to create additional javascript functions that can do more stuff to pageTracker (but might not be present on every page of the site), and call that function from inside the getScript callback.

Right at the spot in the above code that says:

     // THIS LINE I will discuss below

I would put:

    if(typeof gaTransaction == &#039;function&#039;) gaTransaction(pageTracker);

and then stick the following JS anywhere else on the page (either within the same Domready, outside it, in another one, bottom of the page, wherever:

        function gaTransaction(pageTracker) 
        {
            try 
            {                                        
                pageTracker._addTrans(&#039;order_id&#039;,
                                      &#039;affiliate&#039;,
                                      &#039;total_amount&#039;,
                                      &#039;tax_amount&#039;,
                                      &#039;shipping_amount&#039;,
                                      &#039;city&#039;,
                                      &#039;State&#039;,
                                      &#039;country&#039;);
                pageTracker._addItem(&#039;order_id&#039;,
                                      &#039;sku&#039;,
                                      &#039;product_name&#039;,
                                      &#039;category&#039;,
                                      &#039;price&#039;,
                                      &#039;quantity&#039;);
                pageTracker._trackTrans();

            } catch(err) 
            {
                if(window.console) console.log(&#039;Failed to send GA transaction:&#039; + err);
            }
        } // end function gaTransaction()


Doing that allows me to have a tidy original domready that initiates the standard page tracking cleanly, while still allowing other operations and functions to use the asyncronously obtained &quot;pageTracker&quot; object.

The annoying part is that you do have to keep adding &quot;typof&quot; conditions to the original domready for every new function where you want to do something else with the pageTracker.

Does anyone have any better ideas on how to initialize GA inside jQuery&#039;s document.ready function while still allowing use of &quot;pageTracker&quot; in isolated blocks of JS code that might be generated at different points in backend code, for example from different template files?</description>
		<content:encoded><![CDATA[<p>Your code uses jQuery to add tracking to all types of links in the document, but seems to still imply that GA&#8217;s &#8220;pageTracker&#8221; is initialized according to Google&#8217;s standard instructions (straight script before the ending BODY tag) and not also within a jQuery document.ready().</p>
<p>If using jQuery at all, it would make sense to want to call pageTracker within a domready, like so:</p>
<p>    $(document).ready(function() {<br />
        var gaTrackCode = &#8220;UA-****&#8221;;<br />
        var gaJsHost = ((&#8221;https:&#8221; == document.location.protocol) ? &#8220;https://ssl.&#8221; : &#8220;http://www.&#8221;);<br />
            $.getScript(gaJsHost + &#8220;google-analytics.com/ga.js&#8221;, function() {<br />
               try<br />
               {<br />
                   pageTracker = _gat._getTracker(gaTrackCode);<br />
                   pageTracker._trackPageview();</p>
<p>                  // THIS LINE I will discuss below<br />
               }<br />
               catch(err)<br />
               {<br />
                   if(window.console) console.log(&#8217;Failed to load Google Analytics:&#8217; + err);</p>
<p>               }<br />
            }); // end getScript</p>
<p>        }); // end ready</p>
<p>Doing that much will get GA to start counting your pages, and will not block the DOM from rendering while GA gets going.</p>
<p>The problem I ran into with calling GA this way was that the &#8220;pageTracker&#8221; variable is basically undefined outside of that domready callback function.</p>
<p>Even if you try to make pageTracker a global variable inside the domready so that you could try to use it in other functions, or in other domreadys, it will always be &#8220;undefined&#8221; because of the asynchronous way that the ga.js script is obtained. That is, other functions and other subsequent domreadys will begin executing BEFORE that first domready that is trying to create pageTracker finishes.</p>
<p>This is a problem because you might want to conditionally inject some javascript into the page with server side programming, but dont want to mess up a nice, neat standard original domready.</p>
<p>The solution for me was to use server side programming to create additional javascript functions that can do more stuff to pageTracker (but might not be present on every page of the site), and call that function from inside the getScript callback.</p>
<p>Right at the spot in the above code that says:</p>
<p>     // THIS LINE I will discuss below</p>
<p>I would put:</p>
<p>    if(typeof gaTransaction == &#8216;function&#8217;) gaTransaction(pageTracker);</p>
<p>and then stick the following JS anywhere else on the page (either within the same Domready, outside it, in another one, bottom of the page, wherever:</p>
<p>        function gaTransaction(pageTracker)<br />
        {<br />
            try<br />
            {<br />
                pageTracker._addTrans(&#8217;order_id&#8217;,<br />
                                      &#8216;affiliate&#8217;,<br />
                                      &#8216;total_amount&#8217;,<br />
                                      &#8216;tax_amount&#8217;,<br />
                                      &#8217;shipping_amount&#8217;,<br />
                                      &#8216;city&#8217;,<br />
                                      &#8216;State&#8217;,<br />
                                      &#8216;country&#8217;);<br />
                pageTracker._addItem(&#8217;order_id&#8217;,<br />
                                      &#8217;sku&#8217;,<br />
                                      &#8216;product_name&#8217;,<br />
                                      &#8216;category&#8217;,<br />
                                      &#8216;price&#8217;,<br />
                                      &#8216;quantity&#8217;);<br />
                pageTracker._trackTrans();</p>
<p>            } catch(err)<br />
            {<br />
                if(window.console) console.log(&#8217;Failed to send GA transaction:&#8217; + err);<br />
            }<br />
        } // end function gaTransaction()</p>
<p>Doing that allows me to have a tidy original domready that initiates the standard page tracking cleanly, while still allowing other operations and functions to use the asyncronously obtained &#8220;pageTracker&#8221; object.</p>
<p>The annoying part is that you do have to keep adding &#8220;typof&#8221; conditions to the original domready for every new function where you want to do something else with the pageTracker.</p>
<p>Does anyone have any better ideas on how to initialize GA inside jQuery&#8217;s document.ready function while still allowing use of &#8220;pageTracker&#8221; in isolated blocks of JS code that might be generated at different points in backend code, for example from different template files?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Black</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-638</link>
		<dc:creator>Steven Black</dc:creator>
		<pubDate>Tue, 13 Jan 2009 03:10:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-638</guid>
		<description>Once again you&#039;ve created something that&#039;s helped me, Rebecca!  Thanks.

A small upgrade: users may have javascript but, via the magic of the MVP hosts file, (which I highly recommend) Urchin may not load.  Therefore, to be clean, I added the following just inside the click callback.

 if (typeof(pagetracker) !== &quot;function&quot;) {return;}

For reference: Blocking Unwanted Parasites with a Hosts File http://www.mvps.org/winhelp2002/hosts.htm

This file is frequently updated.  Don&#039;t be fooled by the &quot;2002&quot; in the URL.  Don&#039;t let family and friends surf the web without it.</description>
		<content:encoded><![CDATA[<p>Once again you&#8217;ve created something that&#8217;s helped me, Rebecca!  Thanks.</p>
<p>A small upgrade: users may have javascript but, via the magic of the MVP hosts file, (which I highly recommend) Urchin may not load.  Therefore, to be clean, I added the following just inside the click callback.</p>
<p> if (typeof(pagetracker) !== &#8220;function&#8221;) {return;}</p>
<p>For reference: Blocking Unwanted Parasites with a Hosts File <a href="http://www.mvps.org/winhelp2002/hosts.htm" rel="nofollow">http://www.mvps.org/winhelp2002/hosts.htm</a></p>
<p>This file is frequently updated.  Don&#8217;t be fooled by the &#8220;2002&#8243; in the URL.  Don&#8217;t let family and friends surf the web without it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Demolabo</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-636</link>
		<dc:creator>Demolabo</dc:creator>
		<pubDate>Wed, 07 Jan 2009 14:36:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-636</guid>
		<description>I would recommend using also keypress event to the tracking code as follows :

$(&#039;a&#039;).bind(&#039;click keypress&#039;, function(event) { [...]
instead of 
$(&#039;a&#039;).click(function() { [...]</description>
		<content:encoded><![CDATA[<p>I would recommend using also keypress event to the tracking code as follows :</p>
<p>$(&#8217;a').bind(&#8217;click keypress&#8217;, function(event) { [...]<br />
instead of<br />
$(&#8217;a').click(function() { [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Track outbound clicks with Google Analytics and jQuery &#124; blog.rebeccamurphey.com</title>
		<link>http://blog.rebeccamurphey.com/2008/12/04/update-tracking-outbound-clicks-with-google-analytics-and-jquery/comment-page-1/#comment-553</link>
		<dc:creator>Track outbound clicks with Google Analytics and jQuery &#124; blog.rebeccamurphey.com</dc:creator>
		<pubDate>Fri, 05 Dec 2008 04:43:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rebeccamurphey.com/?p=159#comment-553</guid>
		<description>[...] Update: Tracking outbound clicks with Google Analytics and jQuery  [...]</description>
		<content:encoded><![CDATA[<p>[...] Update: Tracking outbound clicks with Google Analytics and jQuery  [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
