Add Google Analytics to OSTicket

To add Google Analytics to OSTicket sounds a daunting task but in reality it is fairly straightforward providing you know a small amount of HTML and or PHP and are able to login to your FTP Server.

This guide will presume that you already have a Google Analytics account and have created a new site property for your OSTicket installation.

Setup User-ID Tracking

You will also need to enabled the User-ID tracking feature of Google Analytics if you wish to track internal users of the system.

2020 06 22 14 50 40 Analytics and 4 more pages Work Microsoft​ Edge b
Admin -> Account Settings -> Tracking Info -> User-ID
2020 06 22 14 50 53 Analytics and 4 more pages Work Microsoft​ Edge b
Once enabled you should see two Green Tickets and complete step 3 to create a user view dashboard

Edit files for staff analytics

You will need to edit the file /scp/tickets.php, this file handles all tickets related actions on the site and is used to pull ticket and queue information when a staff user requests it.

From my testing adding the Google Analytics code to the bottom of the page is sufficient to track every interaction a user has with the SCP side of the site

2020 06 22 14 55 45 Microsoft Store 1

The standard code Google give you will be enough to track Anonymous access to the site but you can edit this to include the additional line below that will track logged in staff users by the means of echoing out their username to the tracking code.

<?php echo $thisstaff->getFirstName() ?>

Copy the below code ?> included at the very bottom of the tickets.php file and click save.

?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_CODE"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('set', {'user_id': '<?php echo $thisstaff->getFirstName() ?>'});
  gtag('config', 'YOUR_TRACKING_CODE');

</script>

Edit files for client anayltics

To enable client analytics simply edit the file /include/client/header.inc.php to include the standard tracking code as below right after the HTML <head> section

<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_CODE"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'YOUR_TRACKING_CODE');
</script>

Check its working

You should be able to check it is working from the Google Analytics homepage by simply looking on your dashboard and noting any new traffic.

2020 06 22 15 06 30 Analytics and 2 more pages Work Microsoft​ Edge

Staff user tracking can be found under Audience and User Explorer, from here you will be able to see the journey the staff member has taken through the site and the tickets they have worked on.

2020 06 22 15 07 47 Analytics and 2 more pages Work Microsoft​ Edge

By clicking on a user you can then drill down further to see the individual tickets they have visited.

2020 06 22 15 10 02 Analytics and 2 more pages Work Microsoft​ Edge

If you found this article helpful, would you consider buying me a Coffee?