var Tracky = { setApiKey: function (key) { ApiKey = key }, recordView: function (location, referrer, userId, organisationId, metadata) { try { if (!ApiKey) { console.error('Missing API Key'); return; } var xhr = new XMLHttpRequest(); xhr.open( "POST", "https://tracking.lbr.cloud/api/v1/record/view", true ); xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("X-API-KEY", ApiKey); xhr.setRequestHeader("X-HOSTNAME", window.location.hostname); xhr.onerror = function (e) { console.error(e); }; xhr.send( JSON.stringify({ id: this.getId(), referrer: referrer, location: location, organisationId: organisationId, userId: userId, type: "View", metadata: metadata }) ); } catch (e) { console.error(e); } }, getId: function () { var randomBytesLength = 24; var randomBytes; var usedCrypto = true; if (window.crypto && window.crypto.getRandomValues) { randomBytes = new Uint8Array(randomBytesLength); window.crypto.getRandomValues(randomBytes); } else { randomBytes = []; for (var i = 0; i < randomBytesLength; i++) { randomBytes.push(Math.floor(Math.random() * 256)); } usedCrypto = false; } var id = window.btoa(String.fromCharCode(...randomBytes)); if (!usedCrypto) { id = id + '_C'; } return id; } };