UNPKG

@trap_stevo/metrictide-client

Version:

A resilient, smart client designed to stream, store, and query metrics to and from a MetricTide analytics server. Whether you're tracking product usage, performance signals, or user behavior, MetricTide Client offers seamless integration, offline robustne

206 lines (150 loc) โ€ข 7.3 kB
# ๐ŸŒ MetricTide Client **Real-Time Metric Reporting Made Effortless** A resilient, smart client designed to stream, store, and query metrics to and from a MetricTide analytics server. Whether you're tracking product usage, performance signals, or user behavior, MetricTide Client offers seamless integration, offline robustness, and predictive insights out of the box. --- ## ๐Ÿš€ Features - ๐Ÿ“ก **MetricTide Server Integration** โ€“ Stream custom metrics to a MetricTide server in real time. - ๐Ÿ“ฆ **Offline-First Buffering** โ€“ Automatically stores unsent metrics and syncs them once connectivity is restored. - ๐Ÿง  **Device-Aware Identity** โ€“ Assigns a consistent, device-specific metric identifier. - ๐Ÿ”„ **Auto Syncing** โ€“ Periodically flushes stored metrics to the backend with retry logic. - ๐Ÿ” **Flexible Querying** โ€“ Retrieve historical, real-time, and aggregate analytics. - ๐Ÿ” **Session-Based Communication** โ€“ Securely transmit metrics through authenticated, session-aware channels. - ๐Ÿ”ง **Custom Gateways** โ€“ Override transport logic with your own network layer. --- ## โš™๏ธ System Requirements | Requirement | Version | |----------------|-----------------------------| | **Browser** | Chrome โ‰ฅ 91, Firefox โ‰ฅ 90 | | **JavaScript** | ES2020+ | | **npm** | โ‰ฅ 9.x (recommended) | | **OS** | Windows, macOS, Linux | --- ## ๐Ÿ› ๏ธ Configuration ### `MetricTideClient` Options | Option | Type | Default | Description | |----------------------------|-------------|-------------|-------------| | `connectionURL` | `string` | `""` | URL to check server availability. | | `baseURL` | `string` | `""` | API base path for reporting and querying metrics. | | `deviceIDPrefix` | `string` | `"MT-"` | Prefix used for the generated metric device ID. | | `flushInterval` | `number` | `10000` | How often (in ms) to attempt syncing stored metrics. | | `offlineFirst` | `boolean` | `false` | Enables buffering and deferred syncing when offline. | | `metricRequester` | `function` | `null` | Optional custom request logic. | | `metricGateway` | `function` | `fetch` | Network method used for transport. | | `metricLinkPairPath` | `string` | `"/metric-device/pair"` | Endpoint for authenticating client device. | | `metricLinkConfigurations` | `object` | `{}` | Enables optional session-based communication. | | `metricCacheConfigurations` | `object` | `{}` | Storage configuration (name, version, store list, etc). | | `metricStatusCheckerOptions`| `object` | `{}` | Options for availability checking logic. | --- ## ๐Ÿ“˜ API Specifications ### ๐ŸŽฏ Tracking | Method | Description | |--------|-------------| | `track(domain, metric, options?)` | Records a metric and attempts immediate or deferred transmission. | | `sync()` | Manually triggers an attempt to flush all stored metrics. | | `activateOnlineListener()` | Initiates the client's online event for metric syncing. | | `activateSyncInterval()` | Initiates metric flushing per flush interval. | | `destroyOnlineListener()` | Stops listening to the client's online event preventing metric syncing upon client connection. | | `destroySyncInterval()` | Stops metric flushing. | | `destroy()` | Stops syncing behavior and removes internal listeners. | | `getDeviceID()` | Returns the consistent, client-generated metric device ID. | ### ๐Ÿ“„ Querying | Method | Description | |--------|-------------| | `getMetrics(filter)` | Retrieves current metrics from the server. | | `getMetricsSince(interval, source, filter)` | Gets metrics from a time window (e.g., `"5m"`, `"1d"`). | | `getMetricsBetween(start, end, source, filter)` | Retrieves metrics between two timestamps. | | `getStoredMetrics(filter)` | Loads metrics that were saved but not yet synced. | | `getMetricsFromDomains(domains, options)` | Queries multiple domains in a single request. | | `getMetricsInDomain(domain, options)` | Queries metrics within a specific domain. | | `getMetricByID(id)` | Retrieves a metric by its unique ID. | | `updateMetric(id, updates)` | Updates an existing metric. | ### ๐Ÿ“Š Aggregation & Prediction | Method | Description | |--------|-------------| | `getAggregate(name)` | Returns aggregate stats for a named metric. | | `getAggregateByType(type)` | Retrieves grouped aggregates by type. | | `groupByTag(tagKey)` | Groups metrics based on a tag. | | `getTimeSeries(name, options)` | Returns a time series view of a metric. | | `predictMetric(name, config)` | Generates a short-term forecast for a metric. | ### ๐Ÿงน Cleanup | Method | Description | |--------|-------------| | `clearMetricsInDomain(domain, options)` | Removes all metrics associated with a given domain. | | `clearAllMetrics()` | Removes all metrics globally. | --- ## ๐Ÿ” Session Communication (Optional) Enable secure, session-aware metric transport using the `metricLinkConfigurations` object: ```js const client = new MetricTideClient({ baseURL : "https://metrics.example.com", metricLinkPairPath : "/devices/pair", metricLinkConfigurations : { enabled : true, options : { persistSessionKey : false, persistSessionID : false, headers : { Authorization : "Bearer YOUR_TOKEN" } } } }); ``` --- ## ๐Ÿ“ฆ Installation ```bash npm install @trap_stevo/metrictide-client ``` --- ## โœจ Basic Usage ```js import HUDMetricTide from "@trap_stevo/metrictide-client"; const client = new HUDMetricTide({ baseURL : "https://metrics.example.com", connectionURL : "https://metrics.example.com/ping", offlineFirst : true, flushInterval : 8000 }); client.track("page_view", { value : 1, metadata : { route : "/dashboard" }, tags : ["navigation"] }); ``` --- ## ๐Ÿ” Example: Buffered Tracking ```js await client.track("interaction", { value : 1, metadata : { type : "click" }, tags : ["button", "ui"] }); ``` If offline mode is enabled, this metric will be held until syncing becomes possible. --- ## ๐Ÿ“Š Example: Fetching Recent Activity ```js const recent = await client.getMetricsSince("10m"); console.log("Recent Metrics:", recent); ``` --- ## ๐Ÿ”ฎ Example: Forecasting Engagement ```js const prediction = await client.predictMetric("engagement_score", { interval : "5m", range : "1h", stepsAhead : 3 }); console.log("Predicted Engagement:", prediction); ``` --- ## ๐Ÿ“Œ Example: Clearing a Domain ```js await client.clearMetricsInDomain("video_player"); ``` --- ## ๐Ÿ“ก Server Integration Designed to interface seamlessly with [MetricTide](https://www.npmjs.com/package/@trap_stevo/metrictide), enabling full tracking, querying, and forecasting. --- ## ๐Ÿ“œ License See [LICENSE.md](./LICENSE.md) for details.