views:

221

answers:

6

Is there a way to track if a user prints a web page?

Thanks, Graham

+2  A: 

For cross browser support, only if you offer to control printing, as in, adding a "display in printable format" button, which will at the minimum, show you the intent to print the page. However, you will not be able to stop a user from printing by conventional means or detect when the page is being printed.

George
+3  A: 

You can achieve this in IE only.

IE supports two client-side events that you can handle: onbeforeprint and onafterprint.

You can add an AJAX request in either event that will call a page server-side to, say, increment a PagePrinted counter in a database.

Tim S. Van Haren
+5  A: 

In Internet Explorer 5+, you can use the onafterprint event, to launch an AJAX request every time the page is printed. If you are using jQuery, you could do the following:

window.onafterprint = function()
{
    $.post("increment_print_counter.php");
};

Then maybe you can use some statistics to estimate the number of times it was printed from the other browsers!

Daniel Vassallo
This could also be used in combination with GoogleAnalytics. Just replacing the $.post line with the appropriate bit of the GA code. This could in theory give you better context about who is printing and so on. :)
Amadiere
+3  A: 

I don't know when the browsers would fetch the CSS when you specify them with media="print"

<link rel="stylesheet" type"text/css"
     href="print.css" media="print">

If they fetch it only when the user tries to print the document than you might try a server side trick to track it.

Give it a try.

Tahir Akhtar
I thought about that, but I checked with Fiddler, and it looks like IE and Chrome fetch them immediately.
Daniel Vassallo
Interesting idea, but I wonder how caching would interact with that.
Benjamin Oakes
+1 Very cool idea.
George
It's cool, but unfortunately it won't work. You can analyse the traffic through Fiddler. Wikipedia articles use `media=print` stylesheets, if you'd like to test it.
Daniel Vassallo
+10  A: 

@Tahir Akhtar had the idea of using a print stylesheet. According to the comments, that doesn't work because they are fetched immediately.

How about taking this further: Use a print stylesheet and define a style in it that makes the browser fetch a certain resource when it's rendered. The resource would return a transparent image.

As for the choice of CSS property, background-image is out, because they are usually not rendered by the browser. My thought would be a list-style-image or a cursor.

I think list-style-image on an absolutely positioned, transparent <ul> might do the trick without visually altering anything (I would not dare to position the element outside the page because of possible browser optimizations cutting it away). Every access to that resource should come from printing a page, or a print preview.

I can't test it right now, maybe later. If anybody tries it, I'd be interested to hear whether it works.

Pekka
That looks promising! Excellent idea. +1
Daniel Vassallo
+1  A: 

You could also add a big, obvious “Print” button to the page, that fires window.print, and does an AJAX request if onafterprint isn’t available.

It wouldn’t give you any stats about users who are just hitting Ctrl+P (and aren’t on IE), of course, but it gives you something (if you don’t mind sticking a “Print” button on the page).

Paul D. Waite