{"id":2196,"date":"2014-04-08T16:22:58","date_gmt":"2014-04-08T16:22:58","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=2196"},"modified":"2014-04-08T16:30:03","modified_gmt":"2014-04-08T16:30:03","slug":"capturing-print-page","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/capturing-print-page\/","title":{"rendered":"PrintAlytics &#8211; Capturing the Print Page action in all browsers"},"content":{"rendered":"<h3>Quick History<\/h3>\n<p>A couple years ago, one of my clients recently had the need to keep track of pages that were printed by the staff for back-office type operations. They were using Internet Explorer exclusively, so I felt comfortable using IE&#8217;s proprietary <em>onafterprint<\/em> event.<\/p>\n<p>Within this handler of this event, I was using AJAX to hit a server-side script that kept track of the number of times a given page was printed. To be accurate, this event was also fired after a page was previewed for printing.<\/p>\n<p>I thought that kinda strange, since it was a JavaScript event fired by the browser, it should have had the fined-grained control to tell us when the page actually printed (as opposed to simply being previewed).<\/p>\n<p>Anyway, this solution worked perfectly, until my client decided to explore other browsers. One day I get the email &#8211; &#8220;why doesn&#8217;t our print tracking work anymore?&#8221;.<\/p>\n<h3>Options for modern browsers<\/h3>\n<p>After digging into it for a few minutes, I came to the conclusion that they were no longer using Internet Explorer exclusively.<\/p>\n<p>After a quick Google search, I found that the <em>window.matchMedia()<\/em> function would quickly solve this issue. The only problem is, it isn&#8217;t fully supported by legacy browsers.<\/p>\n<p>I started composing an email to my client to the effect of, &#8220;any chance you guys can start using the latest Chrome exclusively?&#8221;. \u00a0Then, it hit me, instead of putting the workload on my client, there was a simple solution that has been supported by all browsers since the dawn of time (well, close enough).<\/p>\n<h3>CSS Background Images<\/h3>\n<p>I could drop a background image style inside a print-only CSS block. But, instead of supplying a background image, I&#8217;d use a server-side script and supply any necessary tracking options in the query string.<\/p>\n<pre class=\"prettyprint\"><code>&lt;style type=\"text\/css\"&gt;\r\n  @media print { \r\n    .order-date { \r\n      background-image: url(\/updatePrinted.php?page_id=x); \r\n    } \r\n  }\r\n&lt;\/style&gt;<\/code><\/pre>\n<p>The browser certainly doesn&#8217;t like the content-type response, but it does the job. I suppose I could offer up a content-type: image\/gif header and a very small image as the response, but it&#8217;s not really necessary to get the job done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Capturing visitor print events has applications, whether it be for analytics or to slap someone on the wrist for printing too many pages! I recently found a solid cross-browser way to do it.<\/p>\n","protected":false},"author":1,"featured_media":2210,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[12],"tags":[87],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2196"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=2196"}],"version-history":[{"count":4,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2196\/revisions"}],"predecessor-version":[{"id":2212,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/2196\/revisions\/2212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/2210"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=2196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=2196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=2196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}