solo-analytics
v0.1.1
Published
A comprehensive browser analytics library with TypeScript support
Downloads
1
Maintainers
Readme
📊 Solo Analytics
A lightweight TypeScript library for comprehensive browser and device information collection with a clean API surface.
Features
- 🔍 Extensive browser, OS, and device detection with full TypeScript support
- 📱 Precise mobile, tablet, and desktop device classification
- 🌐 Network connectivity and performance data
- 📏 Screen and viewport dimensions with orientation detection
- ⚡ Comprehensive performance metrics (navigation, timing, memory)
- 🌍 Locale, timezone, and environment information
- ⚙️ Advanced feature detection (camera, microphone, battery, etc.)
- 🔒 Incognito/private mode detection
- 🔄 Auto-refresh capability for dynamic metrics
Installation
npm install solo-analyticsBasic Usage
import { useSoloAnalytics } from "solo-analytics";
// Initialize with default options
const analytics = useSoloAnalytics();
// Access common properties
console.log("Mobile device:", analytics.isMobile);
console.log("Browser:", analytics.browserName);
console.log("OS:", analytics.osName);
// Access the complete data object
console.log("Full analytics data:", analytics.data);Configuration Options
import { useSoloAnalytics } from "solo-analytics";
const analytics = useSoloAnalytics({
// Automatically refresh dynamic data (network, performance)
autoRefresh: true,
// Refresh interval in milliseconds (default: 30000)
refreshInterval: 10000,
// Track page visibility changes
trackVisibility: true,
});API Reference
The useSoloAnalytics() function returns an object with the following methods and properties:
Methods
refresh(): Manually triggers a refresh of all analytics datadestroy(): Cleans up all event listeners and timers to prevent memory leaks
Convenience Properties
isMobile: Whether the device is a mobile phoneisTablet: Whether the device is a tabletisDesktop: Whether the device is a desktop computerisOnline: Current network connection stateisVisible: Whether the page is currently visiblebrowserName: The name of the browserosName: The name of the operating system
Complete Data
data: An object containing all detailed analytics information
Data Structure
The data object contains the following structure:
interface SoloAnalyticsInfo {
browser: {
name: string;
version: string;
major: string;
userAgent: string;
vendor: string;
engine: string;
engineVersion: string;
};
os: {
name: string;
version: string;
architecture: string;
};
device: {
type: "mobile" | "tablet" | "desktop" | "unknown";
vendor: string;
model: string;
orientation: "portrait" | "landscape";
isMobile: boolean;
isTablet: boolean;
isDesktop: boolean;
touch: boolean;
};
network: {
online: boolean;
effectiveType: string;
downlink: number;
rtt: number;
saveData: boolean;
};
screen: {
width: number;
height: number;
availWidth: number;
availHeight: number;
colorDepth: number;
orientation: string;
pixelRatio: number;
touchPoints: number;
};
performance: {
memory: {
jsHeapSizeLimit: number;
totalJSHeapSize: number;
usedJSHeapSize: number;
} | null;
navigation: {
type: string;
redirectCount: number;
};
timing: {
loadTime: number;
domContentLoaded: number;
firstPaint: number | null;
firstContentfulPaint: number | null;
};
};
location: {
timeZone: string;
language: string;
languages: string[];
isRestricted: boolean;
doNotTrack: boolean | null;
cookiesEnabled: boolean;
localStorage: boolean;
sessionStorage: boolean;
};
pageVisibility: "visible" | "hidden";
referrer: string;
isIncognito: boolean;
hasCamera: boolean | null;
hasMicrophone: boolean | null;
hasBattery: boolean | null;
batteryLevel: number | null;
batteryCharging: boolean | null;
permissions: Record<string, string>;
}Framework Integration Examples
React
import { useEffect, useState } from "react";
import { useSoloAnalytics, SoloAnalyticsReturn } from "solo-analytics";
function DeviceInfo() {
const [analytics, setAnalytics] = useState<SoloAnalyticsReturn | null>(null);
useEffect(() => {
// Initialize on the client side
const analyticsInstance = useSoloAnalytics();
setAnalytics(analyticsInstance);
// Cleanup on unmount
return () => {
analyticsInstance.destroy();
};
}, []);
if (!analytics) return <div>Loading...</div>;
return (
<div>
<h1>Device Information</h1>
<p>
Type:{" "}
{analytics.isMobile
? "Mobile"
: analytics.isTablet
? "Tablet"
: "Desktop"}
</p>
<p>Browser: {analytics.browserName}</p>
<p>Operating System: {analytics.osName}</p>
<p>Network Status: {analytics.isOnline ? "Online" : "Offline"}</p>
</div>
);
}Vue 3
<template>
<div>
<h1>Device Information</h1>
<p>Type: {{ deviceType }}</p>
<p>Browser: {{ analytics.browserName }}</p>
<p>Operating System: {{ analytics.osName }}</p>
<p>Network Status: {{ analytics.isOnline ? "Online" : "Offline" }}</p>
</div>
</template>
<script setup lang="ts">
import { onMounted, onUnmounted, ref, computed } from "vue";
import { useSoloAnalytics, SoloAnalyticsReturn } from "solo-analytics";
const analytics = ref<SoloAnalyticsReturn | null>(null);
onMounted(() => {
analytics.value = useSoloAnalytics({
trackVisibility: true,
});
});
onUnmounted(() => {
analytics.value?.destroy();
});
const deviceType = computed(() => {
if (!analytics.value) return "Unknown";
if (analytics.value.isMobile) return "Mobile";
if (analytics.value.isTablet) return "Tablet";
return "Desktop";
});
</script>Nuxt 3
<template>
<div>
<h1>Device Information</h1>
<div v-if="analytics">
<p>Type: {{ deviceType }}</p>
<p>Browser: {{ analytics.browserName }}</p>
<p>Operating System: {{ analytics.osName }}</p>
<p>Network Status: {{ analytics.isOnline ? "Online" : "Offline" }}</p>
</div>
<div v-else>
<p>Loading device information...</p>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from "vue";
import { useSoloAnalytics, SoloAnalyticsReturn } from "solo-analytics";
const analytics = ref<SoloAnalyticsReturn | null>(null);
// Important: Only initialize on client-side to avoid SSR issues
onMounted(() => {
analytics.value = useSoloAnalytics({
trackVisibility: true,
autoRefresh: true,
refreshInterval: 15000,
});
});
onBeforeUnmount(() => {
if (analytics.value) {
analytics.value.destroy();
}
});
const deviceType = computed(() => {
if (!analytics.value) return "Unknown";
if (analytics.value.isMobile) return "Mobile";
if (analytics.value.isTablet) return "Tablet";
return "Desktop";
});
</script>Vanilla JavaScript
<script type="module">
import { useSoloAnalytics } from "solo-analytics";
document.addEventListener("DOMContentLoaded", () => {
const analytics = useSoloAnalytics();
// Display information on the page
document.getElementById("deviceType").textContent = analytics.isMobile
? "Mobile"
: analytics.isTablet
? "Tablet"
: "Desktop";
document.getElementById(
"browserInfo"
).textContent = `${analytics.browserName} on ${analytics.osName}`;
// Cleanup on window close
window.addEventListener("beforeunload", () => {
analytics.destroy();
});
});
</script>Browser Compatibility
Solo Analytics is compatible with all modern browsers, including:
- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 16+
- Opera 47+
- iOS Safari 11+
- Android Browser 67+
Some advanced features may not be available in older browsers but the library includes graceful fallbacks.
Development
# Install dependencies
npm install
# Run development server
npm run dev
# Build for production
npm run buildContributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Repository
GitHub: https://github.com/cesswhite/solo-analytics
License
MIT License
Copyright (c) 2025 Céss White
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
