|
| 1 | + |
| 2 | +# Sunbird Telemetry SDK (v4.0.0) |
| 3 | + |
| 4 | +A robust, isomorphic JavaScript/TypeScript library for generating and syncing Sunbird telemetry events. This version is a complete rewrite using modern TypeScript, replacing legacy dependencies with standard NPM packages. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- **Isomorphic Support**: Works seamlessly in both Browser and Node.js environments. |
| 9 | +- **Modern Stack**: Written in TypeScript with ES6+ features. |
| 10 | +- **Dependency Optimization**: Replaced `jquery`, `fingerprintjs2`, `md5` with: |
| 11 | + - Native `fetch` API (no jQuery dependency). |
| 12 | + - `@fingerprintjs/fingerprintjs` for browser fingerprinting. |
| 13 | + - `crypto-js` for hashing. |
| 14 | + - `ajv` for schema validation. |
| 15 | + - `ua-parser-js` for user agent parsing. |
| 16 | +- **Sticky Fingerprint**: Implements `localStorage` caching to ensure consistent device IDs across browser sessions. |
| 17 | +- **Type Safety**: Full TypeScript definitions included. |
| 18 | +- **Multiple Builds**: CommonJS (CJS), ES Modules (ESM), and Browser Global (IIFE). |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +npm install @project-sunbird/telemetry-sdk |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Browser (ES Modules / Bundlers) |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { $t } from '@project-sunbird/telemetry-sdk'; |
| 32 | + |
| 33 | +$t.initialize({ |
| 34 | + pdata: { id: 'my-app', ver: '1.0', pid: 'sunbird-portal' }, |
| 35 | + env: 'home', |
| 36 | + channel: 'in.ekstep', |
| 37 | + did: 'device-id', // Optional: Library will auto-generate if missing |
| 38 | + authtoken: 'your-auth-token', |
| 39 | + host: 'https://api.sunbird.org', |
| 40 | + endpoint: '/v1/telemetry' |
| 41 | +}); |
| 42 | + |
| 43 | +$t.start( |
| 44 | + {}, // Config overrides |
| 45 | + 'content-id', |
| 46 | + '1.0', |
| 47 | + { type: 'app', mode: 'play', pageid: 'home' } |
| 48 | +); |
| 49 | + |
| 50 | +$t.interact({ |
| 51 | + type: 'CLICK', |
| 52 | + id: 'play-button', |
| 53 | + pageid: 'home' |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +### Browser (Script Tag) |
| 58 | + |
| 59 | +The library exposes a global `Telemetry` object. It also aliases `window.$t` and `window.EkTelemetry` for backward compatibility. |
| 60 | + |
| 61 | +```html |
| 62 | +<script src="path/to/dist/index.global.js"></script> |
| 63 | +<script> |
| 64 | + $t.initialize({ ... }); |
| 65 | + $t.start(...); |
| 66 | +</script> |
| 67 | +``` |
| 68 | + |
| 69 | +### Node.js |
| 70 | + |
| 71 | +```javascript |
| 72 | +const { $t } = require('@project-sunbird/telemetry-sdk'); |
| 73 | + |
| 74 | +// Note: In Node.js, you should ideally provide a 'did' (Device ID) |
| 75 | +// or 'uid' (User ID) as fingerprinting relies on browser features. |
| 76 | +$t.initialize({ |
| 77 | + pdata: { id: 'my-service', ver: '1.0' }, |
| 78 | + env: 'backend', |
| 79 | + channel: 'in.ekstep', |
| 80 | + host: 'https://api.sunbird.org', |
| 81 | + batchsize: 10 |
| 82 | +}); |
| 83 | + |
| 84 | +$t.log({ |
| 85 | + type: 'api_access', |
| 86 | + level: 'INFO', |
| 87 | + message: 'API Request Received', |
| 88 | + params: [{ url: '/api/v1/user' }] |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +## Configuration |
| 93 | + |
| 94 | +| Property | Description | Default | |
| 95 | +| --- | --- | --- | |
| 96 | +| `pdata` | Producer Data (Required) | `{ id: "in.ekstep", ver: "1.0" }` | |
| 97 | +| `env` | Environment (Required) | `"contentplayer"` | |
| 98 | +| `channel` | Channel ID (Required) | `"in.ekstep"` | |
| 99 | +| `did` | Device ID | Browser fingerprint or UUID (Node) | |
| 100 | +| `uid` | User ID | `"anonymous"` | |
| 101 | +| `sid` | Session ID | `""` | |
| 102 | +| `batchsize` | Number of events to batch before syncing | `20` (Max 1000) | |
| 103 | +| `host` | API Host URL | `https://api.ekstep.in` | |
| 104 | +| `endpoint` | API Endpoint | `/data/v3/telemetry` | |
| 105 | +| `authtoken` | Authorization Token | `""` | |
| 106 | +| `enableValidation`| Validate events against schema | `false` | |
| 107 | +| `dispatcher` | Custom dispatcher object `{ dispatch: (event) => {} }` | `undefined` | |
| 108 | + |
| 109 | +**Note on Validation:** |
| 110 | +If `enableValidation` is set to `true`, the library validates events against the built-in Telemetry V3 schema. Events with missing or invalid schemas will be dropped. |
| 111 | + |
| 112 | +## Development |
| 113 | + |
| 114 | +### Scripts |
| 115 | + |
| 116 | +- `npm run build`: Build the library using `tsup` (generates `dist/`). |
| 117 | +- `npm run lint`: Lint source code using `eslint`. |
| 118 | +- `npm test`: Run unit tests using `vitest`. |
| 119 | + |
| 120 | +### Project Structure |
| 121 | + |
| 122 | +- `src/core`: Core telemetry logic (`Telemetry`, `TelemetrySyncManager`). |
| 123 | +- `src/services`: Services like `DeviceService` (fingerprinting) and `Validator` (schema). |
| 124 | +- `src/utils`: Utilities (`Dispatcher`, `Utils`, `DeviceInfo`). |
| 125 | +- `src/schema`: Telemetry JSON schemas. |
| 126 | + |
| 127 | +## Migration from v3.x |
| 128 | + |
| 129 | +1. **Remove jQuery**: Ensure your application handles network requests if you were relying on the SDK's jQuery dependency. The SDK now uses `fetch`. |
| 130 | +2. **Fingerprinting**: The library now uses `@fingerprintjs/fingerprintjs`. This might generate different device IDs than the old library. However, `localStorage` caching is implemented to maintain the ID once generated. |
| 131 | +3. **Imports**: If using ES modules, import `{ $t }` or `{ Telemetry }` instead of relying on globals. |
0 commit comments