Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to set a tracking (permanent) cookie in Rails?

I'm trying to track an "anonymous" user's actions and eventually associate them with their account once they register. I figured I'd do this by setting a permanent cookie. My best option is to have the following in the ApplicationController:

class ApplicationController < ActionController::Base
  before_filter :set_tracking_cookie
  def set_tracking_cookie
    cookies.permanent[:user_uuid] = SecureRandom.uuid unless cookies[:user_uuid]
  end
end

Is this the correct way or are there better solutions?

like image 609
janosrusiczki Avatar asked Apr 04 '13 09:04

janosrusiczki


People also ask

How do I protect my tracking cookies?

You can prevent tracking cookies from spying on your online activity by installing ad block plugins that come along with most major browsers like Firefox, Chrome, Internet Explorer and so on. There are a number of good ad block plugins that you can find on the web by searching 'ad block plugins' in Google.

How long are cookie tracks?

Session cookies only last as long as your browser is open and are automatically deleted when a user closes the browser or exits the app. On the other hand, persistent cookies will continue to exist even after a browser or app is closed. They are used by websites to remember a user and their preferences on a website.

What are cookies rails?

Cookies, Sessions and Flashes are three special objects that Rails gives you in which each behave a lot like hashes. They are used to persist data between requests, whether until just the next request, until the browser is closed, or until a specified expiration has been reached.


1 Answers

Looks good, the permanent cookie has a expiration far in the future (20 years or so) so as long as the user does not manually get rid of it you should be able to track him.

I used constructs like this in a lot of places and it works like charm. You can even make it work on external landing pages if you include something to be loaded through this action (typical tracking pixel).

like image 108
maxigs Avatar answered Sep 27 '22 16:09

maxigs