Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

Php track visits info via piwik (possibly via api or php piwik interface)

Id like to track views to a set of pages i have, and make a report of them. Im having trouble as there are so many ways in piwik to do this. there is an api to return info via xml or json etc. this works well to get page information as i have piwik all ready setup. i also noticed theres a way to make custom values in php via a php tracker.

So how do i implement this:
i have a page set of /single_event_page.php?id=64 where the id= is the event id of my mysql record.


this works very well, but im trying to capture multi event listings page views and save them daily and a total for the month in my database

i was thinking of doing this in a  cron job at 3:30 am as that california is 3 hours off from us so all of usa hits are properly counted.
and setting up a database table Daily_Views with fields of  PAGE_ID, DATE_STAMP,UNIQUE_VIEWS, PAGE_VIEWS
and repeating for Month_Views but having DATE_STAMP being MONTH as the field

this way i can then print my report by page ID getting my event info in a joined table lookup.

so would i capture all the json data and collate to save it to mysql or is there a better way it seams with a large site of 1000's of events this would take a very long time. as id be doing on the first of each month both the prev months totals and the daily totals for that day too making all this lookup take a super long time.

if i do custom values in piwik can i just print a report with all my pages maybe and have piwik do the processing? and not have to save the info to mysql. or even make a php file that uses the api to just display my block of event pages.

some notes:
each event has an author i need to block the event page views for each page for each author ie:
John has events with id's 1,3,7
Jane has events with id's 2,4,5,6

im asking this as its very complicated to me and im not sure the best way to handle my reports of pages.

heres piwiks info as reference

thank you in advance for any code or help you may provide
  • 3
  • 2
1 Solution
Ray PaseurCommented:
The mainstream tool for this sort of thing is Google Analytics and Piwik is wa-a-a-y out of the mainstream.  Google has already answered most of the questions about analytics with built-in functionality that is widely accepted as the industry standard.  If Google Analytics cannot meet your requirements, there is a Piwik forum where questions and answers are more directly focused on Piwik.

And, of course, there is the obligatory Professional Services Team.  These teams seem to show up with every open-source project.  I wonder if they make a lot of money?

Data bases where the row numbers are counted in the thousands are considered "tiny" by today's standards, so you would not expect to see any performance issues from something like that.  This article has some SQL performance guidelines that are applicable to any data base, so if you're concerned about performance, keep these in mind.
JohnnyAuthor Commented:
@ Ray Paseur™ always good to see you field a post. i have used google analytics before a while ago, and google analytics didnt have half the features piwik has. as i said its been awhile so it may have changed. i do like most google products.

i have no way of knowing the scope of the hits that will come to the site, but im talking 1k's a DAY or a better word would be a very heavy visited site. multiple times just for the page alone with one user alone its an event driven site and it will be world wide. again no idea of how many but im expecting it to be use a lot.
with that said, i picked piwik as i like it and have used it for many years and i have not used any of the api features. it seems to do what i need but i am unsure how to use it.

my second question in this post is is it a good way to capture json data for each page(as there maybe thousands for that too) info and process it. i can see this being very slow, and seams to not be efficient at all.

i thought of just making a ip views counter and hits tally for each page, and running my reports from them. but theres still isp's out there sharing ip's. so i thought ok there cookies or sessions, and said well piwik does this and counts it for me why cant i use its info. instead of reinventing the wheel, then i ran into how do i do this the right way. hence the post.

sorry you dont like piwik, and favor GA(not saying it's bad again have not looked at it in several years).

im open to other ways to do this but a global answer is not what im looking for, i dont mind researching farther but i need at least a direction. and outline of how to handle this.
pasific to my needs not use GA and speed up your mysql. im looking for with ga you can do blah blah..

thanks again for your reply you always do an angel id never of thought of and im happy for the mysql link too, something i would not of again thought of.
Ray PaseurCommented:
OK, here are some thoughts about the parts I understand.

JSON is more efficient than XML because it does not need the ending tags, so it provides a more compact data transport mechanism.  In PHP we process JSON data by using JSON_decode() to create an object instance of StdClass.  Then you can use iterators to access the parts of the object.  So if Piwik can give you JSON data, you've got a good thing.

When you store JSON data in a data base table, there are some considerations.  First question would be, "What parts do I want to search / sort?"  These would become separate columns in the table definition, and you would decode the JSON string to isolate these data elements.  You might consider storing the entire JSON string, as well.  Storage is cheap and if you have the original JSON strings you may find that you can do historical analysis with the data.

Data base performance will not be an issue, if "talking 1k's a DAY or a better word would be a very heavy visited site" because there are 86,400 seconds in a day.  If you figure that clients only visit the site half of the day, you've still got 40,000+ seconds to respond to the requests.  Almost any web server, even a shared server, can handle request arrival rates like that.  And once you're getting thousands of hits a day, you will be able to attract investors who will help you buy faster servers :-)

INSERT queries are typically lightning-fast, and these will probably be your most common query.  Complex SELECT queries are where data base optimization matters, but you won't be doing those in real time.  If you find that your SELECT queries are too slow, you can always use EXPLAIN SELECT to see what the SQL engine is doing with the query.

If you collect the data and store it in the way you described above with Daily_Views, you're probably on firm ground.  I would start with that and consider whether I wanted to "prune" the tables as they grow.  You do not really need a separate table for monthly views because that information is inherent in the Daily_Views - you would just use a separate query spanning the days, weeks, months, etc.

Coordinating page ids with author names is probably just a matter of a JOIN query.

If you're reading this and you're still concerned about query performance, you might want to set up a simple test case, generating a thousand-element JSON string, decoding it,and loading it into your data base table.  A mockup like that will allow you to put timers at the front and back to measure the elapsed time.  PHP has the microtime() function that will give you the accuracy of measurement you need.  The test case will probably put you at ease about the query performance.

HTH, ~Ray
JohnnyAuthor Commented:
ok heres what i came up with: for now..ill revisit this later as it seems to be much more envolved then i thought and its going to require much more research.

 Views: page has a uniquie page view (first time viewed)
 Hits: Page has a tally of all views recuring and first time viewed
 loged in user: registered and we have there info on file
 blind ip: we have no record of users ip or is NOT loged in
 check if user is logged in
       if logged in see if user is in database as a view to the page. if they have not add to database as
             user_id,ip,mobile(android/apple/etc)or pc,page,time/date stamp,flag first view
       if user has visited page before add record but change flag first view to recurring flag
 if not logged in user then ad ip as user with blind note as user id of 0 (as there is no 0)
 tally days totals for any date and month of ie january 2014, by searching time/date field. hits will be tally of first view and recurring.
 views(unique) will be with first view flag only

i understand ips can be shared and i also understand its not the best way but it will get me up and running and have at least a min count correct. for hits and unique visits.

@ Ray Paseur™ your nifty explanations always floor me. i seriously wish i knew a fraction of anything you know, i struggle some times. and have to research a lot. thank you for clearing up the json matter and mysql matter of heavy hits to the pages. i will look deeper into GA and piwik(docs are very global) and see if i can find a ga example or guide for all this..and see if i can refine my views/hits better. down the road, i added this ot the to-do lists.

im going to accept your comment because it answered my question of the BEST way to handle it, ill refine my question after i research some more if i need too.

thank you as always
Ray PaseurCommented:
Thanks for the points and thanks for using EE, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now