Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieves the HotJar user ID from the browser. The ID can be passed to analytics or other systems as a way to connect HotJar recordings to other systems.
(function () {
// gets hotjar user id (hjUID)
var hjUID = "";
try {
if (typeof (hj) != "undefined" && hj.hasOwnProperty("globals")) {
// sample: a21c0a02-3b48-53c6-94b4-5604e281e5d8
// the value before the first "-" ("a21c0a02") is the user identifier that can be referenced in the hj interface
// use the following snippet to return only the user identifier (eg// "a21c0a02"):
// hjUID =hj.globals.get("userId").split("-").shift();
// use the following snippet to return the full string (eg// "a21c0a02-3b48-53c6-94b4-5604e281e5d8")
hjUID = hj.globals.get("userId");
}
} catch (err) {
/** ignore **/
}
// return hjUID or an empty string ""
return hjUID;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment