How to Fully Track the Print Page Action with Google Analytics

How to Fully Track the Print Page Action with Google Analytics

Posted by on Tue, Jul 9, 2013
Filed Under | Google Analytics


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.

<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>

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.

Enjoy this post?

Join the discussion below, subscribe to our RSS feed or share it on the web.

This post was written by:

has written 30 posts on the Web Analytics Blog.

Joe is the Analytics Director and a Partner at Blast Analytics & Marketing. He understands Google Analytics like nobody else and is a master of many programming languages.

Add Joe to your circles on Google+


Tags: , , , , ,
  • http://eduardo.cereto.net/ Eduardo Cereto Carvalho

    Thanks for the article, good read.

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

  • http://www.blastam.com Joe Christopher

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

  • http://www.blastam.com Joe Christopher

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

  • 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) {}

  • http://www.blastam.com Joe Christopher

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

  • Angela Lindsey

    umbbigbash.org

  • http://www.blastam.com Joe Christopher

    @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.

  • http://www.blastam.com Joe Christopher

    @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.

  • 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 :-)

  • Becky Boyland

    How much of the code does this snippet replace?

  • http://www.web-tasks.com/about Orlando Small Business SEO

    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….

  • http://web-tasks.com/home/pricing Small Business SEO

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



Goal Driven Online Marketing & Analytics
Copyright © 1999-2013 Blast Analytics & Marketing