ruby on rails - Keeping track of data for analytics without hitting the database each visit -
i reading question/answer adding home-made analytics rails application. in answer, noted shouldn't hit database each view, didn't elaborate.
i'm adding analytics user profiles on site. each time visits profile, i'm checking whether visitor registered , whether visitor viewing own profile (no need record that) , i'm saving ip, viewer's id (if registred) , viewer's role (my app users rolify) database, each time user's profile viewed.
i can't think of straightforward way avoid saving db each time profile viewed.
can explain how might it?
the (admittedly ugly) code below current implementation show action of users_controller.rb
@user = user.find(params[:id]) ip = request.ip if session['warden.user.user.key'][1] #checks if visitor registered visitor_id = session['warden.user.user.key'][1] #gets user id of visitor visitor_id = visitor_id[0] if @user.id != visitor_id #don't record visits own profile visitor = user.find_by_id(visitor_id) if visitor.has_role?(:somerole) visitor_role = 'lawyer' elsif visitor.has_role?(:someotherrole) visitor_role = 'student' elsif visitor.has_role?(:admin) visitor_role = 'admin' else visitor_role = 'user' end else puts 'visiting own profile' end else visitor_id = nil #unregistered visitor visitor_role = nil #unregistered visitor end if @user.id != visitor_id #don't save visits own profile profileview.create!({:viewer_role => visitor_role, :viewer_id => visitor_id, :ip => ip, :user_id => @user.id}) end
it's upto requirement used in future. if want store user data future purpose analysis purpose store db otherwise can keep on session or cache base. think there purpose of analysis doing right.
Comments
Post a Comment