Collecting statistics in ServiceNow (part 2)
Our goal: be able to explore real-time statistical data about ServiceNow users — how many users are online and what are they doing? What are the most visited pages, items, categories, articles, etc.?
As I mentioned in my previous article, collecting real-time statistics in ServiceNow can be as easy as 10 lines of code. But the original approach has one flaw, which is clearly visible on a Google Analytics dashboard: the URL of a page in most of the cases does not correspond to the page Title.
Here is why: ServiceNow backend UI (UI16) is based on iframes — there is a parent page and the content frame inside.
When you navigate through ServiceNow UI you mostly change and reload the content frame. After a new content is loaded into iframe, ServiceNow updates the parent page Title, to address the new content. That small time difference between the fact that content loaded and the page title update creates data inconsistency on Google Analytics side.
In order to collect statistical data consistently, we need to properly handle two events:
- iFrame content load
- Parent page Title update
This can be done using MutationObserver — it provides the ability to watch for changes being made to the DOM tree.
We hook up a <Title> element of the parent page and send stats only when the element updated (lines #8–19):
And that’s it, the updated script will make data consistent so a page URL will correspond to the page Title.
Quick recap:
- Create a Google Analytics ID (explained in part 1)
- Create Global UI script
- Copy-Paste updated code from above
- Update line 6 with your Google Analytics ID
- Save the script and reload the page.
Now you can check your Google Analytics dashboard and page Title & URL data should be consistent.