Read my latest article: Planet Argon Blog (posted Wed, 17 Feb 2010 15:11:00 GMT)

Tracking Google Analytics events in development environment with GoogleAnalyticsProxy

Posted by Robby Russell Sun, 01 Nov 2009 19:55:00 GMT

20 comments Latest by shenhiujie Thu, 18 Mar 2010 08:02:45 GMT

As mentioned in a recent article1, I’ve been diving deep into Google Analytics lately while working on a few client projects. We’re aiming to use much more of the features of Google Analytics and have been hitting some roadblocks with the development versus production application environments. Once you begin to dive into event tracking and AJAX-driven goal conversions, relying on just the sample code that Google Analytics provides you is going to result in you looking at a handful of JavaScript errors.

pageTracker is not defined

another example from the firebug javascript console…

firebug pageTracker is not defined

We see JavaScript errors like this because we don’t load the google analytics code in our development environments. As you can see, we are only loading this in our production environment.

  <% if RAILS_ENV == 'production' -%>
    <!--// Google Analytics //-->
    <script type="text/javascript">
    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"));
    </script>
    <script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-XXXXXX-1");
    pageTracker._trackPageview();
    </script>
  <% end -%>

To track an event with Google Analytics, you’d need to trigger something like:

  pageTracker._trackEvent('Button', 'Click', 'Get in touch');

As you can see from our code earlier, in development, the pageTracker variable isn’t defined and that’s why we’re getting those JS errors. We also don’t want to add conditionals everywhere in our application to check if we’re in development or production environment.. as that’d just make our views uglier than they need to be. So, I decided that I’d create a proxy class in JavaScript that would allow us to trigger _trackEvent() and _trackPageview() and handle it appropriately.

This class works with the following logic:

  • if google analytics is loaded, pass the parameters to the real pageTracker
  • if google analytics is NOT loaded, output the information to console.log() for debugging purposes

For example, on a gallery on our web site… we track when people navigate next and/or previous through the photos. In our development environment, I can watch the JavaScript console output the following:

Firebug - GAP

And in our production environment, we can see that this was sent to Google Analytics.

Firebug - trackEvent()

We’re able to do this by initializing the GoogleAnalyticsProxy class and calling these functions through it. For example:

  _gap = new GoogleAnalyticsProxy();
  _gap._trackEvent('Video', 'Play', 'Homepage video');
  _gap._trackEvent('Video', 'Pause', 'Homepage video');
  _gap._trackEvent('Button', 'Click', 'Call to action X');

You’ll see that we’re just calling _gap versus pageTracker. We then replace all the instances of pageTracker (except where it is defined in the google analytics code block they provide you). You’ll find this located near the bottom of our application.html.erb file.

<% if RAILS_ENV == 'production' -%>
  <!--// Google Analytics //-->
  <script type="text/javascript">
  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"));
  </script>
  <script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-XXXXXX-1");
  pageTracker._trackPageview();
  </script>
<% end -%>

<script type="text/javascript">
  var _gap = new GoogleAnalyticsProxy();
</script>

We now have _gap available throughout our project and can call _trackEvent() and _trackPageview() with it. Note: You can use any JS variable name that you want, _gap is just what I went with.

Get GoogleAnalyticsProxy

I’ve gone ahead and tossed this small JavaScript class (known as GoogleAnalyticsProxy) on Github for your enjoyment. I have some more articles in the works that will show you some tips for how to make the most of Google Analytics. If you have any questions and/or ideas for related article topics, don’t hesitate to let me know.

1 Tracking AJAX-driven events in Ruby on Rails for Google Analytics conversion goals

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Jamie Macey Sun, 01 Nov 2009 21:00:34 GMT

    So out of curiosity, why write a proxy class with a custom name instead of just calling it pageTracker? Load Analytics in prod, and your proxy (now a standin) in development?

  2. Avatar
    Robby Russell Sun, 01 Nov 2009 21:18:07 GMT Recommend me on Working with Rails

    Jamie,

    I considered that as well, but am also planning to expand this so that I can have some callbacks to our Rails applications as well. On one of our projects in particular, we want to track events in our own DB and are planning to extend this further.

    ...but this could totally be used as a way to mock google analytics too.

  3. Avatar
    Jake Mon, 02 Nov 2009 17:12:08 GMT

    Fantastic idea! I can see how that would be quite useful. I have a lot of conditionals in my Rails application to check if it’s in production or not before triggering GA events. Will definitely try this out.

  4. Avatar
    Jake Mon, 02 Nov 2009 17:13:05 GMT

    Quick question. I can just copy the google analytics proxy js file into my public/javascripts directory and reference like a regular js file?

  5. Avatar
    Robby Russell Tue, 03 Nov 2009 06:17:46 GMT Recommend me on Working with Rails

    Jake,

    Yes, you can just copy that file into your javascripts directory. Let me know if you hit any roadblocks.

    Good luck!

  6. Avatar
    Justin Wed, 04 Nov 2009 23:06:10 GMT

    Great idea. Worked like a breeze!

  7. Avatar
    Clem Sun, 08 Nov 2009 17:13:59 GMT

    Not that I’m not interested in GoogleAnalytics stuff but, just out of curiosity, where did you take this picture of you with the lake in the background? ;-)

    Makes me think of Annecy, FR…

  8. Avatar
    Robby Russell Sun, 08 Nov 2009 21:35:52 GMT Recommend me on Working with Rails

    Clem,

    The photo is taken along the Columbia Gorge, which runs between the states of Oregon and Washington.

    Here is a map

  9. Avatar
    Justin Gallagher Sun, 03 Jan 2010 23:11:32 GMT

    Robby,

    Thanks for this, it was almost exactly what I was looking for to track events in production and log events in development. I’ve been using jQuery in my Rails apps lately, and have forked your project on github and modified the code slightly to work without the Prototype framework. I’ve included the link above in case you or anyone else is interested.

    Thanks again.

    -Justin

  10. Avatar
    susan Wed, 10 Feb 2010 12:17:04 GMT

    UGG may be worn in line with This combination of vogue and Kate Winslet have complete the intellect that keeps toes comfortable all choral the praises of UGG’s wholesale ugg boots and have been seen on Australia’s discount ugg boots beaches to keep their feet warm when they came out of the water.Christian Louboutin shoess. In many ins Wedding dresses tances, the particular panache of discount ugg bailey button

  11. Avatar
    michael jordan shoes Mon, 22 Feb 2010 03:12:11 GMT
  12. Avatar
    gucci shoes Sat, 27 Feb 2010 03:41:52 GMT

    if you want to buy here is some good Website for another you can see this any more jordan shoes air jordan shoes michael jordan shoes

  13. Avatar
    ugg boots sale Sat, 06 Mar 2010 02:33:39 GMT
  14. Avatar
    online accredited diploma Tue, 09 Mar 2010 06:53:09 GMT

    Thanks for sharing. i really appreciate it that you shared with us such a informative post..

  15. Avatar
    ged online Tue, 09 Mar 2010 06:53:38 GMT

    The difference between the right word and the almost right word is really a large matter — it’s the difference between a lightning bug and the lightning.

  16. Avatar
    ged online Tue, 09 Mar 2010 06:53:39 GMT

    The difference between the right word and the almost right word is really a large matter — it’s the difference between a lightning bug and the lightning.

  17. Avatar
    ed hardy sunglass Wed, 10 Mar 2010 08:16:14 GMT

    Thanks for this,.

  18. Avatar
    DHL Wed, 10 Mar 2010 08:38:36 GMT
  19. Avatar
    ed hardy clothing Wed, 17 Mar 2010 07:30:29 GMT
  20. Avatar
    shenhiujie Thu, 18 Mar 2010 08:02:45 GMT

Share your thoughts... (really...I want to hear them)

Comments