flux-analytics-sdk 1.0.0 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -0
- package/package.json +2 -2
- package/src/Flux.js +10 -4
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Flux Analytics SDK (JS/Web)
|
|
2
|
+
|
|
3
|
+
This is the official JavaScript/Web SDK for Flux Analytics. It is designed to work seamlessly with the Flux Dashboard and Supabase backend, providing feature parity with the Flutter SDK.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install flux-analytics-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Initialize
|
|
14
|
+
Initialize the SDK at the root of your application (e.g., `index.js`, `App.js`, or `main.ts`).
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
import Flux from 'flux-analytics-sdk';
|
|
18
|
+
|
|
19
|
+
Flux.init(
|
|
20
|
+
'YOUR_APP_ID', // UUID (Application ID from Dashboard)
|
|
21
|
+
'YOUR_FIRM_ID' // UUID (Firm/Workspace ID)
|
|
22
|
+
);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
> **Note:** The SDK comes pre-configured with the default Flux Cloud credentials. You do not need to provide an API Key.
|
|
26
|
+
|
|
27
|
+
### 2. Track Events
|
|
28
|
+
Track any custom event with optional metadata.
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
// Simple event
|
|
32
|
+
Flux.track('button_clicked');
|
|
33
|
+
|
|
34
|
+
// Event with metadata
|
|
35
|
+
Flux.track('purchase_completed', {
|
|
36
|
+
item_id: 'sku_123',
|
|
37
|
+
price: 99.99,
|
|
38
|
+
currency: 'USD'
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 3. Identity Management
|
|
43
|
+
Link sessions to a specific user ID once they log in.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
// When user logs in
|
|
47
|
+
Flux.identify('user_123456');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Features & Technical Details
|
|
51
|
+
|
|
52
|
+
### 1. Automatic Session Management
|
|
53
|
+
- **Session Start**: Automatically tracks `session_start` when `Flux.init()` is called.
|
|
54
|
+
- **Session ID**: Generated (UUID v4) and persisted in memory for the duration of the page lifecycle.
|
|
55
|
+
- **Anonymous ID**: A persistent UUID is generated and stored in `localStorage` (`flux_anonymous_id`) to track unauthenticated users across sessions.
|
|
56
|
+
|
|
57
|
+
### 2. Offline Support (Queue System)
|
|
58
|
+
- Events are not sent immediately if the device is offline.
|
|
59
|
+
- **Storage**: Events are stored in `localStorage` under the key `flux_event_queue`.
|
|
60
|
+
- **Sync**: The SDK automatically listens for the `online` window event and flushes the queue when the connection is restored.
|
|
61
|
+
- **Retry Logic**: Failed requests (non-4xx/5xx) are kept in the queue for retry.
|
|
62
|
+
|
|
63
|
+
### 3. Automatic Metadata
|
|
64
|
+
Every event automatically includes the following metadata:
|
|
65
|
+
- `platform`: 'web'
|
|
66
|
+
- `os_version`: Browser/OS details
|
|
67
|
+
- `user_agent`: Raw User-Agent string
|
|
68
|
+
- `language`: Browser language (e.g., 'en-US')
|
|
69
|
+
- `resolution`: Screen resolution (e.g., '1920x1080')
|
|
70
|
+
- `timezone`: User's timezone
|
|
71
|
+
- `page_url`: Current URL
|
|
72
|
+
- `referrer`: Previous URL (document.referrer)
|
|
73
|
+
|
|
74
|
+
### 4. Attribution (Marketing Tracking)
|
|
75
|
+
The SDK automatically checks URL parameters for marketing campaigns.
|
|
76
|
+
- **Parameters**: Looks for `campaign_id` (or `utm_campaign`) and `click_id` (or `gclid`).
|
|
77
|
+
- **Persistence**: If found, these are stored and attached to the `app_install` (first visit) event and subsequent events.
|
|
78
|
+
|
|
79
|
+
### 5. API Compatibility
|
|
80
|
+
The SDK communicates directly with the Flux/Supabase backend using the following endpoints:
|
|
81
|
+
- **Events**: `POST /rpc/insert_event_v2` (High-performance ingestion)
|
|
82
|
+
- **Sessions**: `POST /sessions` (Upsert on `session_id`)
|
|
83
|
+
- **Installs**: `POST /installs` (Tracked only once per device via `localStorage` flag `flux_app_installed`)
|
|
84
|
+
|
|
85
|
+
## AI / LLM Context
|
|
86
|
+
If you are an AI Assistant integrating this SDK:
|
|
87
|
+
1. Ensure `Flux.init` is called as early as possible.
|
|
88
|
+
2. Use `Flux.trackScreen(screenName)` on route changes for SPA (Single Page Applications).
|
|
89
|
+
3. The `app_id` and `firm_id` MUST be valid UUIDs.
|
|
90
|
+
4. The SDK handles authentication automatically. You do NOT need to ask the user for an API Key.
|
package/package.json
CHANGED
package/src/Flux.js
CHANGED
|
@@ -14,7 +14,10 @@ const Flux = (function () {
|
|
|
14
14
|
|
|
15
15
|
// Base URL for API
|
|
16
16
|
let _baseUrl = 'https://cnpawrfdekifurtkdblw.supabase.co/rest/v1';
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
// PUBLIC ANON KEY (Safe to expose in client-side code, restricted by RLS)
|
|
19
|
+
const _defaultApiKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImNucGF3cmZkZWtpZnVydGtkYmx3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njk2NzgyOTQsImV4cCI6MjA4NTI1NDI5NH0.IiMIagP7Guy1SsiWF4OdDmz1X6EwPkT4tWCumQLZXhg';
|
|
20
|
+
let _apiKey = _defaultApiKey;
|
|
18
21
|
|
|
19
22
|
// Constants
|
|
20
23
|
const _queueKey = 'flux_event_queue';
|
|
@@ -313,11 +316,12 @@ const Flux = (function () {
|
|
|
313
316
|
|
|
314
317
|
// 2xx success, 409 conflict (duplicate) is also success
|
|
315
318
|
if (response.ok || response.status === 409) {
|
|
319
|
+
if (_debug) console.log(`%c[Flux SDK] ✓ Sent to ${endpoint}`, 'color: #10b981; font-weight: bold;');
|
|
316
320
|
return true;
|
|
317
321
|
} else {
|
|
318
|
-
if (_debug) console.error(`Flux Error: ${response.status} - ${response.statusText}`);
|
|
319
322
|
const text = await response.text();
|
|
320
|
-
if
|
|
323
|
+
// Always log errors even if debug is off, but make them prominent if debug is on
|
|
324
|
+
console.error(`%c[Flux SDK] ✗ Error: ${response.status} - ${response.statusText}`, 'color: #ef4444; font-weight: bold;', text);
|
|
321
325
|
return false;
|
|
322
326
|
}
|
|
323
327
|
|
|
@@ -429,7 +433,9 @@ const Flux = (function () {
|
|
|
429
433
|
created_at: new Date().toISOString()
|
|
430
434
|
};
|
|
431
435
|
|
|
432
|
-
if (_debug)
|
|
436
|
+
if (_debug) {
|
|
437
|
+
console.log(`%c[Flux SDK] ⚡ Event Tracked: ${eventName}`, 'color: #6366f1; font-weight: bold; font-size: 12px;', payload);
|
|
438
|
+
}
|
|
433
439
|
|
|
434
440
|
await _addToQueue('events', payload);
|
|
435
441
|
}
|