Reputation: 14002
so first of all i checked and researched this question in stackoverflow and google but there no similar question.
i checked Count number of consecutive visits and SQL Query: Visited the site each day for 30 consecutive days but it doesn't help.
so my question is where to implement this action of testing if a user has visited the site for a consecutive x time, in the links that i put there they check in server side, but i think it's not very optimised because each time the user visits the website a query is triggered to check if todays date is equal or higher than LastVisit
date.
in my application i already do query every time the user loads a page to retrieve his information but if i add an
update userInfo
set DaysConsecutivelyVisited=DaysConsecutivelyVisited+1
when DATEDIFF(CURDATE(),LastVisit)=1
then i'll have two queries every time the user loads the page, so i tought maybe it's better if i do this in client side rather than in server side. I'll put an attribute or hidden span in the document
<body data-lastvisit="2012-03-25" >
and in the javascript i'll do the analysis and if the $("data-lastvisit")
is equal to yesterday i'll send an ajax reqeuest to update the DaysConsecutivelyVisited
what do you think, is it a good idea ?
Upvotes: 0
Views: 97
Reputation: 4371
You'll need a parameter to compare to.. which is - unfortunately - always the data inside your database table. The only thing you can do is to retrieve the userinfo on pageload, which most likely includes a field last_visited
. If the date is not equal to todays date, registered it.
Furthermore, I'd say create a field consecutive_visits
and reset to 0 in case the date's difference is larger than a day and increment it in case there's a day or less difference. This way you'd always have the amount of consecutive visits in the userobject after fetching it on pageload.
I absolutely do not see any need for an ajax request in this case. It's just a compare of the current user data with a Date() instance or timestamp. All calculating can be done serverside and there's only need for an extra MySQL query in case the values are different.
Upvotes: 2