Piwik is working great for us, both for static pages and for tracking JavaScript based interactive usage.
One aspect I didn’t manage to figure out yet is the following: We have a form submitting a POST request which leads to a file download. I attached a call to Piwik’s trackPageView() function to the form’s onsubmit handler to track those user actions.
Looking at the script’s execution and the network access in the browser (Chrome Web Inspector), it seems that Piwik’s tracking function is called all right but the browser for some reason cancels the request to the Piwik server before it is finished which prevents the tracking from happening.
The only theory I could come up with for this is that the browser sees the form submission and prepares going to a new web page by closing the active connections. Once the form reply comes in, it keeps the old page alive nonetheless.
As I am not familiar with browser internals, I am not sure whether this model is correct and how I could work around this issue.
I used that example to gather some more information (which, unfortunately doesn’t really make me understand the situation better).
when clicking the Download button on that page, piwik tracking should happen and a download should start
the download works fine
the tracking works rarely: I find (using a network monitor) that a connection to the is established quite frequently the first time I am submitting the form, but it hardly ever (but not never) seems to work on subsequent form submissions
I find that my browser (Chrome 15 on Ubuntu), marks all connections to piwik after a form submission as ‘(canceled)’ in the Status column of the Network inspector, even those where the connection seems to work fine
looking at the JavaScript console in Chrome, I see messages »Uncaught Error: Too much time spent in unload handler« with a stack trace of (getTimeOverride in v8/DateExtension; B in piwik.js:13) in these situations
seeing v8 mentioned there, I tried the same setup in current Firefox, where I do see connections to the piwik server for each click of the button (but Firebug’s Net display only seems to display the first of those with an indeterminate progress indicator)
I don’t fully understand the issue after this, but it suggests, Chrome/V8 may play a role in this. (It’s not really clear to me what exactly happens to those connections to piwik.php in any case.)
try to add “return true;” in your post handler after the piwik calls. Maybe
otherwise solution is to wrap the function inside a setTimeout javascript call, such as:
onsubmit="setTimeout('trackPiwik()', 500)"
Does it work?
if so, I would love to create a faq or edit the doc to clarify how to do this (track any FORM submit, note: you could also automatically track with custom variables what was submitted!)
thanks for your ideas. The second one seems to do the trick! (The first one gives the same result I had before.)
Adding hints like these to the FAQ sounds like a great idea to me. Is there any good reason for browsers to behave this way or is it just a lucky observation? Could it be that after that 0,5 second break the browser is in the same state it is in immediately after the form submission, which could cause the tracking to fail?