在用户注册/登录Rails + Devise时执行JS代码 [英] Execute JS code on user sign up/sign in with Rails + Devise

查看:233
本文介绍了在用户注册/登录Rails + Devise时执行JS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Kissmetrics(分析应用)JS API文档,并且他们有一个识别方法,让您告诉Kissmetrics有关用户。以下是他们的文档说明:


我们建议您在两种情况下呼叫识别:



当用户成功注册
当用户成功登录时,通过cookie或通过登录页面


什么是最好的方式来实现呢?如何检测用户刚刚注册或登录的时间?

解决方案

将问题分成两部分:注册并登录。



您可以检测新用户何时在您的应用程序中注册,只需在用户模型中添加一个 after_create 钩子。类似于

  class User< ActiveRecord :: Base 
after_create:register_hook

def register_hook
#你的代码在这里
end
end

检测用户登录时有点脏。 Devise修改用户登录时的 user.current_sign_in_at 属性,因此您可以添加一个 before_save hook在用户模型中,检查 current_sign_in_at 属性是否已更改。它应该是这样的:

  class User< ActiveRecord :: Base 
before_save:login_hook,if => current_sign_in_at_changed?

def login_hook
#你的代码在这里
结束
结束

一旦您有正确的回调来检测登录/注册,您可以创建一个带有该信息的cookie,并从javascript中读取或为用户编写一个帮助器方法模型,并在布局中写下如下内容:

 <%if current_user.just_signed_in%> ; 
< script type =text / javascript>
//您在此处登录统计代码
< / script>
<%end%>

<%if current_user.just_signed_up%>
< script type =text / javascript>
//你在这里注册stats代码
< / script>
<%end%>

按照这种方式,完整的模型将是:

  class User< ActiveRecord :: Base 
after_create:register_hook
before_save:login_hook,if => current_sign_in_at_changed?

attr_accessor:just_singed_up,just_signed_in

def register_hook
@just_signed_up = true
end

def login_hook
@just_signed_in = true
end
end


I'm looking at the Kissmetrics (analytics app) JS API docs, and they have an "identify" method that lets you tell Kissmetrics about a user. Here's what their docs say:

We recommend you call identify in two scenarios:

When a user successfully signs up When a user successfully logs in, either through cookies or through a login page

What would be the best way to achieve that? How can I detect when a user has just signed up or signed in?

解决方案

Lets divide the problem in two: register and log in.

You can detect when a new user has registered in you app just adding a after_create hook in you User model. Something like

class User < ActiveRecord::Base
  after_create :register_hook

  def register_hook
    # your code here
  end
end

Detecting when the user logs in is a little bit dirtier. Devise modify the user.current_sign_in_at attribute of the user when he/she logs in, so you could add a before_save hook in the user model and check if the current_sign_in_at attribute has changed. It should be something like:

class User < ActiveRecord::Base
  before_save :login_hook, :if => current_sign_in_at_changed?

  def login_hook
    # your code here
  end
end

Once you have the right callbacks for detecting sign in/sign up, you can just create a cookie with the info and read it from the javascript or write a helper method for the User model and write something like this in your layout:

<% if current_user.just_signed_in %>
  <script type="text/javascript">
    // Your sign in stats code here
  </script>
<% end %>

<% if current_user.just_signed_up %>
  <script type="text/javascript">
    // Your sign up stats code here
  </script>
<% end %>

Following this way, the complete model would be:

class User < ActiveRecord::Base
  after_create :register_hook
  before_save :login_hook, :if => current_sign_in_at_changed?

  attr_accessor :just_singed_up, :just_signed_in

  def register_hook
    @just_signed_up = true
  end

  def login_hook
    @just_signed_in = true
  end
end

这篇关于在用户注册/登录Rails + Devise时执行JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆