{"id":492,"date":"2012-03-27T16:03:20","date_gmt":"2012-03-27T07:03:20","guid":{"rendered":"http:\/\/www.santoshkori.com\/blog\/?p=492"},"modified":"2012-08-23T17:28:25","modified_gmt":"2012-08-23T08:28:25","slug":"how-to-use-google-analytics-to-track-when-people-print-your-web-pages","status":"publish","type":"post","link":"https:\/\/www.santoshkori.com\/blog\/how-to-use-google-analytics-to-track-when-people-print-your-web-pages\/","title":{"rendered":"How to Use Google Analytics to Track When People Print your Web Pages"},"content":{"rendered":"<p>Say you have a website that is printer-friendly but you are not too sure if people are actually printing your web pages. And if they are, you are interested to know how often they print pages and what is the kind of content that users are most likely to print on your site.<!--more--><\/p>\n<p>There are basically three ways to print any standard web page<\/p>\n<ol>\n<li>Experienced users may press the Ctrl + P keyboard shortcut (or Command-P on a Mac) to send the current web page to the printer.<\/li>\n<li>Some web pages have a dedicated <a>PRINT<\/a> button on the page itself.<\/li>\n<li>Other users may prefer to print web pages from the File Menu that is standard across all browsers.<\/li>\n<\/ol>\n<p>Because there are multiple ways to print the same web page, it is difficult to use JavaScript based event tracking in Google Analytics to track print usage.<\/p>\n<p>A simple workaround is that you add an invisible tracking image (like the ones used for read receipts in email) only in the printed version of a web page. Thus when a user prints a web page, through any route, that tracking image will download on his \/ her computer and you can easily track the print (and print preview) action.<\/p>\n<h2>Track Print Usage with Google Analytics<\/h2>\n<p>That was the boring theory but you can safely skip the technical  details and get right into implementing the actual tracking code.<\/p>\n<p>All  you have to do is copy-paste the following code above the closing  &lt;\/body&gt; tag in your website template. If you are on WordPress, you  can simple paste it in your footer.php file.<\/p>\n<p>Please remember to replace REPLACE_ME in the code with your actual Google Analytics Profile ID which looks something like this \u2013 UA-12345-89.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n  var ga  = &quot;REPLACE_ME&quot;;\r\n  var css = document.createElement('style');\r\n\r\n  if (css &amp;&amp; ga != &quot;REPLACE_ME&quot;) {\r\n  var gimg  = &quot;http:\/\/ctrlq.org\/urchin\/?&quot;;\r\n      gimg += &quot;id=&quot; + ga;\r\n      gimg += &quot;&amp;d=&quot; + document.location.hostname;\r\n      gimg += &quot;&amp;i=&quot; + document.location.pathname;\r\n\r\n  var cstr  = &quot;@media print {body:after&quot;;\r\n      cstr += &quot;{content:url(&quot; + gimg + &quot;)}}&quot;;\r\n  var node = document.createTextNode(cstr);\r\n\r\n  if (css.styleSheet) {\r\n      css.styleSheet.cssText = node.nodeValue;\r\n  } else {\r\n      css.appendChild(node);\r\n  }\r\n\r\n  var head = document.getElementsByTagName('head')&#x5B;0];\r\n  if (head) {\r\n      head.appendChild(css);\r\n  }\r\n }\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Once the tracking code is live, wait for a day or two as as Google Analytics may take time to process usage data. Then log in to your Google Analytics dashboard, go to Content \u2013&gt; Site Content \u2013&gt; Pages and set \/print\/ as the search filter.<\/p>\n<p>You\u2019ll get a complete list of web pages that have been printed in this duration while the Pageview column will reflect the the number of times a particular page has been printed . Set the Secondary Dimension in the report as Browser or Operating System or Country and you\u2019ll get additional details about the users who are using the Print function on your web site.<\/p>\n<p>Here\u2019s a sample print usage report generated with Google Analytics.<\/p>\n<h2>How Print Tracking Works?<\/h2>\n<p>Should you be interested in the  technical details, here they are. The JavaScript code adds the following  CSS rule to your HTML webpage while it is rendering in the browser.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;style type=&quot;text\/css&quot;&gt;\r\n  @media print {\r\n      body:after {\r\n        content:url(&quot;GOOGLE_ANALYTICS_TRACKING_IMAGE&quot;)\r\n      }\r\n  }\r\n&lt;\/style&gt;\r\n<\/pre>\n<p>This is simple print only rule that will add an invisible tracking  image to the printer-friendly version of your page (body:after). When a  user prints the page, the tracking image downloads on the user\u2019s  computer and this is registered as a page view in Google Analytics as  shown in the above report.<\/p>\n<p>The above CSS rule is only activated  when the users invokes the print or print preview command. Some PDF  writing programs also use the Print stylesheet when saving web pages to  PDFs and thus, the same tracking code will work in those cases as well.<\/p>\n<p>PS: If you have a PHP enabled web server, you can simple copy this <a href=\"http:\/\/ctrlq.org\/code\/19011-google-analytics-php\" target=\"_blank\" rel=\"noopener\">PHP file<\/a> on to your own server as index.php and\u00a0 replace ctrlq.org\/urchin in the  above JavaScript code with your own web server\u2019s address.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you have a website that is printer-friendly but you are not too sure if people are actually printing your web pages. And if they are, you are interested to know how often they print pages and what is the kind of content that users are most likely to print on your site.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17],"tags":[221,725],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-just-blogging","category-tips-n-tricks","tag-how-to-use-google-analytics-to-track-when-people-print-your-web-pages","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":6,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}