The plausible-tracker is a JavaScript library designed to interact with Plausible Analytics, a lightweight and open-source web analytics tool. I'll break down the design and architecture of the tracker, explaining how it works in detail.
Overview
The plausible-tracker is a small JavaScript library (~1KB) that can be embedded in web applications to track page views, custom events, and goals. It's designed to be highly customizable, allowing developers to tailor the tracking to their specific needs.
Design and Architecture
The tracker consists of two main components:
Tracker Core: This is the main module responsible for handling tracking events, sending data to Plausible Analytics, and managing the tracker's configuration.
Request Module: This module handles the actual HTTP requests to Plausible Analytics, encapsulating the logic for sending events and handling responses.
Tracker Core
The Tracker Core is the central component of the library. It's responsible for:
Initialization: The tracker is initialized with a set of default options, which can be overridden by the user. These options include the domain, API host, and other tracking settings.
Event Handling: The tracker provides methods for tracking page views, custom events, and goals. When an event is triggered, the tracker collects relevant data (e.g., URL, referrer, device width) and creates a payload to be sent to Plausible Analytics.
Automatic Page View Tracking: The tracker can automatically track page views by listening to pushstate and popstate events. This feature can be enabled or disabled depending on the user's needs.
Outbound Link Click Tracking: The tracker can also track clicks on outbound links by listening to click events on anchor tags.
Request Module
The Request Module is responsible for sending the tracking data to Plausible Analytics. It:
Creates the Payload: The Request Module takes the event data collected by the Tracker Core and creates a JSON payload to be sent to Plausible Analytics.
Sends the Request: The module uses the XMLHttpRequest API to send the payload to Plausible Analytics. It handles errors and provides a callback mechanism for the user to handle responses.
Key Concepts
Plausible Options: The tracker accepts a set of options that customize its behavior, such as the domain, API host, and tracking settings.
Event Payload: The tracker creates a JSON payload containing the event data, which is sent to Plausible Analytics.
Callback Functions: The tracker provides callback functions for the user to handle responses from Plausible Analytics.
Sequence Diagram
Here's a high-level sequence diagram illustrating the tracker's workflow:
mermaid
sequenceDiagram
participant User as "User"
participant Tracker as "Tracker Core"
participant Request as "Request Module"
participant Plausible as "Plausible Analytics"
User->>Tracker: Initialize tracker with options
Tracker->>Tracker: Set up event listeners (e.g., pushstate, popstate)
User->>Tracker: Trigger event (e.g., page view, custom event)
Tracker->>Tracker: Collect event data and create payload
Tracker->>Request: Send payload to Plausible Analytics
Request->>Plausible: Send HTTP request with payload
Plausible->>Request: Respond with success or error
Request->>Tracker: Handle response and call user callback
Tracker->>User: Call user callback with response data
In summary, the plausible-tracker is designed to be a lightweight and customizable library for interacting with Plausible Analytics. Its architecture consists of a Tracker Core and a Request Module, which work together to handle event tracking, payload creation, and request sending. The tracker provides a range of features, including automatic page view tracking, outbound link click tracking, and customizable event handling.