UNPKG

@codeankitanime/eventlogger

Version:

A lightweight JavaScript event tracker that logs user interactions (clicks, inputs, scrolls, etc.) along with browser, OS, and optional geolocation/IP metadata. Ideal for building custom front-end analytics.

32 lines (28 loc) 1.24 kB
export async function collectUserMeta() { const ua = navigator.userAgent || 'Unknown'; let os = 'Unknown OS'; let browser = 'Unknown Browser'; const deviceType = /mobile/i.test(ua) ? 'Mobile' : 'Desktop'; if (ua.includes('Windows')) os = 'Windows'; else if (ua.includes('Mac')) os = 'macOS'; else if (ua.includes('Android')) os = 'Android'; else if (ua.includes('iPhone') || ua.includes('iPad')) os = 'iOS'; else if (ua.includes('Linux')) os = 'Linux'; if (ua.includes('Chrome')) browser = 'Chrome'; else if (ua.includes('Firefox')) browser = 'Firefox'; else if (ua.includes('Safari') && !ua.includes('Chrome')) browser = 'Safari'; else if (ua.includes('Edg')) browser = 'Edge'; else if (ua.includes('MSIE') || ua.includes('Trident')) browser = 'Internet Explorer'; let location = 'Unavailable'; if (navigator.geolocation) { try { const pos = await new Promise((res, rej) => navigator.geolocation.getCurrentPosition(res, rej, { timeout: 3000 }) ); location = `Lat: ${pos.coords.latitude}, Long: ${pos.coords.longitude}`; } catch { location = 'Permission Denied'; } } return { os, browser, deviceType, location, userAgent: ua }; }