Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Google Analytics

How to Fully Track the Print Page Action with Google Analytics

July 9, 2013

Have you ever wondered if people are printing pages from your website?

Wonder no more…here is a solution to track all the methods people use to print a page on your website. We will show you how to capture this data whether they;

  • click on your pretty print button,
  • right click and choose print from the right click menu,
  • use keyboard shortcuts ‘Ctrl+P’ (Windows) or ‘Cmd+P’ (Mac),
  • or use the ‘File > Print’ menu.

A common solution is to listen for clicks onto your website’s print button. This works well if everyone used that button. However, there are several other ways that users can print a page, as detailed above. The Google Analytics tracking solution we provide below, tracks all print methods as it hooks into the browser to listen to events just prior to the print dialog appearing.

How to Track the Print Button in Google Analytics

Before we begin, it is important to understand that if a user initiates the print dialog and then cancels, this code will still track it as a print. Thus, I label it in Google Analytics as more of an intent. The tracking code below can be implemented anywhere below your Google Analytics script. Depending on if you use Google’s Classic Analytics (ga.js/dc.js) or Universal Analytics (analytics.js), you’ll want to remove the line of code that doesn’t apply. I’ve included them both in the below code to illustrate the coding for each.

[sourcecode language="js"]<script type="text/javascript">
try{
(function() {
var afterPrint = function() {
_gaq.push(['_trackEvent', 'Print Intent', document.location.pathname]); //for classic GA
ga('send', 'event', 'Print Intent', document.location.pathname); //for Universal GA
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (!mql.matches)
afterPrint();
});
}
window.onafterprint = afterPrint;
}());
} catch(e) {}
</script>[/sourcecode]

Credit for the actual print detection logic goes to TJ VanToll. Be sure to check out his great front end development blog. The code listens for either the window.onafterprint (if the browser supports it) or listens for the mediaQuery to switch to print.

Embed Script via Google Tag Manager

If you are using Google Tag Manager, simply add this as a new tag to fire on all pages. I recommend loading it after the page has loaded (by adding a rule of ‘event’ equals ‘gtm.dom’). Below are the settings in Google Tag Manager (GTM):

ga-track-print-gtm-tag

The Result

Once you have this Google Analytics event tracking code in place, you should start to see the print activity data populate in the Event reports. Below is a sample output after you click into the ‘Print Intent’ event category. For example, you can see below that 1,964 people had the intent to print the about us page.Print Tracking in GA Report

Interested in how to track other outbound/external link activity? Check out this related post on how to track downloads and other outbound tracking in Google Analytics.

  • Thanks for the article, good read.

    Ain’t you missing the script tags in the gtm html tag?

    • @eduardocereto:disqus Thanks for the comment and for catching that in the screenshot! Updated and fixed it.

  • @sam_bonner:disqus Thanks for the contribution! This looks like a solid fix to that bug.

    • Philipp Schneider

      thanks a lot. this also helps if you’re tracking cross-domain iframe-embedded content. with this fix you will only get one hit, and not a second for the iframe 🙂

  • Angela Lindsey

    Tested GTM snippet and no GA events being tracked. Ideas?

    • Angela Lindsey

      try{

      (function() {

      var afterPrint = function() {

      _gaq.push([‘_trackEvent’, ‘Print Intent’, document.location.pathname]); //for classic GA

      };

      if (window.matchMedia) {

      var mediaQueryList = window.matchMedia(‘print’);

      var mqlListenerCount = 0;

      mediaQueryList.addListener(function(mql) {

      if (!mql.matches && mqlListenerCount < 1){

      afterPrint();

      mqlListenerCount++;

      }

      });

      }

      window.onafterprint = afterPrint;

      }());

      } catch(e) {}

      • @angelalindsey:disqus You’ll need to provide a URL so that we can see what else might be going on.

      • Angela Lindsey

        umbbigbash.org

        • @angelalindsey:disqus I tested this on your site and it is tracking the event on print. You should be able to see this in the real-time events data OR wait up to 48 hours to see the data in GA.

          • Angela Lindsey

            Could it be my GA isn’t configured to look for this event? I too was thinking it was set up correctly but I initiated it on Monday of this week and still nothing is showing up Real Time or Content/Event.

          • @angelalindsey:disqus I found the issue. The print event is being sent to UA-XXXXX-X instead of to your web property. This is because you are using the built-in GA tag template in GTM. To fix, you either need to move your base GA tag out of the built in tag template OR modify the script to add the _setAccount and optionally _setDomainName above the _gaq call.

          • Angela Lindsey

            That was EXACTLY IT! Thank you.

  • Becky Boyland

    How much of the code does this snippet replace?

  • The Google Analytic tracking solution we provide below, tracks all
    print methods as it hooks into the browser to listen to events just
    prior to the print dialog appearing. thanks for the great post – Google Analytic….

  • Great source of data post for the tracking print page action for Google Analytic, thanks for the help…

  • Rajdeep Jaiswal

    I want to know print time(how much time it will take to print the page after clicking print button. [after clicking print , print page will be open in new tab])

    • Hello Rajdeep,

      Unfortunately, there is no real way of knowing whether or not the print even occurred. When the user prints the page, there’s an event we’re binding to, however after that point, the print modal pops up and we no longer have any access to the user engagement at that point since that’s an operating system module not a website window. The user could cancel or print and we wouldn’t know.

Joe Christopher
About the Author

As Vice President, Analytics at Blast Analytics & Marketing, Joe leads a team of talented, analytics consultants responsible for helping clients understand and take action on their vast amounts of data, to continuously improve and EVOLVE their organizations. With 20 years of experience in analytics and digital marketing, Joe offers a high-level of knowledge and guidance to clients across all industries. He is an expert in all major analytics platforms including Google Analytics and Adobe Analytics, as well as various tag management systems such as Tealium and Adobe Launch. He also consults on data visualization, data governance, and data quality strategies. Having extensive expertise in many areas, has enabled Joe to become a well known thought leader and speak at industry events such as Tealium’s Digital Velocity series. Joe remains on the pulse of various information technology, programming languages, tools and services, keeping Blast and its clients on the leading edge.

Connect with Joe on LinkedIn. Joe Christopher has written on the Web Analytics Blog.

Ready To Do More With Your Data?

If you have questions or you’re ready to discuss how Blast can help you EVOLVE your organization, talk to an Analytics Consultant today.

Call 1 (888) 252-7866 or contact us below.