Referencing another SO question which is close to your question: Capturing Hotjar User ID in Google Tag Manager & Google Analytics
Anyway you could check if this property exists and if it's a function. At least this is how I solved it.
One thing might be that you need to wait for the DOM to be available:
document.addEventListener("DOMContentLoaded", function () {
hj.globals.get('userId').split('-').shift()
};
Another thing might be that you need to check if the script was loaded at all.
if (window.hj && typeof (window.hj) === "function") {
hj.globals.get('userId').split('-').shift()
}
With both in mind I would perhaps suggest the following code to be very save about it.
document.addEventListener("DOMContentLoaded", function () {
if (window.hj && typeof (window.hj) === "function") {
hj.globals.get('userId').split('-').shift()
}
};