How to Track Devise User Registration as Conversion in Google Analytics

devise, ruby-on-rails

Solution

From the top of my head, I'd use flash.

The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out.

On the `registrations_controller.rb`:

if resource.active_for_authentication?

  flash[:user_signup] = true # or something that you find more appropriate

  set_flash_message :notice, :signed_up if is_navigational_format?
  sign_up(resource_name, resource)
  respond_with resource, :location => after_sign_up_path_for(resource)
else
  set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
  expire_session_data_after_sign_in!
  respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end

Then, on the view that you redirect to after a signup, I'd render the necessary code to trigger a Google Analytics event based on the presence of `flash[:user_signup]`.

Problem

I'm using Devise with my Rails 3.2 app and I want to be able to add track new registrations as conversions in Google Analytics. I'd like to have the new users directed to the same page that they are being redirected to now, if possible (i.e. may be a pass thru view that redirects to the current page users are redirected to after create). Can someone please help me figure out the best way to do this with Devise? ``` # users/registrations_controller.rb # POST /resource def create build_resource if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location => after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end end def after_sign_up_path_for(resource) after_sign_in_path_for(resource) end ```

Original source