I'll break down the design and architecture of the plausible-tracker based on the repository content.
Core Architecture
The plausible-tracker is a lightweight analytics tracking library designed to interact with Plausible Analytics. Here's a detailed breakdown of its architecture:
1. Main Components
The tracker consists of two main modules:
tracker.ts - The core tracking functionality
request.ts - Handles the actual event sending logic
Implements history API monitoring by wrapping history.pushState
Listens to popstate events
Optionally tracks hash changes if hashMode is enabled
Provides cleanup functionality to remove listeners
c) Outbound Link Tracking
Uses MutationObserver to detect link changes in the DOM
Automatically tracks clicks on external links
Handles dynamic content through DOM mutation observation
Provides cleanup functionality to remove observers and listeners
5. Safety Features
The tracker implements several safety checks:
typescript
- Localhost detection and optional tracking
- localStorage-based opt-out (`plausible_ignore`)
- Safe error handling for localStorage access
6. Integration Features
Exposes global window.plausible for external access
Supports both module and CommonJS imports
Provides TypeScript type definitions
Supports callback functions for event tracking confirmation
7. Event Flow
User action triggers event
Event is processed through tracker
Safety checks are performed
Payload is constructed
Event is sent to Plausible API
Optional callback is executed with status
8. Design Principles
The tracker follows several key design principles:
Lightweight (< 1kb)
Non-blocking (async event sending)
Type-safe (full TypeScript support)
Cleanup-friendly (all features provide cleanup functions)
Configuration-flexible (supports various initialization options)
Privacy-focused (respects user opt-out)
This architecture makes the tracker both lightweight and flexible while providing comprehensive tracking capabilities. The modular design allows for easy maintenance and extension of functionality.
Would you like me to elaborate on any particular aspect of the architecture?