tags:

views:

318

answers:

7

At my work, we're interested in tracking how often people print our web pages. (If a lot of people are doing it, we'll probably put more focus on making the print view nicer.)

Obviously if we put a "Print" button on the page, we can log when people click on that, but if users just use the "File" menu and choose "Print," we have no way of knowing if they did that.

Is there some clever way to use print-only CSS to cause the browser to issue a web request only when printing? My experiments seem to say no, but this seems like it should be possible.

Edit: A reasonable answer is offered below that works in FF; I'm offering a 100 point bounty for a solution that works on IE as well.

+2  A: 

Maybe you could add a background image to the print.css file and link that background image to a file on your server which does the logging.

For example:

body {background-image:url(http://www.example.com/printlogger.aspx);}

I'm not sure if that works, just a thought

Update: I just tried the above. It does increment the counter if you do a print preview. However it doesn't when update the counter when printing a page (even with print background images turned off). Another option might be to use the CSS content property.

UPDATE II You can use the content property, works in Firefox, not in IE8. I haven't tested other browsers:

body
{
    content:url(http://www.example.com/Count.aspx);
}
jao
Don't know whether that works, but definitely worth trying. Without the image, it seems too likely that the browser would preemptively download the print.css file.
harpo
most browsers don't print the background images by default, so I'm not sure they will load the background-image
Gareth Davis
+1  A: 

Insert an element in your HTML source like:

<div id="print_tracker"></div>

In your screen CSS:

#print_tracker { display: none }

(That's not really needed unless you have some default styles for divs that causes it to have a size.)

In your print CSS:

#print_css { display: block; height: 1px; background-image: url(clear.gif); }

Then just track the hits to that image.

Tyson
That doesn't appear to work, not least because most browsers are configured not to print background images by default.
Dan Fabulich
you can modify the approach to this... put a `<div id="ptracker"><ul><li>blah</li></ul></div>` in your screen css, set display:none, and in your print css, change the bullet used on the list item... hacky....
Roy Rico
+2  A: 

There is no accurate way to see when a page is being printed (and some may consider it privacy invasion). The suggestions for having a separate print CSS do work, but they can create a lot of false alarms - Print Preview, prefetching, and mirroring tools (even wget) would request the CSS file too, even if they are not going to print anything.

grawity
+1  A: 

You could check that your printing CSS has loaded (you can select CSS for a particular layout using media selectors) with javascript. The javascript could then log to the server this request.

workmad3
This technique fails with IE6. It downloads media=print stylesheets wether you're printing or not.
SpliFF
A: 

You could have a "print version" page and put some server-side logging on that page, however that could be a bit annoying for all parties involved.

What else you could do to is something like this:

  1. On page load, use javascript to add a print-only stylesheet on the page which hides everything except a message saying "Please use the print link on this page".
    • Use javascript here so that users with JS disabled will still be able to print as normal. You won't be able to log them, but you won't piss them off either.
  2. Put a link on the page which, when clicked, logs the event via AJAX, removes that stylesheet and then calls window.print()
nickf
That's a terrible idea. You're basically forcing them to print a empty page first.
SpliFF
at least they'll learn pretty quick..!
nickf
+2  A: 

To build on responses from Tyson and Jao, you can try this approach to get around the issue of background images not being displayed/printed by default. instead of background image, use a bullet image...

Insert an element in your HTML source like:

<div id="print_tracker"><ul><li>&nbsp;</li></ul></div>

In your screen CSS:

@media screen {
    #print_tracker { display: none; }
}
@media print {
    #print_tracker { display: block; }
    #print_tracker UL { list-style-image: url(/images/printtracker.gif); }
}

Then track the hits to that image. Hacky... i know

Roy Rico
Winner! This works on IE FF and Safari. It does fire on print preview, and that is acceptable.
Dan Fabulich
Glad this worked for you, Dan.
Roy Rico
The server side catch is good, but better would be client side, so we can send data to event based analytics software. I've been trying to get the client to notice a change and haven't found anything that works for more than IE. It SEEMS like if we can change the style of an object we should be able to change something that JS could notice, and that could function as our 'onafterprint' event. I've tried an onload and doing something that would change the window size temporarily so I could listen to the 'resize' event.So far, no dice. Not sure it can be done. Any ideas?
Simple As Could Be
Sorry, @Kid A, i can't think of anything that would work
Roy Rico
+1  A: 

For IE, explore use the print - related events of the Document body.

window.onbeforeprint - Fires just prior to the page being printed window.onafterprint - Fires immediately after the page to be printed out has been loaded into printer queue/cache.

$(function() {

    //Check to see if browser supports onbeforeprint (IE6, IE7 and IE8)
    if (window.onbeforeprint !== undefined) {
       window.onbeforeprint = TrackPrint;

    }

}); function TrackPrint(){
$.get("http://www.example.com/Count.aspx");}
brian chandley
It also fires on print preview, though that may acceptable
SpliFF