I was looking for something new to track within the websites interactions, and I decided to give a try to track the user’s intent to print our pages.
So I’m going to show you my own approach for tracking this user action using Google Tag Manager.
We’ll need to create a new tag with the following code will allow us to track the user printing intent when he clicks on printing from the File Menu or when he uses the keys shortcut (Control-P on Windows)
It may happen that a browser does have support for the onBeforePrinting event and mediaQueries, so the code is looking into the dataLayer for a previously send printing event to avoid duplicate events.
Reminder: The code above needs to be wrapped between <script></script> tags or you will end having this text code injected on your page.
Just to be safe, we’ll run this tag on the “gtm.dom” event. Then we’ll have a push into the dataLayer telling us the user tried to print the current page, and we’ll be able to fire an event for Google Analytics or use it with any other tool.