Doug Johnston
Doug Johnston

Reputation: 854

Tracking anonymous users

I have an app that supports email subscriptions, surveys, and comment forms. I've got a model for each (Subscription, SurveyResponse and Comment, respectively). I also have a Visitor model to store shared information like name, email, phone, etc.

I'm wondering how to best tie the models together. Using a one-to-one association between each model and the Visitor model would treat each event separately. So, if John Doe submitted a comment and a survey, he would get two records in the visitors table. I'm leaning towards this approach - it's simpler.

On the other hand, I could set up a one-to-many association on Visitor with a unique constraint on email. I could then attempt to tie each event to an already existing record, something like:

@visitor = Visitor.find_by_email(params[:email]) || Visitor.new(params)

This approach seems problematic since I can never be certain that I'm getting the right record. What if two different people submit a comment using "[email protected]"? Unlikely, but possible

Is there a better way to approach this problem?

A few clarifications:

Upvotes: 2

Views: 448

Answers (2)

greggreg
greggreg

Reputation: 12085

If you're never planning on having a full users system or verifying email addresses then you're probably better off with the one-to-one association. It's essentially the same as hard-coding the fields of the visitor model into each of the other tables, but cutting down on repetition. If you don't care that the same email address can have different names, phone numbers, etc across your site then stick with that.

If you want the names, phone numbers, etc to always be the most recent used then do an update on the visitors table when they submit something.

You can use a cookie to pre-fill in the form fields, for convenience but this doesn't prevent someone from entering in conflicting information on another computer or browser.

Upvotes: 1

Reza
Reza

Reputation: 1577

My suggestion is to store a persistent cookie with a random/unique id on the client side and use it to track the user.

If the user is a known user (Logged in) you can then link that id to the userId in a many to one relation (as a user might login from different computers).

Upvotes: 0

Related Questions