Editor's note:
Thanks for visiting GA Tip of the Day. Please see current posts on the Cardinal Path blog.

Generate Print Events with onafterprint JavaScript Handler

For visitors using Firefox or Internet Explorer, you can combine the built-in onafterprint JavaScript handler with Google Analytics _trackEvent code as below to record each time a Web page in printed:

<body onafterprint="_gaq.push(['_trackEvent','page','print', document.location.pathname+document.location.search])">

The specific parameters that you pass to _trackEvent are your decision, but you’d certainly want to record the URL of the page printed – either as the event label or possibly the event action. (In the example above, document.location.pathname is used to capture the part of the URL after the domain but before any question mark, and document.location.search is used to capture any question mark in the URL and the querystring that follows the question mark.)

Note that onafterprint is used within the <body> tag, so you’ll probably incorporate it into your page template (and not inject on a page-by-page basis).

As noted above, onafterprint is supported only in Firefox and IE, but since it’s probably safe to assume that Chrome and Safari users print pages at roughly the same rate as Firefox and IE users, you could easily apply advanced segments to calculate browser percentages and extrapolate print events for just Firefox and IE to print events for all browser types.

Please share this post:
Facebook Twitter Plusone Linkedin Email

One thought on “Generate Print Events with onafterprint JavaScript Handler

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>