Follow Along as I Stumble on the Path to Learning RoR

Tracking Conversions in Google Analytics for Devise User Sign-Ups

I wanted to track user sign-ups as a goal, while using the Devise gem.  The Devise gem redirects users to the root path as a default, which I wanted to keep.  The problem with that is that I didn’t want to every visit to the root page as a conversion, only the ones that were a redirect after the user registered.

Here’s what I ended up doing:

In the users#registrations_controller.rb, I added this flash:

flash[:user_signup] = true

then in the root path view, I added:

<% if flash[:user_signup] == true %>
<%= render :partial => “shared/google_analytics_user_sign_up” %>  # edited google analytics code
<% else %>
<%= render :partial => “shared/google_analytics” %> # regular google analytics code
<% end %>

In the google_analytics_user_sign_up partial, I have this:

<script>
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-99999999-1’, ‘foo.com’);
ga(‘send’, ‘pageview’, ‘/special/path’);
</script>
<!– Google Analytics end –>

In Google Analytics, add it as a goal, in the destination add:

/special/path

You can name the path whatever you want, as long as they match in the code and in Google Analytics.  Just make sure that it’s unique, it’s not the path of another url someone might go to.

Advertisements

Comments are closed.