roaarrr-browser
Version:
Browser analytics tracking library for roaarrr
159 lines (138 loc) • 4.25 kB
JavaScript
(function (root, factory) {
if (typeof exports === "object") {
module.exports = factory();
} else if (typeof define === "function" && define.amd) {
define([], factory);
} else {
root.roaarrr = factory();
}
})(typeof self !== "undefined" ? self : this, function () {
var TRACKING_ENDPOINT = "https://server-4g8o.onrender.com";
var API_KEY = null;
// Global error handlers to prevent console errors
if (typeof window !== "undefined") {
window.addEventListener("error", function(event) {
console.warn("Uncaught error:", event.error);
event.preventDefault();
});
window.addEventListener("unhandledrejection", function(event) {
console.warn("Unhandled promise rejection:", event.reason);
event.preventDefault();
});
}
function getPageInfo() {
return {
url: typeof window !== "undefined" ? window.location.href : "",
referrer: typeof document !== "undefined" ? document.referrer : "",
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : "",
language: typeof navigator !== "undefined" ? navigator.language : "",
};
}
function sendTrackingData(data) {
if (!API_KEY) {
return;
}
if (typeof fetch === "undefined") {
return;
}
fetch(TRACKING_ENDPOINT + "/api/pageview", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
body: JSON.stringify(data),
}).catch((error) => {
// Silently handle errors
});
}
function sendIdentifyData(userData) {
if (!API_KEY) {
console.warn(
"API key is required. Please initialize the analytics with an API key."
);
return;
}
if (!userData.identification) {
console.warn(
"Identification is required. Please provide an identification."
);
return;
}
if (typeof fetch === "undefined") {
console.warn("fetch is not available in this environment");
return;
}
fetch(TRACKING_ENDPOINT + "/api/identify", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
body: JSON.stringify(userData),
}).catch((error) => {
console.warn("Error sending identify data:", error);
});
}
function sendEventData(eventName, eventData) {
if (!API_KEY) {
console.warn(
"API key is required. Please initialize the analytics with an API key."
);
return;
}
if (typeof fetch === "undefined") {
console.warn("fetch is not available in this environment");
return;
}
fetch(TRACKING_ENDPOINT + "/api/event", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
body: JSON.stringify({ eventName, ...eventData }),
}).catch((error) => {
console.warn("Error sending event data:", error);
});
}
var analytics = {
init: function (apiKey, config) {
API_KEY = apiKey;
if (!API_KEY) {
console.warn("API key is required");
return;
}
if (typeof window !== "undefined") {
sendTrackingData(getPageInfo());
let lastUrl = window.location.href;
if (typeof MutationObserver !== "undefined") {
new MutationObserver(() => {
const currentUrl = window.location.href;
if (currentUrl !== lastUrl) {
lastUrl = currentUrl;
sendTrackingData(getPageInfo());
}
}).observe(document, { subtree: true, childList: true });
}
}
},
identify: function (userData) {
if (userData && typeof userData !== "object") {
console.warn("User data, if provided, must be an object.");
return;
}
sendIdentifyData(userData || {});
},
funnel: function (eventName, eventData) {
if (!eventName || typeof eventName !== "string") {
console.warn("Event name is required and must be a string.");
return;
}
sendEventData(eventName, eventData || {});
},
};
// Support both default and named exports
analytics.analytics = analytics;
return analytics;
});