Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Track Devise User Registration as Conversion in Google Analytics

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
like image 703
yellowreign Avatar asked Aug 13 '13 22:08

yellowreign


People also ask

How do I see registered users in Google Analytics?

See Active Users dataSign in to Google Analytics. Navigate to your view. Open Reports. Select Audience > Active Users.

Can Google Analytics identify individual users?

This identifier can be a single, first-party cookie named _ga that stores a Google Analytics client ID, or you can use the User-ID feature in conjunction with the client ID to more accurately identify users across all the devices they use to access your site or app.


2 Answers

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].

like image 82
jvalente Avatar answered Sep 21 '22 09:09

jvalente


You can do it from your controller:

Step 1: in order to keep it organised, you can create a file app/controllers/concerns/trackable.rb with the following content:

module Trackable
  extend ActiveSupport::Concern

  def track_event(category, action)
    push_to_google_analytics('event', ec: category, ea: action)
  end

  def track_page_view
    path = Rack::Utils.escape("/#{controller_path}/#{action_name}")
    push_to_google_analytics('pageview', dp: path)
  end

  private

  def push_to_google_analytics(event_type, options)
    Net::HTTP.get_response URI 'http://www.google-analytics.com/collect?' + {
      v:   1, # Google Analytics Version
      tid: AppSettings.google_analytics.tracking_id,
      cid: '555', # Client ID (555 = Anonymous)
      t:   event_type
    }.merge(options).to_query if Rails.env.production?
  end
end

Step 2: Replace your tracking ID.

Step 3: Finally, track your conversions in your controller:

# app/controllers/confirmations_controller.rb
class ConfirmationsController < Devise::ConfirmationsController
  include Trackable

  after_action :track_conversion, only: :show

  private

  def track_conversion
    track_event('Conversions', 'from_landing_page')
    # or # track_event('Conversions', user.email)
  end
end

Extra: you can also use the track_page_view method to track specific actions that don't have views (like API requests).

More info here: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide.

like image 32
Lucas Caton Avatar answered Sep 22 '22 09:09

Lucas Caton