How to track post views by logged in user? (using post_meta) (11 posts)

  1. MickR
    Posted 3 years ago #

    Hello all,

    My first post on the forum so hopefully in the right place.

    I have a requirement to track which logged in users have viewed which posts (the latest 50 users per post max). I'm thinking one way to do this is to create/update entries in the wp_postmeta table whenever a post is viewed, however i'm struggling getting my mind round how to prevent concurrent update issues.

    In order to limit the database entries for each post to the last 50, i would need to get the postmeta before updating it, search for the oldest entry, delete it, and replace it with the details of the latest visit (current_user name and date).

    The issue (in my mind anyway) is how to prevent the postmeta data for a post being retrieved concurrently should two or more users visit a post at the same time. The risk is that the original postmeta data would be processed twice and the new postmeta entry would only reflect one of those visits.

    Hope that makes sense, and if anyone has any thoughts (or possible other approaches) would be great to hear them.



  2. Tara
    Volunteer Moderator
    Posted 3 years ago #

    ake a look at this WP-Snippet, you might be able to tweak it to fit your needs: http://wp-snippets.com/post-views-without-plugin/

  3. MickR
    Posted 3 years ago #

    Hi t-p, thanks for the response. Had a look at that and it's not really what i'm after. The difference being that that's a simple count updating the same individual postmeta record each time a post is viewed.

    What i'm trying to achieve is to store the IDs of the last 50 unique logged in users who view each post. The owner of a post will then be able to see which other registered users have visited their post. (In this particular application each registered user will only be allowed to create one post).

    I'm bouncing between possibly using duplicate keys in the postmeta table, or alternatively creating/using a new DB table as a solution - if i could work out whether it's possible to update the oldest of postmeta entries that have duplicate keys i'd be one happy chappy!

  4. bcworkz
    Posted 3 years ago #

    How about just letting the table grow beyond 50 entries, temporarily. Then you just have to log each access in turn. Then once in a while, schedule a task to go in and delete the older records so that the table is only 50 records long again.

  5. MickR
    Posted 3 years ago #

    Hi bcworkz,

    You're right, may be the way to go, and possibly using a cron job to do the tidying up.

    One thing i'm not clear on though is whether you're thinking add to the wp_postmeta table, or possibly creating a new table for the purpose.

    If a new table, would you have any thoughts on the choice between one new table per user (post), versus one overall table where all visit stats are stored for every user.

    If i'm storing 50 records for each user, a single global table could have anywhere from, let's say, 500,000 records upwards. Whilst adding a record for each post view doesn't have any real overhead, i'm wondering if retrieving the data (50 records spread across the table) for a user to see who has viewed their post could be processor heavy?

    Obviously i'm still bouncing thoughts around here, but appreciate the feedback.

  6. bcworkz
    Posted 3 years ago #

    Umm... I wasn't thinking that far into it, to tell the truth. Now that you bring it up, for that kind of scale, definitely create your own table to contain this data. But I think creating a table for each post would highly impractical. My understanding of mySQL is that reading data is relatively light on resource in comparison to writing data, though writing one row obviously is not a big deal. But neither is reading 50 divergent rows. The magic of a good indexing and caching system I guess.

  7. yunieskid
    Posted 3 years ago #

    I am trying to get the posts viewed by a certain loged in user, I have how to count the number of views in a post, but i can't retrieve the count of views by an given user.
    What I need is to have a register of the amount of posts by a user, to identify the category of those posts and then have an idea of the favorite category for the loged in user, and then in the home page, show to the user the posts belonging to that category.

    Any help for solving this problem?.

  8. bcworkz
    Posted 3 years ago #

    @yunieskid, you should really start your own topic so your question gets maximum exposure. People that may be able to help you often skip over threads with several responses because such threads have usually devolved into a two way conversation. The forum admins also prefer you do this because it helps with their own statistics collection.

    To address your question anyway, there's a number of possible approaches. I personally would probably first try inserting a function call on every applicable template. That function would add a row into a table specifically setup to accumulate such data. You could then make specific mySQL queries to calculate whatever statistics you need.

  9. yunieskid
    Posted 3 years ago #

    @bcworkz, thanks for your reconmedations related to the way i have to make my questions, as a matter o fact i did also post the question as a new topic, i've been searching in other sites and i didn't find so much, and i did just make something similar to what you told me, and what i am going to do is to create a table in the database with 3 columns, the current user ID, the category of the post that is visiting and the amount of times that the user visits posts with that category, USER_ID, CATEGORY, COUNT_VISITS, so i will be able to determine what is the more visited category by each logged in user, and later perfomr what i need.

  10. MickR
    Posted 3 years ago #

    hi bcworkz,

    Thought i'd drop back in and let you know how i got on.

    In the end wrote a plugin to address the need. Plugin creates a new DB table on activation and I opted for a column based solution. Long and short of it is that for each new post 50 rows are automatically created. One of those is an auto-updated timestamp and i'm using that to roll round and update the oldest entry when the 51st unique visit takes place.

    Plugin also has a shortcode function which displays the stats when called. (Avatar of person who visited, how many times they visited the post, the last time they visited, and a link to their profile).

    In test mode and so far so good :)

  11. bcworkz
    Posted 3 years ago #

    @MickR, thanks for the update. Sounds like a great implementation to meet your needs. I enjoy watching an idea get bounced around and develop on sites like this. I'm confident you will have continued success.

Topic Closed

This topic has been closed to new replies.

About this Topic