User Goal Tracking in Rails with Vanity and Google Analytics

Tony Pitale, Former Viget

Article Category: #Code

Posted on

With the release of Vanity and the availability of the Google Analytics Data Export API, we now have two additional tools to further understand how users are navigating our web applications. I’d like to share how we go about comparing metrics tracked in Vanity to data pulled from Google Analytics with Garb.

Goals & Bounces

Goals can be simply defined as important destinations on our websites and in our applications. When a user gets to a page, or submits a form, or books/buys something, etc., we count that as a goal.

Bounces are another way of saying “leaves”. When a user visits a single page, and then exits their browser, or navigates to another site entirely, we see a bounce.

Tracking Goals

Vanity is a new library for A/B testing and for making it much easier to track goals. Vanity may or may not have been a result of Nathaniel Talbott’s talk on EDD (Experiment Driven Development) which stressed the importance of feature development through experiments (A/B testing) and encouraged the audience to create a tool to make this sort of testing much simpler. Assaf Arkin has achieved this or at least moved us all much closer to the ideal.

Tracking the Goal Using Vanity

I’m not going to talk about using Vanity to set up A/B tests. I am going to show how easy it is to make a comparison between data collected with Vanity via the track! method as compared to data pulled from GA with Garb.

Given the case where we are tracking a registration form, on the page “/users/new”, being submitted:

 metric "Registration Goal" do description "Measure registration goals." end 

This would go in experiments/metrics/registration_goal.rb as per the Vanity documentation.

We can now track this metric in our controller:

 def create @user = User.new(params[:user]) if @user.save # track the metric "registration" track! :registration_goal self.current_user = @user redirect_to root_url else render :action => :new end end 

Once we’re tracking that data we can work to pull the other half of our analytics from Google.

Tracking the Visitor Using Garb

Using Garb we’ll be creating a report that tracks pageviews for a particular page path (/users/new) from earlier.

To work with Vanity — having it report the metrics from GA and graph them for comparison — we need to add a method: values which takes start and end dates.

Here’s what our metric looks like with these added:

 metric "Registration Pageview" do description "Measure page views for /users/new" def values(from, to) report = Garb::Report.new(GARB_PROFILE, {:start_date => from, :end_date => to}) report.metrics :pageviews report.dimensions :page_path, :date report.sort :date report.filter :page_path.eql => '/' # hack because GA data isn't returned if it's 0 data = report.results.inject({}) do |hash, result| hash.merge(result.date => result.pageviews.to_i) end (from..to).map do |day| key = day.strftime('%Y%m%d') data[key] || 0 end end end 

Note that I’ve set a constant GARB_PROFILE to the profile I wish to use. I have yet to determine a more effective way to pass the profile for use by Garb.

We can see that Garb + Vanity Metric (first image) matches the graph provided in GA (second image):

Vanity Graph for /users/new

Google Analytics Graph for /users/new

With this data pulled into the Vanity dashboard, it is trivial to compare the number of visitors to a registration form to the number who completed registration. Thus, we’ve created accurate goal tracking and, by comparing the two sets of data, it is fairly easy to see the bounce rate for the registration page.

Related Articles