logo

NJP

Hotjar Service Portal integration

Import · Mar 08, 2019 · article

Hotjar is a saas product that basically tracks users' clicks, mouse movements and scrolls on your site. It can do a lot more but the basic use case is to capture this data and see how people are using pages etc.

To integrate with ServiceNow Service Portal is pretty straightforward. There are some limitations as well so I'll go over those too.

Integration

Assuming you've already signed up for a hotjar account and created your first site, hotjar will have prompted you to add a script to your page . This script injects the scripts that actually do all the clever hotjar gubbins.

To include this script in the Service Portal page you need to add it as a js include in your Portal's theme.

1. Go to Service Portal > Portals and select your portal

2. Click the Theme record

3. Scroll to the related lists and click to add a new JS Include

4. On this first page click the magnifying glass on JS Include and click to create a new record

5. On this page fill in the fields:

Display name: hotjar

Source: UI Script

6. Click the magnifying glass on UI script and click to create a new record

7. On this page fill in the fields:

Name: hotjar

UI Type: All

Description: Script to initiate hotjar tracking

Script: copy in the tracking code from hotjar (strip out any non-JavaScript code, described below)

Adjustments/Improvements

Stripping out non-JavaScript

The hotjar tracking script comes wrapped in an html element and with a comment above, Simply remove the two <script> tags and the comment and you are left with only JavaScript.</p> <h3>Dealing with page loading</h3> <p>After a bit of trial and error I found that hotjar would load immediately while my portal page was still showing loading spinner</p> <p><img src="https://community.servicenow.com/community/image/serverpage/image-id/147679iDE6F5FC107F11348/image-size/large?v=v2&px=999" alt="image"></p> <p>This was a problem when I went to review the data in hotjar as the screen shot the code has taken was of this loading page and so users&#39; clicks etc didn&#39;t show over the elements they were actually on. </p> <p>To prevent this I added some code to only load hotjar once loading was complete. It&#39;s a bit a of fudge so if anyone has a better idea I&#39;d be happy to hear. </p> <p>My code uses jQuery to check if the loading element is present:</p> <ul> <li>If it is, it waits a second and checks again</li> <li>If it isn&#39;t, it runs the hotjar snippet</li> </ul> <pre><code>jQuery(document).ready(function(){ checkLoading(); function checkLoading(){ if(isLoading()){ window.setTimeout(checkLoading,1000); } else { startHotjar(); } } function isLoading(){ return jQuery(&#39;BODY .page:first-child H4:first-child&#39;).hasClass(&#39;sp-page-loader&#39;); } function startHotjar(){ //your hotjar tracking code } }); </code></pre> <h2>Limitations</h2> <p>Currently there is some functionality that I don&#39;t believe hotjar can support for ServiceNow OOB Portal widgets like sc_cat_item.</p> <ul> <li>hotjar doesn&#39;t appear to recognise forms on cat items as html forms</li> <li>hotjar does not capture entire screen i screenshot if scrolling required (i think this may be remediable)</li> </ul>

View original source

https://www.servicenow.com/community/developer-articles/hotjar-service-portal-integration/ta-p/2327889