On certain custom post type I would like track when visitor clicks to view for example phone number, website etc. I know I could do this with google analytics but I would also like to save this data to Wordpress, so I could show this info for post author on post edit screen. Also I would like to show total number of clicks and past week/month stats. Now how would you do this?

This is what I'm thinking. With jQuery detect click, then save the value to custom meta key with ajax. This would take care of the total clicks somewhat easily. But what about week/month clicks amount?

Does my idea have any point? Is there better ways to do it (maybe somehow with google analytics api?)?

  • WP Meta and post types don't lend themselves towards storing counters, you'll run into issues with race conditions and blockers and your data will be unreliable with a high margin or error that gets worse as your site scales up
    – Tom J Nowell
    Oct 6 '15 at 1:13

Unless you have a "religious" objection to share data with google then using GA is just the simplest and best way to go. Storing stats in your DB is a good start if you want to bring your server down when there is slightly high traffic, and in any case you can get all the stats that you want via the GA APIS.

  • Agree here - front-end DB writes are a bad idea if you get any significant traffic.
    – vancoder
    Oct 5 '15 at 19:49

As you've alluded to in your question, there are a number of ways to do this but the week/month stats adds a level of complexity that you may not be aware of at initial glance.

The simple way would be to store "clicks" or "views" as a meta value in wp_postmeta but that doesn't give you the timestamp you need.

The more robust solution would be to store the data in a dedicated table. Each "click" or "view" would be inserted as a new row, complete with a timestamp so you can do your weekly / monthly totals.

Please be aware that going the complex route with a dedicated analytics table will bloat your database very quickly and the queries for "totals" will bog down performance as the table gets larger. There are ways around that concern as well (views, caching, rolling archives into a summary table, etc.) but all of that goes deeper into a "database architecture" discussion.

  • Thanks. I was afraid that this would be the case with the more complex stats. I will look if the monthly/weekly stats are really needed and if we could just go simpler total stats way
    – user995317
    Oct 5 '15 at 14:59

I suppose you want maybe something like MOUSEFLOW, they can track your clicks and even you can see a video of what users were doing on your website, but it costs something, you get 100 records for free, if you want more you got to pay, that is all I know, here is the link


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.