@one-shot-analytics/react
Version:
One Shot Analytics - Privacy-focused analytics for Next.js applications
115 lines (113 loc) • 4.01 kB
JavaScript
;
"use client";
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.tsx
var src_exports = {};
__export(src_exports, {
Analytics: () => Analytics
});
module.exports = __toCommonJS(src_exports);
var import_react = require("react");
function getBrowser(userAgent) {
if (userAgent.includes("Chrome")) return "Chrome";
if (userAgent.includes("Firefox")) return "Firefox";
if (userAgent.includes("Safari")) return "Safari";
if (userAgent.includes("Edge")) return "Edge";
if (userAgent.includes("Opera")) return "Opera";
return "Unknown";
}
function getOS(userAgent) {
if (userAgent.includes("Windows")) return "Windows";
if (userAgent.includes("Mac")) return "MacOS";
if (userAgent.includes("Linux")) return "Linux";
if (userAgent.includes("Android")) return "Android";
if (userAgent.includes("iOS")) return "iOS";
return "Unknown";
}
function getDevice(userAgent) {
if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(userAgent)) return "Tablet";
if (/Mobile|Android|iP(hone|od)|IEMobile|BlackBerry|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(userAgent)) return "Mobile";
return "Desktop";
}
function Analytics({
siteId,
apiKey,
endpoint = "https://one-shot-analytics.vercel.app/api/analytics"
}) {
(0, import_react.useEffect)(() => {
if (typeof window === "undefined") return;
const SESSION_ID = Math.random().toString(36).substring(2);
const userAgent = window.navigator.userAgent;
async function sendAnalytics() {
try {
console.log("[One Shot Analytics] Sending pageview:", {
url: window.location.href,
siteId,
endpoint
});
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
site_id: siteId,
event_type: "pageview",
page_url: window.location.href,
referrer: document.referrer || null,
user_agent: userAgent,
browser: getBrowser(userAgent),
os: getOS(userAgent),
device: getDevice(userAgent),
session_id: SESSION_ID,
country: null,
// Will be set by the server
city: null
// Will be set by the server
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Analytics request failed: ${text}`);
}
console.log("[One Shot Analytics] Pageview tracked successfully");
} catch (error) {
console.error("[One Shot Analytics] Failed to send analytics:", error);
}
}
sendAnalytics();
let lastUrl = window.location.href;
const observer = new MutationObserver(() => {
if (window.location.href !== lastUrl) {
lastUrl = window.location.href;
sendAnalytics();
}
});
observer.observe(document, { subtree: true, childList: true });
return () => {
observer.disconnect();
};
}, [siteId, apiKey, endpoint]);
return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Analytics
});