Solved

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

Posted on 2014-01-10
5
812 Views
Last Modified: 2016-02-24
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.
example:
my_doamin/analytics/index.php?module=API&method=Actions.getPageUrl&pageUrl=/single_event_page.php?id=64&idSite=1&period=range&date=2013-12-01,2014-01-01&format=xml&token_auth=xxx

returns
<label>/single_event_page.php?id=64</label>
<nb_visits>1</nb_visits>
<nb_hits>2</nb_hits>

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
http://developer.piwik.org/api-reference/reporting-api
http://developer.piwik.org/api-reference/PHP-Piwik-Tracker
http://developer.piwik.org/

thank you in advance for any code or help you may provide
Johnny
0
Comment
Question by:Johnny
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39770923
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.  
http://forum.piwik.org/

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?
http://piwik.org/consulting/

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.
http://www.experts-exchange.com/Database/MySQL/A_1250-3-Ways-to-Speed-Up-MySQL.html
0
 

Author Comment

by:Johnny
ID: 39771065
@ 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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39771206
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
0
 

Author Comment

by:Johnny
ID: 39771459
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.

------------------------------
define:
 
 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
 
 
 
 tracking:
 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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39771502
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Importing and exporting data Magento 1.x ? 4 37
Router for PHP reqeusts 12 32
Mail Not Sent 6 42
Instering to MySQL table 5 19
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now