
Tracking Drupal's Fivestar with Google Analytics
Google Analytics can track more than just page views! Here's a simple Drupal module that records an event when a visitor rates content with Fivestar.
fivestar_ga.js
/** * @file * Records a google analytics event when a fivestar rating is made. */ Drupal.behaviors.fivestarGA = function(context) { $('.fivestar-widget .star', context).click(function() { $form = $(this).parents('form'); var contentPath = $("[name=content_path]", $form).val(); var rating = $(this).parent().find('.star').index(this) + 1; _gaq.push(['_trackEvent', 'Rating', rating + ' star', contentPath]); }); };
fivestar_ga.module
<?php /** * Fivestar / Google Analytics integration. */ /** * Implements hook_form_alter(). */ function fivestar_ga_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'fivestar_custom_widget' || strstr($form_id, 'fivestar_form_node_')) { // Store the content path in the rating form. // This will be used later as an identifier for the event. if ($form['content_type']['#value'] == 'node') { $content_path = drupal_get_path_alias('node/' . $form['content_id']['#value']); $form['content_path'] = array( '#type' => 'hidden', '#value' => $content_path, ); } } } /** * Implements hook_init(). */ function fivestar_ga_init() { if (user_access('rate content')) { drupal_add_js(drupal_get_path('module', 'fivestar_ga') . '/fivestar_ga.js'); } }
Comments
Can you expand on the benefits of this approach - why would someone want to do it?
What does it change on the GA side that is helpful?
Wed, 01/19/2011 - 22:58
It's just a different way to visualize the data - pushing events into Analytics gives you access to a variety of charts and reports. Visits that include events aren't counted as bounces, so it can also lower your bounce rate.
Here's a screenshot of what event tracking looks like:
Sat, 01/15/2011 - 23:31
It was unclear how the correct use of the tool. How do I configure fivestar_ga correctly? I could not install on my drupal!
Wed, 01/19/2011 - 21:57
Don't forget the .info file =)
Wed, 07/13/2011 - 14:32
The work looks great but your blog is very vauge!
Clearer directions would be awesome :)
Thu, 07/14/2011 - 07:37
A slight modification which passes on the value to GA, for example a 5 star rating would give return 5 - then in GA you can find the average value of rating etc:
_gaq.push(['_trackEvent', 'Rating',
rating + ' star', contentPath, rating]);
Sat, 08/13/2011 - 00:47
Hi,
Nice script. Do you know if something similar can be used to track data that a user enters into an exposed filter in views with the operator "contains"? I expose a text field to search through title fields of a content type and would like to track what users actually type in... Thanks, Jens
Tue, 11/08/2011 - 04:08
There is no need to store the path of the page in a hidden form field. Just use window.location.pathname in the javascript. Then you can eliminate the hook_form_alter completely.
Drupal.behaviors.fivestarGA = function(context) {
$('.fivestar-widget .star', context).click(function() {
var rating = $(this).parent().find('.star').index(this) + 1;
_gaq.push(['_trackEvent', 'Rating', rating + ' star', window.location.pathname, rating]);
});
};
Tue, 11/08/2011 - 18:57
The path value is necessary when multiple voting widgets are displayed on the same page (for example a View).
Sat, 01/15/2011 - 13:18