Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Support Page Visibility API to detect pre rendering requests and avoid over-counting page views #2496

mattab opened this Issue · 23 comments

3 participants


The page requests happen in the browser, looking like a regular webpage view with the exception of a few JavaScript variables addressable from your sites JavaScript. Support for preloading webpages and all related assets is a feature of WebKit-based browsers released after May 2011, including Chrome 13.


I think this feature sucks but we have to deal with it anyway.

This ticket is to test with these new browsers, and make sure that Piwik doesn't trigger the pageview when pre-fetching, and then trigger the pageview when the page is actually loaded.


I think this feature doesn't suck: It's quite powerful from a users point of view.
Anyway, the solution might be to use document.webkitVisibilityState (see I guess there is something similar to deal with the Firefox prefetch mechanism (see, e.g. they send the HTTP header "X-moz: prefetch").


the problem is that these are different techniques (one client-side; one server-side).

webkit breaks the link tracker ... which sucks more imho


There is also the new Google chrome prefetching feature which can be tested in javascript:


I think this feature should be implemented quickly. Google Chrome 13 is released now and has prerendering activated by default. It very likely will fast gain a large market share, significant enough to falsify the visitor statistics.

Replying to alexwillner:

I think this feature doesn't suck: It's quite powerful from a users point of view.
In my opinion it's a great feature to improve the user experience.
Anyway, the solution might be to use document.webkitVisibilityState [...].
Another Link: Using the Page Visibility API by Google.
I guess there is something similar to deal with the Firefox prefetch mechanism [...].
According to the Web Developer's Guide to Prerendering in Chrome there is no need to deal with Firefoxs prefetch mechanism. "Prefetching is a feature that is currently implemented in Firefox [...]. When it encounters the tag, the browser fetches the top-level resource at that URL (most often an HTML page). Prefetching, however, often does not lead to user-noticeable speed-ups because most of the content is in sub-resources, such as JavaScript, CSS, and media files."


thoti, if you have some dev skills, it would be great if you could help and submit a patch for this feature :)


I'll have a look at it. ;)


I attached a patch for piwik.js.
For guys who know about prerendering and want to track it, I added the function "setCountPrerender".


thoti, thank you for the patch.

Some feedback

  • what is the exact test procedure to test this change? Including, how to test the "calls to logPageView after the prerendering state is left." ?
  • only LogPageview is managed, but probably the ecommerce interactions, goal tracking, etc. should be included in this change as well, so the code could be refactored to work with all logging functions?

Thank you for following up :)


thoti did you have any chance to look at my feedback above? Thanks!


Attachment: Page which prerenders the site called prerendered.html


Attachment: Site which gets prerendered by start.html



I used the the files attached, start.html and perendered.html, for testing. Google Chromes Task-Manager shows an extra process for each prerenderd page. You can call the Task-Manager by pressing Shift + Esc.

  1. Load start.html.
  2. Check if prerenderd.html is prerendered. a. Look in the Task-Manager for a prerendered site. a. Check that their isn't any entry in Piwiks visitor-log for prerendered.html.
  3. Click the link shown in start.html. (Must be done within 30 seconds. Otherwise Chrome drops the prerendered site and will load it again if the link is clicked.)
  4. Check Piwiks visitor-log for an entry of prerendered.html.

Also Change E-Commerce etc.

I can do it. But it could take until mid-October. Sorry.


cheers for the details on the tests!

OK for the patch on Mid-october. We will include it in Piwik 1.7.


Attachment: Patch for /trunk/js/piwik.js


I updated the code now to work with the three functions mentioned in the List of all methods available in the Tracking API > Using the Tracker Object:

  • trackGoal,
  • trackLink,
  • trackPageView. The page with the tracking API should be updated, too. The new function "setCountPrerendered" allows it to change the default behavior and also log prerendered sites.

Suggestions for improvements are always welcome. :)

(piwik_js.2.patch can be deleted, it's an duplicate of piwik_js.patch.)


Thanks, looks good to me!

Vipsoft can you please confirm the JS review?

thoti, can you confirm you have tested the code and the config setter setCountPrerendered ? Have you tested in Chrome, Safari ?
Also, what is the easiest way to reproduce the "pre rendering request" since I'd like to test it too?

Thanks for the great patch, it will be nice to fix this issue and improve Piwik accuracy.


Attachment: Patch for /trunk/js/piwik.js


I have tested it with Chrome 15, Safari 5.1, IE 9 and IE 7 (compatibility mode of IE9), with and without setCountPrerendered.

Testing is described at comment:11. In prerendered.html you might add piwikTracker.setCountPrerendered(true); before the call of trackPageView(). I couldn't upload a new version with this line included, because Trac thinks it's spam.

There's a problem with Chrome. If you load start.html and immediately click on the link to the prerendered prerendered.html, the view of prerendered.html often gets logged twice. I could reproduce the problem with test-files having no references to Piwik, so it's likely to be a bug of Chrome. Unfortunately I couldn't figure out what exactly is going wrong.


Thanks for your contribution. We'll commit for next release!

If you have other ideas of improvements in piwik feel free to grab another ticket :)


(In [5401]) fixes #2496 - thanks thoti!

  • some refactoring of thoti's patch for minification
  • implement fallback chain for browser support of page visibility API
  • update jslint, unit tests, and docs

todo: update api page

investigate missed minification



@mattab mattab added this to the 1.7 Piwik 1.7 milestone
@robocoder robocoder was assigned by mattab
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.