On any website, such as on StackOverflow, each question has a view count, and user reading a question but has previous read it won't count twice.
I have some ideas about how it is implemented and using what tables to do it.
What do you think is the best way to implement this?
What counts as a view on YouTube? Each time a viewer intentionally initiates the playing of a video on their device and watches for at least 30 seconds, that counts as a view. Pretty simple! If you play your own video, that will be counted as a view.
Do replays count as views on YouTube? Yes, but only if the replays seem natural. If you replay a video once, it will count as a view. However, if you constantly refresh the page to artificially up the view count, YouTube will pinpoint this as a spamming practice (see the Views, reloaded definition above).
Experts believe that after 4 or 5 views in one day, YouTube stops adding new views to a video's view count number. So, don't make the mistake of thinking you can just get your friends or team members to watch your videos over and over again, because the answer is, you just can't.
The answer is yes, provided the video is being played for the duration of 30 seconds or more. If you are watching a video, again and again, let's say for 15 times for 30 seconds or more, you have gained 15 new views.
You have a couple of options as I see it.
Cookies
You can store a cookie in the users browser for each page you are logging views on. Check for this cookies existence and don't log a view if the cookie already exists.
Downside to this is that it won't work if cookies are disabled or someone is trying to game the system.
On the plus side you don't have to worry about the storage of potentially millions/billions of rows of table data.
Database
You hold a record for each view. Relating that record to a user in some way e.g. MemberID, IP Address; something that should be unique to the user. IP is not ideal but good enough if you are not requiring users to login.
So you would have for example a table with the following columns,
The date will be useful for a couple of reasons,
If your application gets popular in this situation then you will need to deal with the storage implications. I run a popular Facebook app which results in over 100,000 view rows being added each day. Realistically though if your app gets so popular that it becomes a problem then you'll have much bigger issues to deal with.
On my website I deal with counting guest views and the 'mass of data' this produces by dividing down the number of views using a random number.
Say I have a random number generator with a good distribution between 0 and 1, and I'm getting 100,000 views a day on a particular page. If I call the 'logView()' function every view, but in it generate an new random number and only really log the view to the DB when the random number is < 0.001, then for the 100,000 views I will only hit the DB approximately 100,000*0.001 = 1000 times.
If I want to return a view count, then I just divide my DB number by the same value, eg. 1000/0.001 = 100,000. This is approximatly accurate to the nearest 1000 views.
Obviously you can choose a random number range dependent on the load of your site, and even change this if your load changes dramatically (you just need to modify your stored values accordingly).
Also, a page with only 1000 views may not even get a 1 in the view count, but if you have a page with 100,000 views, then the one with 1000 is pretty insignificant.
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