推荐计划 - cookie 等 (Rails)
我正在为我的 Ruby on Rails 应用程序构建一个推荐程序,以便用户可以共享包含其用户 ID (app.com/?r=ID) 的链接。如果访问者登陆应用程序主页时存在推荐人 ID,则主页上的注册表单将包含一个隐藏字段,该字段将填充推荐人 ID。然后,控制器检测 ID,并在被推荐的访客注册时在推荐表中创建新的推荐。它可以工作,下面是这段代码:
@referrer = User.find(params[:r]) rescue nil
unless @referrer.nil?
@referral = Referral.new(:referrer_id=>@referrer.id)
end
非常简单的东西,但很容易破坏(例如:如果访问者离开主页,引用者 ID 就会丢失)。我觉得 cookie 可能是一种更可靠的方法,其中包含推荐人 ID 的 cookie 会在被推荐用户的计算机上存储 x 天。这很常见,尤其是像 Groupon 这样的联属计划,但我从未使用过 cookie,也不知道从哪里开始。
另外,有什么好的方法可以屏蔽或更改推荐系统的 URL 吗?我不喜欢 app.com/?r=1842,而是更喜欢 app.com/x39f3 <- 与给定用户关联的随机生成的数字序列,没有 ?r= 部分。
非常感谢任何帮助。谢谢!
I'm building a referral program for my Ruby on Rails app, such that a user can share a link that contains their user ID (app.com/?r=ID). If a referrer ID is present when a visitor lands on app's homepage, the signup form on the homepage contains a hidden field that populates with the referrer's ID. The controller then detects the ID and creates a new referral in a referral table if the referred visitor signs up. It works, and here's that chunk of code:
@referrer = User.find(params[:r]) rescue nil
unless @referrer.nil?
@referral = Referral.new(:referrer_id=>@referrer.id)
end
Pretty simple stuff, but it's pretty easy to break (ex: if visitor navigates away from the homepage, referrer ID is lost). I feel like cookies could be a more robust method, where a cookie containing the referrer's ID is stored on the referred user's computer for x days. This is pretty commonplace, especially with affiliate programs like Groupon, but I have never worked with cookies and have no idea where to start.
Also, is there any good way to mask or change the URLs of the referral system? Instead of having app.com/?r=1842, I would prefer something like app.com/x39f3 <- a randomly generated sequence of numbers associated with a given user, without the ?r= portion.
Any help is greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答 cookie 问题,设置它们非常容易:
然后以相同的格式读回它们(但没有分配)。我建议将此代码放入应用程序控制器的 before_filter 中。这样,无论访问者首次登陆您网站的哪个页面,都会设置 cookie。
关于将 URL 结构更改为建议的格式,您需要让推荐代码与特定模式匹配,否则您可能会遇到路由问题。例如,如果它们匹配 3 个字母后跟 3 个数字的格式,您可以将以下内容放入您的路由文件中:
对 app#index 的引用应更改为您处理引用的控制器,并且您可以通过以下方式访问referrer_id:参数[:referrer_id]。
希望这有一些用处。
罗宾
To answer the cookie question, it's quite easy to set them:
And then it's the same format to read them back (but without the assignment). I would suggest putting this code in a before_filter in your application controller. This way, the cookie will be set irrespective of the page on which your visitor first lands on your site.
With regards to changing the structure of the urls to the suggested format, you would need to have the referral codes match a specific pattern, otherwise you are likely to run into routing problems. If, for example, they matched the format of 3 letters followed by three numbers, you could put the following your routes file:
The reference to app#index should be changed to the controller in which you handle referrals and you can access the referrer_id through params[:referrer_id].
Hope this is of some use.
Robin