Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
Java Tracking Client V1 - PiwikTracker.java #2172
I transformed PiwikTracker.php to Piwiktracker.java and wanted to share the code for further improvement/fixes. There is still stuff missing but it works for me so far.
To track a (JSP-)page I use the following code:
<% PiwikTracker pt = new PiwikTracker(1, "http://localhost/piwik", request);
I am still a newbie to Piwik so I might have gotten something wrong...
Exactt, thanks for your submission. This is the first third party language we will support after PHP, good news!
The code looks very close to the PHP version which is good as well. I noticed json_encode is not implemented? this is important since Custom Variables are sent json encoded to the tracker.
You can safely remove all XDEBUG related code.
Also, maybe you haven't used the latest 1.2 PiwikTracker to do the conversion. There are a few fixes and now the setIp and set date of visit work with token_auth, so they can be passed in getRequest() method. Implementation has slightly changed (ie. was finalized) in 1.2 around Custom Variables (eg. the parameter is called cvar not _cvar)
for readability, you can remove the PHP functions not implemented
Thanks for this, Java users from the piwik community will definitely appreciate. We can also let you know if there are further updates to PiwikTracker.php if you'd like to maintain
hi guys, thx for your comments!
@vipsoft: I removed the license part because it contained too many URLs.
@matt: I will investigate the changes and upload a new version in the next couple of days.
here my refined calling code:
<% String pageTitle = "Tracker Test";
exactt, thanks for the updates, it looks really cool.
Have you tested all the functions? Are you confident it all works?
If so, and because you are keen to maintain the lib, I propose that we commit this class to the SVN and link it from the Tracking API page (like we do for the PHP version).
Let me know your thoughts, great work anyway! Now on to Python, ruby :)
The changes involve:
So far my few points. But without the start from exactt i would right now fight with the js api :) so thanks for the commit!
mheuser, this sounds great. Please post a comment here when you have done work in your github, and when you are done please post the Zip here.
Ideally, we will include the Java API in the Piwik SVN and then give you access to SVN so you can maintain it (even though there shouldn't be much work, we might add new features in the API later).
Thanks for the effort! They have been a very good starting point. Unfortunately I do not find the API well documented, i.e. which parameters are used with which data type so I was very happy to find this class.
We have also used the class in a project now but had to adjust them to our needs. The PiwikTracker.java attached here requires JRE 6 for cookie handling which is not feasible for all project setups - and has not been for ours. Furthermore it uses a synchronous approach which might block further processing if the PIWIK server is unavailable.
We have gone for an asynchronous approach and a handling w/o cookies (we are identifying unique visitors differently). I would provide a diff, but we are using PiwikTracker as POJO without any logic in it except building the URL.
We have found some small issues and here are changes that need to be done to URL building in PiwikTracker#getRequest(int) regardless of the project setup:
Parameter _refts requires epoch time in seconds, a Date was given. Java furthermore counts epoch times in milliseconds.
The parameter forcedDateTime needs to be encoded in a way PHP's strtotime() can handle. We are using the following formatter and had no issues with it:
As the #empty(int) is a wrong method (returns true if value != 0), the resolution was not transmitted correctly. We have changed the line to:
The "biggest" change affected custom variables. It fails with an IndexOutOfBoundsException because the position is given although there are no elements in the List. So we are "adding" the variables to the list rather than "putting" them in. I have no clue if the order of custom variables is that important. However as we are always calling the method in the same order, the order of the variables remain the same anyway. The second change affected the json_encode which should JSONObject instead JSONArray:
I hear your complain about lack of documentation :) I think the best way to start is to look at the PHP Tracker, the code should be fairly easy to understand: https://github.com/piwik/piwik/blob/master/libs/PiwikTracker/PiwikTracker.php
The order is not important but the value array must be indexed by the custom variable slot.
Hope it helps!
(In ) Fixes #2168
Refs #2222 Accurate Paypal tracking (or any other third party "after the fact" Goal conversion): implementation is now done, we must write some kind of guide and test
As well as the previous comment, the Java client must be updated with the new functions:
All this code is pretty trivial hopefully you can reach an agreement soon for the Piwik Java client and we can start advertising it and commit it to SVN!
Thanks Java devs
Replying to JulienM:
The pom.xml change is trivial. The problem of the dependency is that I don't find a proper one.
The getVisitorId function is also trivial to add.
Is there a way to get access to svn or the get a path where I can upload my source? The attachments will get very confusing if we do the source providing in that way.
mfochler, thanks for your update in trunk!! Excellent class for Java developers.
The Tracking API is now stable. The new functions are:
AS well as these new functions, the Tracking API functions added in 1.4 haven't been ported in the Java client:
Finally, the following functions have been updated ( a new parameter added, for Custom Variables for scope "page" support)
The code for these functions is fairly straightforward (very small code) and don't introduce any new concept so it is mostly reusing existing code. The PHP client is at: https://github.com/piwik/piwik/blob/master/libs/PiwikTracker/PiwikTracker.php and the auto generated doc at: http://qa.piwik.org:8080/phpdocs/PiwikTracker/PiwikTracker.html
Thank you for your useful help so far! Hoping that someone can submit a patch which mfochler can then commit to trunk :-)
I will create a patch in near future.
In the new code of the piwikTracker the param "_ref" is used for the attributionInfo element 4.
The other changes can be made easily.
I hope I can make the changes at the weekend.
(!empty($this->forcedDatetime) ? '&cdt=' . urlencode($this->forcedDatetime) : '') .
Hi! I have a problem with sending "custom variables" via "org.piwik.SimplePiwikTracker.java".
If we try to send more than one custom variable, only one custom variable of them will be shown on Piwik application.
The problem is that SimplePiwikTracker seems not to send custom variables with startindex 0, and this is wrong.
(Full example URL of Piwik Tracking -> http://piwik.local/piwik.php?idsite=111&rec=1&apiv=1&url=http%3A%2F%2Flocalhost%3A8080%2Finm%2Fadesso%2F&_id=BD2F6502EA8ABE3D0FF7F9DC749A3114&cip=127.0.0.1&token_auth=34f6ee3769a9e5f6239573b6637b6336&pdf=true=false&res=400x700&cookie=false&_cvar=%5B%5B%22Modelname%22%2C%22BROWSER+Firefox+8.0%22%5D%2C%5B%22Prefered+Markup%22%2C%22html%22%5D%5D&rand=0.20301514584441116&action_name=http%3A%2F%2Flocalhost%3A8080%2Finm%2Fadesso%2F )
So I guess that Piwik application doesn't parse all custom variables correctly.
I tried to fix this issue.
Class : org.piwik.SimplePiwikTracker.java
Can you apply the fix, and release the update version for us ?
Thanks a lot for help!
I improved the existing official PIWIK JAVA connector:
Hi I've been using piwik for awhile but just recently tried using this Java code. Things are just about working, but there is one odd behavior. I'm not sure if what I'm seeing is a bug or user-error.
My problem is that Piwik isn't recognizing these two goals as being achieved by the same visitor. In the database, specifically piwik_log_conversion, the two goals are logged with different idvisit and idvisitor values. Also, for Goal #2, which is recoded via Java, the location_continent is 'unk' and location_country is 'xx' So somewhere the visitor tracking is breaking down.
I looked into the code, and followed along with a debugging proxy server. The Java code is forwarding the "piwik_visitor" cookie. But the actual cookies I have in my browser are _pk_id.6.1fff and _pk_ses.6.1fff. I modified the Java code so that all cookies are forwarded, and can confirm from the debugging proxy server that they are being passed along. I was careful to ensure the "cookie" URL parameter was set to "true." Even with these changes, however, piwik is still not tracking the goals as having been completed by the same visitor.
My server is up-to-date, at v1.7.1.
This code is called a 'plug-in' but I'm assuming I don't need to actually install a plug-in on the server. All I need to use is this Java code which makes a GET request to piwik.php .. correct?
Is this a bug? User error? Any suggestions on how to address it?
Yes correct.. We should really put up a documentation for the JAVA client in http://piwik.org/docs/tracking-api - if anyone would like to help with this it would be great.
Also to debug the Piwik.php request to find out more about th elogic piwik is doing, you can see: http://piwik.org/docs/tracking-api/reference/#toc-debugging-the-tracking-api-requests
Matt, you pointed me in the right direction. I think I have found a bug.
When the SimplePiwikTracker object is created, it sets the visitorID to a random string:
This is never changed. I wrote up some quick code to examine the cookies and extract the visitorId:
After invoking this function, things seem to work.
Someone who is more familiar with the code should definitely review this, I'm not at all familiar with piwik internals. But the code as written seems to make two key mistakes:
I'm using @Version 1.0.4. There is a comment "cookiesupport removed cause was not used" Perhaps this functionality used to be present but was removed by mistake?
In trunk, there is a constructor which takes a HttpServletRequest to look for cookies. (https://github.com/piwik/piwik/blob/master/libs/PiwikTracker/java/src/main/java/org/piwik/SimplePiwikTracker.java?desc=1#L105)
This constructor won't actually help you. It only looks for the piwik_visitor cookie.
With the PHP PiwikTracker, the functionality you are looking for is getVisitorId : https://github.com/piwik/piwik/blob/master/libs/PiwikTracker/PiwikTracker.php?desc=1#L570.
This method looks for cookies which contain the visitor id.
Have a look at the getVisitorId in the JAVA PiwikTracker : https://github.com/piwik/piwik/blob/master/libs/PiwikTracker/java/src/main/java/org/piwik/SimplePiwikTracker.java?desc=1#L115.
It doesn't look for cookies.
This method should be rewritten to provide the same level of functionality as the PHP PiwikTracker. Otherwise, users will be confused.
To further normalize both PiwikTrackers, there should also be only one JAVA constructor (which doesn't look for the 'piwik_visitor' cookie).
I compared the Java PiwikTracker class against the PHP version and see many differences. Crucially, the Java class uses a different cookie name, and lacks logic for extracting the visitor ID from the cookie. It appears this logic was simply not translated correctly in the initial check-in, and has not been corrected since. In short, visitor tracking seems totally broken.
The Java code lacks the convenient "doTrackPageView" method which is 1) Included in the PHP version 2) useful and 3) described in the original bug request.
The Java code adds an interface for the PiwikTracker class, but it is unused. I don't see a reason for it to exist.
I haven't tested this but strongly suspect the code will not play nicely behind a proxy server like the Apache HTTPd server. I suspect it will record the IP address of the proxy server not the end-user. It is fairly common for Java services to be implemented on a different server, and made available to end-users via proxy. That is certainly how I do things. To address this I would replace calls like getRemoteAddr(), which are not proxy-aware, with a proxy-aware equivalent.
I'm not sure what the overall process for this group is. This particular bug report has an empty "Owned By" field. The original submitter and author is exactt, but he hasn't posted here in over a year. So, I'm hesitant to spend time making fixes and submitting them here. But it feels to me that the code should be brought into line with the PHP version. I could do this work, but don't want to step on anyone's toes, and don't want to do the work if it won't get reviewed / integrated. FYI- I am a solid Java developer, over 10 years now. My PHP is not strong, but I can make sense of the existing code.
So ... opinions on next steps?
thivnor, thanks for your message.
The existing code probably works for some use cases but it was created a while back, and we have regularly updated the PHP client since. However the php client is now stable and will stay so.. it is therefore a good time to revisit and finalize the Java client!
Would you be interested to work on it and submit a 1.0 release synced with PHP client in terms of API?
Ideally you would submit a patch, see: http://piwik.org/participate/development-process/#toc-how-to-submit-a-patch
If you are interested to maintain it, we could also give you SVN access, see the link for explanation of our dev process.
It would be great to have this code updated in trunk. Also it would be nice if we could auto generate the Java API doc similarly to our automated PHP doc.
I propose that you create a new ticket called "Java Tracking client 1.0" as this ticket is too long, and we can start fresh
I commited the patch mentioned above to the trunk. I am also interested in creating a better Java implementation of the PIWIK-Tracker to fix those issues you mentioned before. (tracking behind a proxy, ..)
Please share the link to the new ticket here when you create one.