How to track user actions and custom events with Google Analytics and jQuery
This is a customization of Rebecca Murphey’s script:
1 $('a').each(function() { 2 var $a = $(this); 3 var href = $a.attr('href'); 4 5 if(typeof pageTracker == 'undefined') { return; } 6 7 // Link is external 8 if (href.match(/^http/) && !href.match(document.domain)) { 9 $a.click(function() { 10 pageTracker._trackPageview('/external/' + href); 11 }); 12 } else { 13 $a.click(function() { 14 pageTracker._trackPageview('/internal' + href); 15 }); 16 } 17 });
Note that clicks are shown as page views in reports, so you should exclude them from all reports. A future version of Google Analytics will allow you to track events, such as mouse clicks, without affecting page view reporting, see this page on the new event tracking beta feature for more information.
Opening external links in a new window with jQuery and "rel=external"
Usually you would do this with target=’blank’, but that attribute has been removed, so use this instead:
1 $('a[rel=external]').click(function() { 2 window.open(this.href); 3 return false; 4 });
How to display an animated icon during Ajax request processing
This example uses the jQuery JavaScript library.
First, create an Ajax icon using the AjaxLoad site.
Then add the following to your HTML :
1 <img src="/images/loading.gif" id="loading-indicator" style="display:none" />
And the following to your CSS file:
1 #loading-indicator { 2 position: absolute; 3 left: 10px; 4 top: 10px; 5 }
Lastly, you need to hook into the Ajax events that jQuery provides; one event handler for when the Ajax request begins, and one for when it ends:
1 $(document).ajaxSend(function(event, request, settings) { 2 $('#loading-indicator').show(); 3 }); 4 5 $(document).ajaxComplete(function(event, request, settings) { 6 $('#loading-indicator').hide(); 7 });
Showing ActiveRecord error messages from jQuery Ajax actions and scripts
The HTML , in a layout file, for example application.html.erb:
1 <div id="error-message" style="display:none"> 2 </div>
The JavaScript, rendered by for example create.js.erb:
1 <% if !@category.valid? %> 2 <% 3 errors = <<ERR 4 <p>Please fix the following errors:</p> 5 <ul> 6 #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } } 7 </ul> 8 ERR 9 %> 10 $('#error-message').html('<%= escape_javascript(errors) %>'); 11 $('#error-message').show(); 12 <% else %> 13 $('#error-message').hide(); 14 <% end %>
How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"
All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.
This is a slight variation of his code:
In application.html.erb, or whatever layout file you’re using, put:
1 <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>
In application.js, or whatever JavaScript file you’re using, put:
1 $(document).ajaxSend(function(event, request, settings) { 2 if (typeof(window.AUTH_TOKEN) == "undefined") return; 3 settings.data = settings.data || ""; 4 settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN); 5 });
That’s all…