It can sometimes be important to know if your content has been printed. The usual way to track this (in Google Analytics and other web analytics tools) is to use a “Print button” with an onclick event that sends the data to the tool.
However, not every website has a print button, and there are many other ways to print a page:
- Printing from the Browser Menu
- Right Click and choose Print from the Right Click Menu
- Ctrl+P (Windows) or Cmd+P (Mac)
In this blog post I show you how to track printing, and what method used to print the page.
About the Script used for Tracking Printing in Google Analytics
The script in this blog post is based on a script made by TJ VanToll, and is described in his blog post called Detecting Print Requests with Javascript.
The script and solution is a little bit “hacky”, and that is described below.
Opera Browser and Print Tracking
As he describe in his blog post, you have to use different methods for different browsers to detect printing. So what I have done is to take his script, and added tracking of printing method to my script. The reason for this is not only curiosity, but also that printing from the Opera browser can’t be detected using only javascript (perhaps this will change when Opera starts to use Webkit as the engine for the browser).
To get some understanding of printing from a Opera browser, you will have to rely on Print Button tracking and tracking of Ctrl/Cmd+P. Printing from Browser Menu or Right Click + Print can’t be tracked from a Opera browser.
Chrome Browser and Print Tracking
Chrome is a different “beast” as well. It will in most of the time fire several events, and only the first event will show the correct Print Method. Because of this I only allow the script to run once.
General things about this script for Print Tracking
Other things to be aware of is that most browsers based on my testing (Internet Explorer, Chrome and Firefox) will tell you that the page was printed even if you cancel printing, while in Safari you will have to Print the Page or choose Print Preview for the script to “kick off”.
If you right click but don’t print, and instead chooses to print from the Browser Menu, this will probably be tracked as Right Mouse Button Printing.
Script for tracking Print in Google Analytics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<script type="text/javascript"> // This script for tracking Printed Pages & Print Method in Google Analytics is provided as it is, and was put together by Eivind Savio April 2013. You need jQuery for this script to work. Happy tracking! (function () { var runOnce; var afterPrint = function() { if (!runOnce) { // Because of Chrome we can only allow the code to run once. runOnce = true; var printData = $('html').attr('printType'); var mouseButton = $('html').attr('mouseBtn'); if (printData === undefined && mouseButton === 'Right') { // Print activated using Right Mouse Button printData = 'Right Mouse Button'; } else if (printData === undefined) { // Print (probably) activated using Browser Menu printData = 'Browser Menu'; } _gaq.push(['_trackEvent','Page Printed', printData, window.location.pathname]); // Send Print Data to Google Analytics $('html').removeAttr('printType'); // Clear the attribute, if not printing from the menu can be tracked wrongly. $('html').removeAttr('mouseBtn'); // Clear the attribute, if not printing from the menu can be tracked wrongly. }; }; if (window.matchMedia) { // Track printing from browsers using the Webkit engine var mediaQueryList = window.matchMedia('print'); mediaQueryList.addListener(function(mql) { if (!mql.matches) { afterPrint(); } }); } window.onafterprint = afterPrint; // Internet Explorer $(document).keydown(function(allBrowsers){ // Track printing using Ctrl/Cmd+P. if (allBrowsers.keyCode==80 && (allBrowsers.ctrlKey || allBrowsers.metaKey)) { $('html').attr('printType', 'Ctrl/Cmd+P'); if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. afterPrint(); } } }); // Detect Right Mouse Button Click $('html').mousedown(function(e) { if( e.which == 3 ) { $('html').attr('mouseBtn', 'Right'); } }); // Print activated using window.print() ex. from a button. If you don't have a print button on your page, you can remove this code. $('.printMe').click(function(){ // Change this to fit your Class or ID. $('html').attr('printType', 'Print Button'); // Track printing using Print Button if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. afterPrint(); } window.print(); // If window.print() is activated in another script, you can remove it from this tracking script. }); }()); </script> |
Some final words
This script relays on jQuery, so you will need that installed on your site to get this script to work.
If you think the code could be improved, feel free to chime in (I’m not a programmer).
The code in this post was written in 2013, and today I would have done everything in Google Tag Manager. LunaMetrics has written the blog post Tracking Page Prints with Google Tag Manager that will show you how to do this.
Be the first to comment on "How to Track Printed Pages in Google Analytics"