Friday, 19 May 2017

How to Track When People Print Web Pages on your Site

TRACK PRINTED WEB PAGES WITH GOOGLE ANALYTICS




What we need to do is add an invisible 1×1 tracking image to the printed version of our web pages. Now when a site visitor prints a web page, through any route, the Analytics tracking image will download on their computer and print action will be recorded into your Google Analytics.

The implementation is really easy. Simply copy-paste this code above the closing </body> tag in your website template. If you are on WordPress, you can paste the code in your footer.php file. Do remember to replace UA-1234-56 with your actual Google Analytics Profile ID.


<script>

var googleAccountID = "UA-1234-56";

function s4() {
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}

function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

(function(){

  var GIF = "https://ssl.google-analytics.com/collect?v=1&t=event" +
      "&ec=print&tid=" + googleAccountID + "&cid=" + guid() +
      "&z=" + (Math.round((new Date()).getTime() / 1000)).toString() +
      "&ea=" + encodeURIComponent(document.title) +
      "&el=" + encodeURIComponent(document.location.pathname);

  var rule = "body:after{content:url(" + GIF + ")}";
  var head = document.head || document.getElementsByTagName('head')[0];
  var css  = document.createElement('style');

  if (css && head) {

      css.setAttribute("type",  "text/css");
      css.setAttribute("media", "print");

      if (css.styleSheet) { // For IE
          css.styleSheet.cssText = rule;
      } else {
          css.appendChild(document.createTextNode(rule));
      }

      head.appendChild(css);
      
    
  }

})();

</script>

Once you have added the tracking JavaScript code to your web pages, log in to your Google Analytics dashboard, go to Real Time –> Events and choose Print for the event category. Alternatively, for old reports, you should go to Behavior -> Events report.


HOW PRINT TRACKING WITH ANALYTICS WORKS?

The above JavaScript code adds the following CSS rule to your web page. It append the Google Analytics tracking image to the page but the rule is only trigger when the print stylesheet is active.

<style type="text/css" media="print">
  body:after {
        content:url(GOOGLE_ANALYTICS_TRACKING_IMAGE);
  }
</style>
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.


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Labels

404 AdBlock Add-on Airtel GPRS Trick Airtel SMS Trick Alexa Amazon Amazon Kindle Amazon Prime Android Android 8 Android Oreo antivirus Apple Apple Mac ASCII Audacity Audio Authotkey Backup Balance Transfer in Vodafone Battery Bing Blogger Blogging Bookmarklet Browser Camera Chromebook clock Cloud colors command lines Computer Computer Tricks configuration Contact Creative Commons Credit Card CSS devolop DIY Doodle DOS Download Dropbox E-Mail eBook Email Email Attachment Embed Encryption English Error Evernote Eyes Facebook Facebook Tricks Feedburner Flipkart Font Foursquare Free Internet Free sms trick in Vodafone G Mail Gadget Game Getty Images GIF Gists Github Google Google AdSense Google Analytics Google Apps Google Chrome Google Contacts Google Currents Google DNS Google Docs Google Drive Google Earth Google Font Google Forms Google Images Google Map Google Photos Google Play Store Google Plus Google Print Google Reader Google Script Google Sheets Google Spreadsheet Google Translate GPRS Setting GPS Hacking Health App HelloFax Hindi Hoodie HTML Icons idea Image Editing Images IMEI Indian Railways Infographics Instagram Internet Internet Explorer Internet Tricks iOS iPad iPhone IRCTC iTunes iTV JavaScript JioCinema JioTV Junglee Kindle Language Translation Laptop Laptop. TV Life Time FREE GPRS Life-Style Link Linkedln Linux logo Make Money Online Microdoft Powerpoint Microdoft Word Microsoft Office Microsoft Outlook Mobile Mosaic Music Name Networking nexus Notepad OCR Online Shopping Open DNS OS Outlook Password PDF Petya Phillips Hue Lights Photogtraphy Pixel Play Station Podcasts Pokemon Pokemon Go Polls Print Productivity Proxy Server Pushbullet QR Code Ransomware Reddit Reliance Hack GPRS Reliance Jio RGB Ringtone Router RSS Safe Mode Samsung Galaxy S Scrabble Screen Capture Screen Sharing Screencast Secrets Security Send free sms from PC SEO Sierra Skype Slideshare SMBv1 SMS Snapchat Snapdeal Social Media Solution Sound Device Speech Recognition Sql Steam Sync Synology NAS Tata Docomo GPRS trick Teleprompter Torrent Trick Tricks TV Twitter UltraISO Unicode Unknown Extension Unlimited 2GB Unlimited 3GB Unlimited GPRS USB USB Security Key Video Editing virtual desktop Virus attack VLC Vodafone 110% working trick for GPRS Vodafone 3g Vodafone GPRS VPN wallpapers WannaCry Web Design Web Domain Website Wget Whatsapp WiFi Wikipedia Windows Windows 10 Windows 10 S Windows KN Windows Tricks windows updates Winows N Wolfarm Alpha WordPress XBox YouTube Zip
Twitter Delicious Facebook Digg Stumbleupon Favorites More