tinyplg-browser
Version:
Browser analytics tracking library for tinyplg
151 lines (131 loc) • 4.07 kB
JavaScript
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define([], factory);
} else {
root.tinyplgAnalytics = factory();
}
}(typeof self !== 'undefined' ? self : this, function () {
var TRACKING_ENDPOINT = "https://server-4g8o.onrender.com";
var API_KEY = null;
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) {
console.error(
"API key is required. Please initialize the analytics with an API key."
);
return;
}
if (typeof fetch === 'undefined') {
console.error("fetch is not available in this environment");
return;
}
fetch(TRACKING_ENDPOINT + "/api/pageview", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
body: JSON.stringify(data),
}).catch((error) => {
console.error("Error sending analytics data:", error);
});
}
function sendIdentifyData(userData) {
if (!API_KEY) {
console.error(
"API key is required. Please initialize the analytics with an API key."
);
return;
}
if (!userData.identification) {
console.error(
"Identification is required. Please provide an identification."
);
return;
}
if (typeof fetch === 'undefined') {
console.error("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.error("Error sending identify data:", error);
});
}
function sendEventData(eventName, eventData) {
if (!API_KEY) {
console.error(
"API key is required. Please initialize the analytics with an API key."
);
return;
}
if (typeof fetch === 'undefined') {
console.error("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.error("Error sending event data:", error);
});
}
var analytics = {
init: function (apiKey, config) {
API_KEY = apiKey;
if (!API_KEY) {
console.error("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.error("User data, if provided, must be an object.");
return;
}
sendIdentifyData(userData || {});
},
funnel: function (eventName, eventData) {
if (!eventName || typeof eventName !== "string") {
console.error("Event name is required and must be a string.");
return;
}
sendEventData(eventName, eventData || {});
}
};
// Support both default and named exports
analytics.analytics = analytics;
return analytics;
}));