Graham
Graham

Reputation: 209

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

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

Thanks, Graham

Upvotes: 20

Views: 6623

Answers (8)

Jeromy French
Jeromy French

Reputation: 12121

Yes, for most browsers there are events that can be used to track print requests.

  • IE5+ supports window.onbeforeprint and window.onafterprint
  • Chrome9+ and Safari5.1+ support window.matchMedia()

See TJ VanToll's "Detecting Print Requests with JavaScript" blog post for more information.

Upvotes: 1

tom
tom

Reputation: 19

  1. Turn off print with CSS (no display on the body tag)
  2. Have a print button on page that pops open a new print only page. You can then tie that URL to the user (provided you have some info on them)

Upvotes: 1

Pekka
Pekka

Reputation: 449525

@Tahir Akhtar had the idea of using a print stylesheet. According to the comments, that doesn't work because the stylesheet is always fetched , regardless of whether the browser currently is in print view or not.

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.

Upvotes: 20

Paul D. Waite
Paul D. Waite

Reputation: 98816

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).

Upvotes: 1

Daniel Vassallo
Daniel Vassallo

Reputation: 344351

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!

Upvotes: 8

Tahir Akhtar
Tahir Akhtar

Reputation: 11625

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.

Upvotes: 5

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

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.

Upvotes: 3

George Johnston
George Johnston

Reputation: 32258

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.

Upvotes: 2

Related Questions