Skip to content

Commit ffd4900

Browse files
Merge pull request #60 from HarishGangula/release-5.3.0
Refactor telemetry library to TypeScript
2 parents c11fa15 + 15e518f commit ffd4900

File tree

93 files changed

+7696
-20593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+7696
-20593
lines changed

.eslintrc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
plugins: ['@typescript-eslint'],
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
],
8+
rules: {
9+
'@typescript-eslint/no-explicit-any': 'off',
10+
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }]
11+
},
12+
env: {
13+
browser: true,
14+
node: true,
15+
es2020: true
16+
}
17+
};

.github/workflows/npm-publish.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ jobs:
99
# Build and publish to npm registry
1010
build-and-publish:
1111
runs-on: ubuntu-latest
12-
defaults:
13-
run:
14-
working-directory: js
1512
steps:
1613
# Step 1: Checkout the source code from the tag
1714
- uses: actions/checkout@v3
1815

1916
# Step 2: Set up Node.js environment for building
20-
# Uses Node.js 16 which is stable and well-supported
21-
- name: Set up Node.js 16
17+
- name: Set up Node.js 22
2218
uses: actions/setup-node@v3
2319
with:
24-
node-version: '16'
20+
node-version: '22'
2521
cache: 'npm'
26-
cache-dependency-path: 'js/package-lock.json'
22+
cache-dependency-path: 'package-lock.json'
2723
registry-url: 'https://registry.npmjs.org'
2824
always-auth: true
2925

@@ -33,8 +29,8 @@ jobs:
3329
id: cache-npm
3430
uses: actions/cache@v3
3531
with:
36-
path: js/node_modules
37-
key: ${{ runner.os }}-npm-${{ hashFiles('js/package-lock.json') }}
32+
path: node_modules
33+
key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
3834
restore-keys: |
3935
${{ runner.os }}-npm-
4036

@@ -51,4 +47,4 @@ jobs:
5147
- name: Publish to npm
5248
env:
5349
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
54-
run: npm publish --access public
50+
run: npm publish --access public

.github/workflows/pull-request.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@ jobs:
1414
- name: Set up Node.js
1515
uses: actions/setup-node@v3
1616
with:
17-
node-version: '18'
17+
node-version: '22'
1818

1919
- name: Install dependencies
20-
run: npm install
21-
working-directory: js
20+
run: npm ci
2221

2322
- name: Build
2423
run: npm run build
25-
working-directory: js
2624

2725
- name: Run tests
2826
run: npm test
29-
working-directory: js
30-
continue-on-error: true
3127

3228
#- name: Install SonarQube scanner
3329
# run: npm install -g sonarqube-scanner
@@ -46,12 +42,10 @@ jobs:
4642
- name: Set up Node.js
4743
uses: actions/setup-node@v3
4844
with:
49-
node-version: '18'
45+
node-version: '22'
5046

5147
- name: Install dependencies
5248
run: npm install
53-
working-directory: js
5449

5550
- name: Run ESLint
5651
run: npm run lint
57-
working-directory: js

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
coverage
4+
.DS_Store

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.

js/.eslintrc.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

js/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)