Tracking AJAX-driven events in Ruby on Rails for Google Analytics conversion goals
10 comments Latest by replica pandora jewelry Thu, 11 Mar 2010 06:21:33 GMT
Tracking your KPI’s is extremely important in your online venture. At a minimum, you should be using something like Google Analytics to track conversions in your application. Setting up goals is actually quite simple, especially if you’re just tracking that specific pages are loaded. However, if some of your conversion points occur through AJAX, you might not be capturing those activities in Google Analytics.
Lucky for you, it’s actually quite simple to update this. I thought I’d show you a fairly simple example to help you along.
On our web site, we have a mini contact form at the bottom of many of our pages. When submitted, if JavaScript is enabled, it’ll perform an Ajax request to submit the form. If you fill out the main Get in Touch form that gets processed and we redirect people to a thank you page. The URL for that is unique and we’re able to track those in Google Analytics quite easily.
However, with the Ajax-form, the URL in the browser isn’t going to change so Google Analytics isn’t going to track that conversion. So, we needed to track that properly.
To do this, we just need to call a JavaScript function that the Google Analytics code provides you.
pageTracker._trackPageview("/contact_requests/thanks");Let’s look at some simple code from our controller action. If the request is from JavaScript, we currently replace the form area with the content in a partial. (note: if you’re curious about the _x, read Designers, Developers and the x_ factor)
respond_to do |format|
format.html { redirect_to :action => :thanks }
format.js do
render :update do |page|
page.replace :x_mini_contact_form_module, :partial => 'mini_contact_form_thanks'
end
end
endAs you can see, the redirect will within the format.html block will lead people to our conversion point. However, the format.js block will keep the user on the current page and it’ll not trigger Google Analytics to track the conversion. To make this happen, we’ll just sprinkle in the following line of code.
page.call 'pageTracker._trackPageview("/contact_requests/thanks");'However, if you need to do something like this in several locations in your application, you might want to just extend the JavaScriptGenerator page. GeneratorMethods. (you could toss this in lib/, create a plugin, etc…)
module ActionView
module Helpers
module PrototypeHelper
class JavaScriptGenerator #:nodoc:
module GeneratorMethods
# Calls the Google Analytics pageTracker._trackPageview function with +path+.
#
# Examples:
#
#
# # Triggers: pageTracker._trackPageview('/contact_requests/thanks');
# page.track_page_view '/contact_requests/thanks'
#
def track_page_view(path)
record "pageTracker._trackPageview('#{path}');"
end
end
end
end
end
endThis will allow us to do the following:
page.track_page_view "/contact_requests/thanks"
# or using a route/path
page.track_page_view thanks_contact_requests_pathSo, our updated code now looks like:
render :update do |page|
page.replace :x_mini_contact_form_module, :partial => 'mini_contact_form_thanks'
page.track_page_view thanks_contact_requests_path
endWith this in place, we can sprinkle similar code for our various conversion points that are Ajax-driven and Google Analytics will pick it up.
Happy tracking!
Enjoying the content? Be sure to subscribe to my RSS feed.






Nice! This is great insight into tracking user interactions via Ajax. I’m wondering how you decide which interaction is a Pageviews and which one is an Event.
http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
Michael,
In the example I referenced in my post (a form submission), I consider the response a new page view versus an event. Events are typically better classified as things such as:
In my example, I’d see the trackable events as:
The conversion goals are typically related to page views, which was the end result of them engaging in these two steps above.
Mileage may vary…
That’s helpful – thanks. I was trying to figure out how I’d classify the Facebook Connect process that doesn’t yield a page refresh.
Keeping warm in winter is necessary. Ensuring feet warm is especially important. discount ugg cardy boots If my feet feel cold and uncomfortable, I lose all my energy. If feet are cold, a person may catch cold easily. Being ill is terrible for everyone. discount ugg boots Before I find UGG boots, I think it is very difficult to keep feet warm. Now, thanks to UGG boots, I am not afraid of the cold winter any more. And in winter, I only wear UGG boots; wholesale ugg boots it is the best and unique choice.
nyc lady, design hubs, newyork chics, create hubs, fashion dressing, handbag club, purse league
Fishing is not just about fishing. It’s About fishing, during a fishing trip, wow cd keyaion cd keywow time cardeve time cardsaion time cardxbox 360 live pointswow eu cd keywow time cardbuy wow cdkeyfamilies and friends relax together as they enjoy being outdoors and learning new skills. Fishing is considered as a great sport in almost all countries and Canada is no exception. last chaos goldlastchaos goldlc goldlast chaos goldlastchaos goldlc goldlastchaos goldFishing is the kind of gift that lingers, one that’s sure to outlive you and me.What is Fly Fishing.buy wow time cardxbox live 12 month cardxbox 360 live pointsaion eu cdkeyaion us cdkeyeve game cardguildwars cdkeyguild wars cd keycall of duty 6 codeFly fishing is a distinct and ancient angling method, most renowned as a method for catching trout and salmon, but employed today for a wide variety of species including pike, bass, panfish, and carp,buy call of duty 6 codepsn cardPlaystation Network Cardxbox live 12xbox live 12xbox live 12xbox live 12xbox live 12 Monthsxbox live 12 Monatexbox live 12 Meses as well as marine species, such as redfish, snook, tarpon, bonefish and striped bass. Fly lines are heavier than regular fishing line, some made to float and some heavier to sink. last chaos goldacheter lastchaos goldacheter last chaos goldlastchaos gold kaufenlast chaos gold kaufenlast chaos gold kaufFly fishermen seeking bass developed the spinner/fly lure and bass popper fly, which are still used today. xbox live 12 Moissro goldsilkroad online goldaion kinahaion power levelingaion cd keyaion eu kinahaion goldaion powerlevelingaion levelingaion eu cdkeyFly fishing can be done in fresh water or salt water. Fly fishing for trout is a very popular sport which can be done with dry flies, sinking wet flies and sinking nymphs.buy aion cd keywow goldwow goldwow goldwow goldwow goldguild wars cd key freeaion cd key freeaion cd key generatoraion cd key kostenlosWhat About Bass.Bass fishing is a wonderful activity and to derive the maximum benefits out of it, it is better you get to know few basics about the activity and it will be sure your additional knowledge can see you through your success in bass fishing.About TroutTrout tend to be looking upstream for food, explaining the success of fishing “from” downstream while wading upstream. aion cd key shopaion cd keysncsoft time cards buy by paypalaion game key with credit cardcall of duty 6call of duty 6 code ps3call of duty 6 democall of duty modern warfare 2
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
Nice and nicer!
gucci shoes
Are you want to buy uggs?There are many good ugg boots
thanks to UGG boots, I am not afraid of the cold winter any more. And in winter,