UNPKG

owl-analytics

Version:

A simple plug & play analytics tracker 🦉

112 lines (107 loc) • 3.25 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { default: () => owlAnalytics }); module.exports = __toCommonJS(index_exports); // src/utils.ts function getDeviceInfo() { return { userAgent: navigator.userAgent, platform: navigator.platform, language: navigator.language, screenWidth: window.screen?.width || 0, screenHeight: window.screen?.height || 0 }; } async function getLocation() { try { console.log("\u{1F30D} Attempting client-side location detection..."); const res = await fetch("https://ipapi.co/json/", { signal: AbortSignal.timeout(3e3) }); if (!res.ok) { throw new Error(`HTTP ${res.status}`); } const data = await res.json(); if (data.country_name && data.country_name !== "Unknown") { return { country: data.country_name, countryCode: data.country_code || data.country, city: data.city || "Unknown" }; } throw new Error("Invalid location data"); } catch (error) { const browserLang = navigator.language || navigator.languages?.[0] || "en-US"; const countryHint = browserLang.split("-")[1] || "Unknown"; return { country: countryHint, countryCode: countryHint, city: "Unknown" }; } } // src/client.ts async function startTracking(owlEye, apiUrl) { console.log("\u{1F989} Owl Analytics: Starting tracking..."); console.log("\u{1F989} OwlEye:", owlEye); console.log("\u{1F989} API URL:", apiUrl); const data = { owlEye, url: window.location.href, referrer: document.referrer, device: getDeviceInfo(), timestamp: (/* @__PURE__ */ new Date()).toISOString() }; try { const location = await getLocation(); data.country = location.country || "Unknown"; } catch (error) { data.country = "Unknown"; } try { const response = await fetch(apiUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), keepalive: true }); const responseData = await response.json(); } catch (err) { const blob = new Blob([JSON.stringify(data)], { type: "application/json" }); } } // src/index.ts function owlAnalytics({ owlEye, apiUrl = "http://localhost:5000/api/analytics/track" }) { if (!owlEye) { console.error("\u{1F989} Owl Analytics: owlEye (API key) is required"); return; } startTracking(owlEye, apiUrl); }