UNPKG

crunchit

Version:

Autotrack the events from your users

49 lines (46 loc) 1.45 kB
function identify(userDetails) { return new Promise((resolve, reject) => { let distinctId = window.localStorage.getItem("DISTINCT_ID"); let sessionId = window.sessionStorage.getItem("CRUNCH_SESSION_ID"); // ! Getting the stuff from window object // let battery; const API_URL = "https://events-backend-5dzspnb6za-uc.a.run.app/"; let app_id = window.localStorage.getItem("CRUNCH_APP_ID"); fetch(`${API_URL}add-relation`, { method: "POST", headers: { "Content-Type": "application/json", project_id: app_id, }, body: JSON.stringify({ ...userDetails, distinctId, id: sessionId, }), }) .then((response) => { // ! Clear the batch window.localStorage.setItem( "CRUNCH_IDENTIFY", JSON.stringify({ ...userDetails, id: distinctId, }) ); return response.json(); }) .then((data) => { if (window.crunchDebug) console.log("add-relation response:", data); resolve(data); }) .catch((error) => { if (window.crunchDebug) console.error("add-relation error:", error); reject(error); }); // if (typeof window !== "undefined") { // battery = await window.navigator?.getBattery?.(); // battery = { isCharging: battery?.charging, level: battery?.level }; // } }); } module.exports = identify;