2013-08-13 37 views
5

Tôi đang sử dụng Devise với ứng dụng Rails 3.2 của mình và tôi muốn có thể thêm theo dõi đăng ký mới dưới dạng chuyển đổi trong Google Analytics. Tôi muốn người dùng mới được chuyển hướng đến cùng một trang mà họ đang được chuyển hướng đến bây giờ, nếu có thể (tức là có thể là lượt xem qua mà chuyển hướng đến trang hiện tại mà người dùng được chuyển hướng đến sau khi tạo).Cách theo dõi đăng ký người dùng Devise dưới dạng chuyển đổi trong Google Analytics

Ai đó có thể giúp tôi tìm ra cách tốt nhất để làm điều này với Devise không?

# 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 

Trả lời

10

Từ đầu tôi, tôi sẽ sử dụng đèn 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.

Trên 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 

Sau đó, trên quan điểm cho rằng bạn chuyển hướng đến sau khi một đăng ký, tôi muốn làm cho mã cần thiết để kích hoạt sự kiện Google Analytics dựa trên sự hiện diện của flash[:user_signup].

+1

Điều này hiệu quả. Sử dụng ý tưởng của bạn và bài đăng này http://blog.scoutapp.com/articles/2009/10/27/rails-google-analytics-easy-goal-tracking, tôi đã tìm ra rằng tôi có thể thay đổi URL mà mã Google Analytics đang đọc http://bit.ly/R01O8l (thay đổi _trackPageview()) – yellowreign

1

Bạn có thể làm điều đó từ điều khiển của bạn:

Bước 1: để giữ cho nó có tổ chức, bạn có thể tạo một tập tin app/controllers/concerns/trackable.rb với nội dung sau:

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 

Bước 2: Thay thế số theo dõi ID của bạn.

Bước 3: Cuối cùng, theo dõi chuyển đổi của bạn trong điều khiển của bạn:

# 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 

tắm: bạn cũng có thể sử dụng phương pháp track_page_view để theo dõi hành động cụ thể mà không có tầm nhìn (như yêu cầu API).

Thông tin thêm tại đây: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide.

Các vấn đề liên quan