Collecting user statistics in ServiceNow

Andrew Pishchulin
4 min readOct 15, 2018

It’s a simple question, which may not have a simple answer: how many ServiceNow users are online at any given moment and what are they doing? What are the most visited pages, items, categories, and articles?

ServiceNow does not provide very granular usage statistics. Someone may argue that in ServiceNow you can see a list of online users, but that is not necessarily true. Because you can see only the users on the same node you’re logged in. So you should not be surprised when you know that someone is online for sure, but you don’t see that user in the list.

And we still want to know all of the online users, all of the active pages, locations, etc. and have comprehensive statistics on that.

A custom app may accomplish what you are looking for, but there is a much better solution — Google Analytics.

Google Analytics has everything you would ever need in terms of collecting stats: real-time data, pages, locations, transitions etc. It can track internal ServiceNow pages and forms as well as Service Portal pages.

So how we can add Google Analytics to track ServiceNow activities? It can be done in just two steps:

  1. Create a Google Analytics account and set up a tracking ID.
  2. Add a Google Analytics tracking script into ServiceNow.

1. Set up Google Analytics

This is a very simple and straightforward process, which is explained here.

First, you need to create an account (it’s free) at google.com/analytics.

Then you need to create a tracking ID, which is called property:

Once you get that done, Google Analytics will provide you with a script, which you usually need to copy and paste into every webpage on the website you want to track. The script contains your unique tracking ID:

2. Add Google Analytics script into ServiceNow

And there is a catch — you cannot directly copy and paste a default Google Analytics script into ServiceNow. It’s a mix of HTML and Javascript, but if we want to run it in ServiceNow, it should be pure Javascript code.

Luckily, we can modify the script and also define what data we want to send to Google Analytics.

Everything is based on analytics.js — a library, which provides many tracking options. And here comes your imagination — you can be very granular and define what you want to send and when to send it dynamically.

This is the modified script, which sends a page title whenever script is executed:

OK, we have a modified tracking script and now we need to put it into ServiceNow. If you want to track Service Portal statistics, then you need to add that script to the corresponding pages/widgets. But the most interesting thing is to track internal ServiceNow pages, and that can be done using global UI scripts.

Important: global UI script runs every time you open a form, list etc., so you should be very careful. If you add an incorrect script and it breaks the UI, it will impact the entire instance and fixing it may be problematic.

  1. Create a UI script and paste the tracking code from above. Make sure you put your own Google Analytics tracking ID in line #7.
  2. Make UI script global.

And that’s it. Reload you ServiceNow page and then check Google Analytics dashboard, you should see users online, be able to track statistics etc.

The approach described above is simple, straightforward and works well. But there is one problem, which may cause some data inconsistency: the script sends data to Google Analytics immediately once URL changes, but at that time the document Title may not be updated yet. And therefore on the Google Analytics dashboard, you may see the actual URL with an old page Title.

UPDATE: Part 2 explains how to update the script and eliminate any data inconsistency.

--

--