Posted Saturday, January 16th, 2021 - 3,226 views
Tracking PDF views in Google Analytics can involve an extremely complex setup using Google Tag Manager, but there is another easier way. To do this you’ll need access to your website’s server to be able to create a .php file. Read on for instructions.
I’ll lay out the steps below in list form, and the instructions will follow.
Steps to tracking PDF downloads without using Google Tag Manager:
This file will contain the logic you need to track and redirect users to the appropriate PDF.
Make sure the php file has all the proper HTML code that represents a website including an HTML tag, any META tags, a HEAD tag, a TITLE tag, a BODY tag, and all associated CLOSING tags.
Place your Google Analytics code in the head of the document.
If you are only tracking one PDF read/download, add the following code into the body of your PHP file:
<?php sleep(1); $pdf_file = htmlspecialchars($_GET['pdf_file']); if(isset($_GET['pdf_file'])) { if ($pdf_file=="filename") { header("refresh:0;url=https://www.website.com/pdf/pdf_name.pdf?pdf_file=filename"); } else { header("refresh:0;url=https://www.website.com/"); } } ?>
If you want to track multiple PDF reads/downloads, add the following code into the body of your PHP file and revise it accordingly to your specifications:
<?php sleep(1); $pdf_file = htmlspecialchars($_GET['pdf_file']); if(isset($_GET['pdf_file'])) { if ($pdf_file=="pdf_01") { header("refresh:0;url=https://www.website.com/pdf/pdf_01.pdf?pdf_file=pdf_01"); } elseif ($pdf_file=="pdf_02") { header("refresh:0;url=https://www.website.com/pdf/pdf_02.pdf?pdf_file=pdf_02"); } elseif ($pdf_file=="pdf_03") { header("refresh:0;url=https://www.website.com/pdf/pdf_03.pdf?pdf_file=pdf_03"); } elseif ($pdf_file=="pdf_04") { header("refresh:0;url=https://www.website.com/pdf/pdf_04.pdf?pdf_file=pdf_04"); } elseif ($pdf_file=="pdf_05") { header("refresh:0;url=https://www.website.com/pdf/pdf_05.pdf?pdf_file=pdf_05"); } else { header( "refresh:0;url=https://www.website.com/" ); } } ?>
We add the sleep() function at 1 second to let the user see the page is being redirected, and most importantly to let Google Analytics track the hit.
Once you’ve finished creating the PHP file above, upload it to the root directory of your website’s server.
Now you have to edit all the current links on your site pointing to PDFs. You will link them like the following:
<a href="ttps://www.website.com/pdf-reader.php?pdf_file=pdf_01">Click here to read PDF</a>
Add the following line of code to create a waiting-spinner effect to your document for users to briefly see.
<div class="loader"></div>
Use the following CSS to make the waiting-spinner stylish and spin.
<style> .loader { border: 16px solid #f3f3f3; border-radius: 50%; border-top: 16px solid #3498db; width: 120px; height: 120px; -webkit-animation: spin 1s linear infinite; /* Safari */ animation: spin 1s linear infinite; } /* Safari */ @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style>
Lastly you’ll want to track the PDF reads in Google Analytics. In our case above if you wanted to track views in your fourth pdf you would go to the Page Views/Landing Pages report in Google Analytics, and search for “/pdf_name.pdf?pdf_file=pdf_04” and there you will find the views.
Feel free to leave your comments or code optimization tips in the comments below.
Leave a Reply