How To EASILY Track PDF Downloads & PDF Reads

Posted Saturday, January 16th, 2021 - 3,283 views

pdf-download-buttonTracking 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:

  • Create a PHP file and name it something like pdf-reader.php.
  • Place your Google Analytics tracking code in the header.
  • Upload it to the root directory of your server.
  • Add PHP code/logic that will redirect it to the PDF in question.
  • Update your links to your PDF files to point to pdf-reader.php.
  • Get the extra CSS provided.
  • Track PDF Views in Google Analytics

Create a PHP file and name it something like pdf-reader.php

This file will contain the logic you need to track and redirect users to the appropriate PDF.

Place your Google Analytics tracking code in the header

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.

Add PHP code/logic that will redirect it to the PDF in question

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.

Upload it to the root directory of your server

Once you’ve finished creating the PHP file above, upload it to the root directory of your website’s server.

Update your links to your PDF files to point to pdf-reader.php

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>

Get the extra CSS/HTML provided

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>

Track PDF Views in Google Analytics

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

Your email address will not be published. Required fields are marked *

©2024 First Planet

Studio
  • 2007 James St. 
    Burlington, ON, L7R 1H1
  • info@firstplanet.ca
  • 289-499-5934
Follow us