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?
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With