@project-sunbird/telemetry-sdk 1.3.0 → 2.0.0

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 CHANGED
@@ -1,915 +1,131 @@
1
1
 
2
- ![](https://travis-ci.org/manjudr/telemetry-lib.svg?branch=master)
3
- [![codecov](https://codecov.io/gh/manjudr/telemetry-lib/branch/master/graph/badge.svg)](https://codecov.io/gh/manjudr/telemetry-lib)
4
-
5
-
6
-
7
- ## Need
8
-
9
- The purpose of a standalone JS library for telemetry is to facilitate capture and distribution of telemetry data by users who would like to use their own apps, content players or portals.
10
-
11
- We chose to use a JS library for the following reasons:
12
-
13
- * All the telemetry events that are generated and synced to the server have the same format (field data types and time zone value)
14
-
15
- * It is easy to upgrade to new versions, in case of major changes in telemetry
16
-
17
- * There is effortless backward compatibility, as changes are handled within the telemetry library. Any upgrade of the telemetry library does not require code changes in the content
18
-
19
- * There are reduced number of API calls
20
-
21
- * There are simple API methods to generate the complete telemetry event as only the required fields are passed
22
-
23
- ## Prerequisites
24
-
25
- The following are prerequisites to use or integrate the JS library:
26
-
27
- * JQuery library should be available
28
-
29
- * Valid Authtoken and Key to make API calls
30
-
31
- * The [telemetry.min.js](https://github.com/project-sunbird/project-sunbird.github.io/blob/dev/pages/developer-docs/telemetry/other_files/telemetry.min.js){:target="_blank"} file
32
-
33
- **Note:** For details on generating and using the Authtoken and Key, refer to the section
34
-
35
- * Device ID value
36
-
37
- **Note:** For details on how to get the device ID value, refer to [website](https://android-developers.googleblog.com/2011/03/identifying-app-installations.html){:target="_blank"}
38
-
39
- ## Configure
40
-
41
- This JS library helps to generate telemetry events. These events sync to the server or data-pipeline in a batch as defined in the configuration. To log telemetry events, the user has to call the start method by passing the configuration along with other parameters.
42
-
43
- **Note:** All telemetry events sync only to the server or data-pipeline, when connected to the Internet.
44
-
45
- Telemetry events are generated based on the configuration of the telemetry library.
46
-
47
- **Required Configuration (Context)**
48
-
49
- <table>
50
-
51
- <thead>
52
- <tr>
53
- <th>Property</th>
54
- <th>Description</th>
55
- <th>Required</th>
56
- <th>Default Value</th>
57
- </tr>
58
- </thead>
59
- <tbody>
60
- <tr>
61
- <td>pdata</td>
62
- <td>Producer data. It is an object containing id, version and pid.</td>
63
- <td>true</td>
64
- <td>Defaults to genie ex : {"id": "genie", "ver": "6.5.2567" pid:""}</td>
65
- </tr>
66
- <tr>
67
- <td>channel</td>
68
- <td>It is an string containing unique channel name.</td>
69
- <td>true</td>
70
- <td>Defaults to in.ekstep</td>
71
- </tr>
72
- <tr>
73
- <td>uid</td>
74
- <td>It is an string containing user id.</td>
75
- <td>true</td>
76
- <td>defaults to "anonymous"</td>
77
- </tr>
78
- <tr>
79
- <td>did</td>
80
-    <td>It is an string containing unique device id.
81
- <ul><li>To generate did value for android refer to <a href ='https://android-developers.googleblog.com/2011/03/identifying-app-installations.html'>here</a> ANDROID_ID is generally used for mobiles</li>
82
- <li>To generate did value for web client refer <a href = 'https://github.com/Valve/fingerprintjs2'>here</a>. If consumer is not sending any did value then by default library will generate did using <a href ='https://github.com/Valve/fingerprintjs2'>fingerPrintJs2</a>.</li><li>For server side it's mandtory to pass did value</li></ul>
83
- </td>
84
- <td>true</td>
85
- <td>Default to <a href="https://github.com/Valve/fingerprintjs2">fingerPrintjs2</a>(Note: Only for web client)</td>
86
- </tr>
87
- <tr>
88
- <td>authtoken</td>
89
- <td>It is an string containing consumer token to access the API</td>
90
- <td>true</td>
91
- <td></td>
92
- </tr>
93
- <tr>
94
- <td>Property</td>
95
- <td>Description</td>
96
- <td>Required</td>
97
- <td>Default Value</td>
98
- </tr>
99
- <tr>
100
- <td>env</td>
101
- <td>It is an string containing Unique environment where the event has occurred</td>
102
- <td>true</td>
103
- <td>defaults to "ContentPlayer"</td>
104
- </tr>
105
- </tbody>
106
- </table>
107
-
108
- **Additional Configuration**
109
-
110
- <table>
111
- <tr>
112
- <td>Property</td>
113
- <td>Description</td>
114
- <td>Required</td>
115
- <td>Default Value</td>
116
- </tr>
117
- <tr>
118
- <td>sid</td>
119
- <td>It is an string containing user session id.</td>
120
- <td>optional</td>
121
- <td> </td>
122
- </tr>
123
- <tr>
124
- <td>batchsize</td>
125
- <td>It is an int containing number of events count to sync at a time. Can be configured from min value 10 to max value 1000.</td>
126
- <td>optional</td>
127
- <td>defaults to 20</td>
128
- </tr>
129
- <tr>
130
- <td>mode</td>
131
- <td>It is an string which defines to identify preview used by the user to play/edit/preview.</td>
132
- <td>optional</td>
133
- <td>defaults to "play"</td>
134
- </tr>
135
- <tr>
136
- <td>host</td>
137
- <td>It is an string containing API endpoint host.</td>
138
- <td>optional</td>
139
- <td>defaults to "https://api.ekstep.in"</td>
140
- </tr>
141
- <tr>
142
- <td>endpoint</td>
143
- <td>It is an string containing API endpoint. Please don't change default value. Update this only when the data is proxied</td>
144
- <td>optional</td>
145
- <td>Defaults to "/v3/telemetry"</td>
146
- </tr>
147
- <tr>
148
- <td>tags</td>
149
- <td>It is an array. It can be used to tag devices so that summaries/metrics can be derived via specific tags. Helpful during analysis</td>
150
- <td>optional</td>
151
- <td>Defaults to []</td>
152
- </tr>
153
- <tr>
154
- <td>cdata</td>
155
- <td>It is an array. Correlation data. Can be used to correlate multiple events. Generally used to track user flow</td>
156
- <td>optional</td>
157
- <td>Defaults to []</td>
158
- </tr>
159
- <tr>
160
- <td>dispatcher</td>
161
- <td></td>
162
- <td></td>
163
- <td></td>
164
- </tr>
165
- </table>
166
-
167
- **Sample:**
168
-
169
- <pre>
170
- {
171
- "pdata": {
172
- "id": "genie",
173
- "ver": "6.5.2567",
174
- "pid": ""
175
- },
176
-
177
- "env": "ContentPlayer",
178
- "channel": "XXXX",
179
- "did": "20d63257084c2dca33f31a8f14d8e94c0d939de4",
180
- "authtoken": "XXXX",
181
- "uid": "anonymous",
182
- "sid": "85e8a2c8-bb8e-4666-a21b-c29ec590d740",
183
- "batchsize": 20,
184
- "mode": "play",
185
- "host": "XXXX",
186
- "endpoint": "/v3/telemetry",
187
- "tags": [],
188
- "cdata": []
189
- }
190
- </pre>
191
-
192
- **Dispatcher:**
193
-
194
- User can define custom dispatcher to override the default functionality of telemetry sync. By default telemetry events will send to default server/host. User can override this default functionality by defining his own "dispatcher" object to handle telemetry events.
195
-
196
- <pre>
197
- var customDispatcher = {
198
- dispatch: function(event){
199
- // User defined logic to send telemetry to server or store locally etc..
200
- }
201
- };
202
- </pre>
203
-
204
- Send this object as dispatcher in the above sample configuration ("dispatcher":customDispatcher).
205
-
206
- ## How to use telemetry JS library
207
-
208
- Download the telemetry-sdk npm module from [here](https://www.npmjs.com/package/@project-sunbird/telemetry-sdk)
209
-
210
- <pre>
211
- npm i @project-sunbird/telemetry-sdk
212
- </pre>
213
-
214
- **Example:**
215
-
216
- <pre>
217
- $t = require('@project-sunbird/telemetry-sdk');
218
- $t.start(config, contentId, contentVer,data, options);
219
- </pre>
220
-
221
-
222
- To use the telemetry JS libraries, add the following to your HTML/application. The file path is a relative path, for example; assets/js to the associated files within the html content.
223
-
224
- <pre>
225
- &#x3C;!-- External Libraries --&#x3E;
226
- &#x3C;script src=&#x22;[relative_path]/jquery.min.js&#x22;&#x3E;&#x3C;/script&#x3E;
227
-
228
- &#x3C;!-- Telemetry JS library --&#x3E;
229
- &#x3C;script src=&#x22;[relative_path]/telemetry.min.js&#x22;&#x3E;&#x3C;/script&#x3E;
230
- &#x3C;script src=&#x22;[relative_path]/auth-token-generator.min.js&#x22;&#x3E;&#x3C;/script&#x3E;
231
- &#x3C;script&#x3E;
232
- function init() {
233
- // Generate auth token
234
- // Key: Partner generated key
235
- // secret: partner secret value
236
- let token = AuthTokenGenerate.generate(key, secret);
237
- config.authToken = token;
238
- let startEdata = {};
239
- let options = {};
240
- $t.start(config, &#x22;content_id, &#x22;contetn_ver&#x22;, startEdata, options );
241
- }
242
- init()
243
- &#x3C;/script&#x3E;
244
- </pre>
245
-
246
-
247
-
248
-
249
- ## Telemetry API methods
250
-
251
- Every API method has an associated event. The following API methods log details of the associated telemetry event.
252
-
253
- * [Start](developer-docs/telemetry/jslibrary/#start) - This method initializes capture of telemetric data associated to the start of user action
254
-
255
- * [Impression](developer-docs/telemetry/jslibrary/#impression) - This method is used to capture telemetry for user visits to a specific page.
256
-
257
- * [Interact](developer-docs/telemetry/jslibrary/#interact) - This method is used to capture user interactions on a page. For example, search, click, preview, move, resize, configure
258
-
259
- * [Assess ](developer-docs/telemetry/jslibrary/#access)- This method is used to capture user assessments that happen while playing content.
260
-
261
- * [Response](developer-docs/telemetry/jslibrary/#response) - This method is used to capture user responses. For example; response to a poll, calendar event or a question.
262
-
263
- * [Interrupt](developer-docs/telemetry/jslibrary/#interrupt) - This method is used to capture  interrupts triggered during user activity. For example;  mobile app sent to background, call on the mobile, etc.
264
-
265
- * [End](developer-docs/telemetry/jslibrary/#end) - This method is used to capture closure after all the activities are completed
266
-
267
- * [Feedback](developer-docs/telemetry/jslibrary/#feedback) - This method is used to capture user feedback
268
-
269
- * [Share](developer-docs/telemetry/jslibrary/#share) - This method is used to capture everything associated with sharing. For example; Share content, telemetry data, link, file etc.
270
-
271
- * [Audit](developer-docs/telemetry//jslibrary/#audit) - This method is used when an object is changed to know previous and current state. This includes lifecycle changes as well.
272
-
273
- * [Error](developer-docs/telemetry/jslibrary/#error) - This method is used to capture when users face an error
274
-
275
- * [Heartbeat](developer-docs/telemetry/jslibrary/#heartbeat) - This method is used to know is process is running or not.
276
-
277
- * [Log](developer-docs/telemetry/jslibrary/#log) - This method is used to capture generic logging of events. For example; capturing logs for API calls, service calls, app updates etc.
278
-
279
- * [Search](developer-docs/telemetry/jslibrary/#search) - This method is used to capture the search state i.e. when search is triggered for content, item, assets etc.
280
-
281
- * [Metrics](developer-docs/telemetry/jslibrary/#metrics) - Service business metrics (also accessible via health API)
282
-
283
- * [Summary](developer-docs/telemetry/jslibrary/#summary) - Summary event
284
-
285
- * [Exdata](developer-docs/telemetry/jslibrary/#exdata) - This method is used as a generic wrapper event to capture encrypted or serialized data
286
-
287
-
288
-
289
-
290
- ### Start
291
-
292
- This API is used to log telemetry when users view content or initiate game play
293
-
294
- <pre>
295
- start: function(config, contentId, contentVer, data, options) { }
296
- </pre>
297
-
298
- Request Arguments:
299
-
300
- <pre>
301
- let config = Object; // Telemetry Configurations
302
- let contentId = String; //Required. Id of the content
303
- let contentVer = String; //Required. Version of the content. Defaults to "1.0"
304
- let data = { // Required. event data
305
- "type": String, //Required. app, session, editor, player, workflow, assessment
306
- "mode": "", //Required. mode of preview: preview, edit or play
307
- "stageid": "" //Required. stage id where the play has been initiated
308
- };
309
- let options = { // Optional
310
- context: {}, // To override the existing context
311
- object: {}, // To override the existing object
312
- actor: {}, // To override the existing actor
313
- tags: {}, // To override the existing tags
314
- runningEnv: "server" // It can be either client or server
315
- };
316
-
317
- </pre>
318
-
319
- ### Impression
320
-
321
- This API is used to log telemetry when users visit a specific page.
322
-
323
- <pre>
324
- impression: function(data, options) { }
325
- </pre>
326
-
327
- Request Arguments:
328
-
329
- <pre>
330
-
331
- let data = { // Required
332
- "type": String, //Required. Impression type (list, detail, view, edit, workflow, search)
333
- "subtype": String, //Optional. Additional subtype. "Paginate", "Scroll"
334
- "pageid": String, //Required. Unique page id
335
- "itype": "", // type of interaction - SWIPE, SCRUB (fast forward using page thumbnails) or AUTO
336
- "stageto": "" // game level, stage of page id to which the navigation was done
337
- };
338
- </pre>
339
-
340
- <pre>
341
- let options = { // Optional
342
- context: {}, // To override the existing context
343
- object: {}, // To override the existing object
344
- actor: {}, // To override the existing actor
345
- tags: {}, // To override the existing tags
346
- runningEnv: "server" // It can be either client or server
347
- };
348
- </pre>
349
-
350
-
351
- ### Interact
352
-
353
- This API is used to log telemetry of user interactions on the page. For example, search, click, preview, move, resize, configure
354
-
355
- <pre>
356
- interact: function(data, options) { }
357
- </pre>
358
-
359
- Request Arguments:
360
-
361
- <pre>
362
-
363
- let data = { // Required
364
- "type": "", // Required. Type of interaction TOUCH,DRAG,DROP,PINCH,ZOOM,SHAKE,ROTATE,SPEAK,LISTEN,WRITE,DRAW,START,ENDCHOOSE,ACTIVATE,SHOW,HIDE,SCROLL,HEARTBEAT,OTHER
365
- "subtype": "", // Optional. Additional types for a global type. For ex: for an audio the type is LISTEN and thesubtype can be one of PLAY,PAUSE,STOP,RESUME,END
366
- "id": "", // Required. Resource (button, screen, page, etc) id on which the interaction happened - use systemidentifiers when reporting device events
367
- "pageid": "", // Optional. Stage or page id on which the event happened
368
- "extra": { // Optional. Extra attributes for an interaction
369
- "pos": [{ "x": , "y": , "z": }], // Array of positional attributes. For ex: Drag and Drop has two positional attributes. One where the drag has started and the drop point
370
- "values": [], // Array of values, e.g. for timestamp of audio interactions
371
- "tid": "", // When interaction is between multiple resources, (e.g. drag and drop) - target resource id
372
- "uri": "" // Unique external resource identifier if any (for recorded voice, image, etc.)
373
- }
374
- };
375
- </pre>
376
- <pre>
377
- let options = { // Optional
378
- context: {}, // To override the existing context
379
- object: {}, // To override the existing object
380
- actor: {}, // To override the existing actor
381
- tags: {}, // To override the existing tags
382
- runningEnv: "server" // It can be either client or server
383
- };
384
- </pre>
385
-
386
- ### Assess
387
-
388
- This API is used to log telemetry of assessments that have occured when the user is viewing content
389
-
390
- <pre>
391
- assess: function(data, options) { }
392
- </pre>
393
-
394
- Request Arguments:
395
-
396
- <pre>
397
- let QUESTION = {
398
- "id": "", // unique assessment question id. its an required property.
399
- "maxscore", // user defined score to this assessment/question.
400
- "exlength": , // expected time (decimal number) in seconds that ideally child should take
401
- "params": [ // Array of parameter tuples
402
- { "id": "value" } // for ex: if var1 is substituted with 5 apples the parameter is {"var1":"5"}
403
- ],
404
- "uri": "", // Unique external resource identifier if any (for recorded voice, image, etc.)
405
- "desc": "short description",
406
- "title": "title",
407
- "mmc": [], // User defined missing micros concepts
408
- "mc": [] // micro concepts list
409
- }
410
-
411
- let data = { //Required
412
- "item": QUESTION, // Required. Question Data
413
- "pass": "", // Required. Yes, No. This is case-sensitive. default value: No.
414
- "score": "", // Required. Evaluated score (Integer or decimal) on answer(between 0 to 1), default is 1 if pass=YES or 0 if pass=NO.
415
- "resvalues": [{ "id": "value" }], // Required. Array of key-value pairs that represent child answer (result of this assessment)
416
- "duration": "" // Required. time taken (decimal number) for this assessment in seconds
417
- };
418
- </pre>
419
- <pre>
420
- let options = { //Optional
421
- context: {}, // To override the existing context
422
- object: {}, // To override the existing object
423
- actor: {}, // To override the existing actor
424
- tags: {}, // To override the existing tags
425
- runningEnv: "server" // It can be either client or server
426
- };
427
- </pre>
428
-
429
- ### Response
430
-
431
- This API is used to log telemetry of user response. For example; Responded to assessments.
432
-
433
- <pre>
434
- response: function(data, options) { }
435
- </pre>
436
-
437
- Request Arguments:
438
-
439
- <pre>
440
- let TARGET = {
441
- "id": "", // Required. unique id for the target
442
- "ver": "", // Required. version of the target
443
- "type": "", // Required. Type of the target
444
- "parent": {
445
- "id": "", // Optional. parent id of the object
446
- "type": "" // Optional. parent type of the object. Required if parentid is present.
447
- }
448
- };
449
-
450
- let data = { // Required
451
- "target": TARGET, // Required. Target of the response
452
- "qid": "", // Required. Unique assessment/question id
453
- "type": "", // Required. Type of response. CHOOSE, DRAG, SELECT, MATCH, INPUT, SPEAK, WRITE
454
- "values": [{ "key": "value" }] // Required. Array of response tuples. For ex: if lhs option1 is matched with rhs optionN - [{"lhs":"option1"}, {"rhs":"optionN"}]
455
- };
456
- </pre>
457
- <pre>
458
- let options = { // Optional
459
- context: {}, // To override the existing context
460
- object: {}, // To override the existing object
461
- actor: {}, // To override the existing actor
462
- tags: {}, // To override the existing tags
463
- runningEnv: "server" // It can be either client or server
464
- };
465
- </pre>
466
-
467
- ### Interrupt
468
-
469
- This API is used to log telemetry for any interruptions that have occurred when a user is viewing content or playing games. For example; screen lock, incoming call, etc.
470
-
471
- <pre>
472
- interrupt: function(data, options) { }
473
- </pre>
474
-
475
- Request Arguments:
476
-
477
- <pre>
478
- let data = { //Required
479
- "type": "", // Required. Type of interuption
480
- "pageid": "", // Optional. Current Stage/Page unique id on which interuption occured
481
- "eventid": "" // Optional. unique event ID
482
- };
483
- </pre>
484
-
485
- <pre>
486
- let options = { // Optional
487
- context: {}, // To override the existing context
488
- object: {}, // To override the existing object
489
- actor: {}, // To override the existing actor
490
- tags: {}, // To override the existing tags
491
- runningEnv: "server" // It can be either client or server
492
- };
493
- </pre>
494
-
495
-
496
-
497
- ### Feedback
498
-
499
- This API is used to log telemetry of feedback provided by the user.
500
-
501
- <pre>
502
- // To log content start/play event
503
- feedback: function(data, options) { }
504
- </pre>
505
-
506
- Request Arguments:
507
-
508
- <pre>
509
- let data = { // Required
510
- "contentId": "", // Required. Id of the content
511
- "rating": 3, // Optional. Numeric score (+1 for like, -1 for dislike, or 4.5 stars given in a rating)
512
- "comments": "User entered feedback" // Optional. Text feedback (if any)
513
- };
514
- </pre>
515
-
516
- <pre>
517
- let options = { // Optional
518
- context: {}, // To override the existing context
519
- object: {}, // To override the existing object
520
- actor: {}, // To override the existing actor
521
- tags: {}, // To override the existing tags
522
- runningEnv: "server" // It can be either client or server
523
- };
524
- </pre>
525
-
526
- ### Share
527
-
528
- This API is used to log telemetry when a user shares any content with other users.
529
-
530
- <pre>
531
- // To log content start/play event
532
- share: function(data, options) { }
533
- </pre>
534
-
535
- Request Arguments:
536
-
537
- <pre>
538
- let data = { // Required
539
- "dir": "", // In/Out
540
- "type": "", // File/Link/Message
541
- "items": [{ // Required. array of items shared
542
- "obj": {
543
- "id": "",
544
- "type": "",
545
- "ver": ""
546
- },
547
- "params": [
548
- { "key": "value" }
549
- ],
550
- "origin": { // Origin of the share file/link/content
551
- "id": "", // Origin id
552
- "type": "" // Origin type
553
- },
554
- "to": {
555
- "id": "",
556
- "type": ""
557
- }
558
- }]
559
- };
560
- </pre>
561
- <pre>
562
- let options = { // Optional
563
- context: {}, // To override the existing context
564
- object: {}, // To override the existing object
565
- actor: {}, // To override the existing actor
566
- tags: {}, // To override the existing tags
567
- runningEnv: "server" // It can be either client or server
568
- };
569
-
570
- </pre>
571
-
572
- ### Audit
573
-
574
- This API is used to log telemetry when an object is changed. This includes life-cycle changes as well.
575
-
576
- <pre>
577
- audit: function(data, options) { }
578
- </pre>
579
-
580
- Request Arguments:
581
-
582
- <pre>
583
- let data = { // Required
584
- "edata": {
585
- "props": [""], // Updated properties
586
- "state": "", // Optional. Current state
587
- "prevstate": "" // Optional. Previous state
588
- }
589
- };
590
- </pre>
591
- <pre>
592
- let options = { // Optional
593
- context: {}, // To override the existing context
594
- object: {}, // To override the existing object
595
- actor: {}, // To override the existing actor
596
- tags: {}, // To override the existing tags
597
- runningEnv: "server" // It can be either client or server
598
- };
599
- </pre>
600
-
601
- ### Error
602
-
603
- This API is used to log telemetry of any error that has occurred when a user is viewing content or playing games.
604
-
605
- <pre>
606
- error: function(error, options) { }
607
- </pre>
608
-
609
- Request Arguments:
610
-
611
- <pre>
612
- let error = { // Required
613
- "err": "", // Required. Error code
614
- "errtype": "", // Required. Error type classification - "SYSTEM", "MOBILEAPP", "CONTENT"
615
- "stacktrace": "", // Required. Detailed error data/stack trace
616
- };
617
- </pre>
618
-
619
- <pre>
620
- let options = { // Optional
621
- context: {}, // To override the existing context
622
- object: {}, // To override the existing object
623
- actor: {}, // To override the existing actor
624
- tags: {}, // To override the existing tags
625
- runningEnv: "server" // It can be either client or server
626
- };
627
- </pre>
628
-
629
- ### Heartbeat
630
-
631
- This API is used to log telemetry for heartbeat event to denote that the process is running.
632
-
633
- <pre>
634
- heartbeat: function(data, options) { }
635
- </pre>
636
-
637
- Request Arguments:
638
-
639
- <pre>
640
- let data = { // Required
641
- "edata": {}
642
- }
643
- </pre>
644
-
645
- <pre>
646
- let options = { // Optional
647
- context: {}, // To override the existing context
648
- object: {}, // To override the existing object
649
- actor: {}, // To override the existing actor
650
- tags: {}, // To override the existing tags
651
- runningEnv: "server" // It can be either client or server
652
- };
653
-
654
- </pre>
655
-
656
- ### Log
657
-
658
- This API is used to log telemetry of generic log events. For example; API calls, service calls, app updates, etc.
659
-
660
- <pre>
661
- log: function(data, options) { }
662
- </pre>
663
-
664
- Request Arguments:
665
-
666
- <pre>
667
- let data = { // Required
668
- "type": "", // Required. Type of log (system, process, api_access, api_call, job, app_update etc)
669
- "level": "", // Required. Level of the log. TRACE, DEBUG, INFO, WARN, ERROR, FATAL
670
- "message": "", // Required. Log message
671
- "params": [{ "key": "value" }] // Optional. Additional params in the log message
672
- };
673
- </pre>
674
-
675
- <pre>
676
- let options = { // Optional
677
- context: {}, // To override the existing context
678
- object: {}, // To override the existing object
679
- actor: {}, // To override the existing actor
680
- tags: {}, // To override the existing tags
681
- runningEnv: "server" // It can be either client or server
682
- };
683
- </pre>
684
-
685
- ### Search
686
-
687
- This API is used to log telemetry when a user triggers a search for any content, item or asset
688
-
689
- <pre>
690
- search: function(data, options) { }
691
- </pre>
692
-
693
- Request Arguments:
694
-
695
- <pre>
696
- let data = { // Required
697
- "type": "", // Required. content, assessment, asset
698
- "query": "", // Required. Search query string
699
- "filters": {}, // Optional. Additional filters
700
- "sort": {}, // Optional. Additional sort parameters
701
- "correlationid": "", // Optional. Server generated correlation id (for mobile app's telemetry)
702
- "size": 333, // Required. Number of search results
703
- "topn": [{}] // Required. top N (configurable) results with their score
704
- };
705
- </pre>
706
- <pre>
707
- let options = { // Optional
708
- context: {}, // To override the existing context
709
- object: {}, // To override the existing object
710
- actor: {}, // To override the existing actor
711
- tags: {}, // To override the existing tags
712
- runningEnv: "server" // It can be either client or server
713
- };
714
- </pre>
715
-
716
- ### Metrics
717
-
718
- This API is used to log telemetry for service business metrics (also accessible via health API).
719
-
720
- <pre>
721
- metrics: function(data, options) { }
722
- </pre>
723
-
724
- Request Arguments:
725
-
726
- <pre>
727
- let data = { // Required
728
- "edata": {
729
- "metric1": Int,
730
- "metric2": Int
731
- /// more metrics, each is a key value
732
- }
733
- };
734
- </pre>
735
-
736
- <pre>
737
- let options = { // Optional
738
- context: {}, // To override the existing context
739
- object: {}, // To override the existing object
740
- actor: {}, // To override the existing actor
741
- tags: {}, // To override the existing tags
742
- runningEnv: "server" // It can be either client or server
743
- };
744
- </pre>
745
-
746
- ### Summary
747
-
748
- This API is used to log telemetry summary event
749
-
750
- <pre>
751
- summary: function(data, options) { }
752
- </pre>
753
-
754
- Request Arguments:
755
-
756
- <pre>
757
- let data = { // Required
758
- "edata": {
759
- "type": "", // Required. Type of summary. Free text. "session", "app", "tool" etc
760
- "mode": "", // Optional.
761
- "starttime": Long, // Required. Epoch Timestamp of app start. Retrieved from first event.
762
- "endtime": Long, // Required. Epoch Timestamp of app end. Retrieved from last event.
763
- "timespent": Double, // Required. Total time spent by visitor on app in seconds excluding idle time.
764
- "pageviews": Long, // Required. Total page views per session(count of CP_IMPRESSION)
765
- "interactions": Long, // Required. Count of interact events
766
- "envsummary": [{ // Optional
767
- "env": String, // High level env within the app (content, domain, resources, community)
768
- "timespent": Double, // Time spent per env
769
- "visits": Long // count of times the environment has been visited
770
- }],
771
- "eventssummary": [{ // Optional
772
- "id": String, // event id such as CE_START, CE_END, CP_INTERACT etc.
773
- "count": Long // Count of events.
774
- }],
775
- "pagesummary": [{ // Optional
776
- "id": String, // Page id
777
- "type": String, // type of page - view/edit
778
- "env": String, // env of page
779
- "timespent": Double, // Time taken per page
780
- "visits": Long // Number of times each page was visited
781
- }]
782
- }
783
- };
784
- </pre>
785
-
786
- <pre>
787
- let options = { // Optional
788
- context: {}, // To override the existing context
789
- object: {}, // To override the existing object
790
- actor: {}, // To override the existing actor
791
- tags: {}, // To override the existing tags
792
- runningEnv: "server" // It can be either client or server
793
- };
794
- </pre>
795
-
796
- ### Exdata
797
-
798
- This API is used to log telemetry for external data, while playing content
799
-
800
- <pre>
801
- exdata: function(data, options) { }
802
- </pre>
803
-
804
- Request Arguments:
805
-
806
- <pre>
807
- let data = {
808
- "type":"" - Free flowing text.For ex: partnerdata,xapi etc
809
- ....Serialized data(can be either encrypted / encoded / stringified)
810
-
811
- };
812
- </pre>
813
-
814
- <pre>
815
- let options = { // Optional
816
- context: {}, // To override the existing context
817
- object: {}, // To override the existing object
818
- actor: {}, // To override the existing actor
819
- tags: {}, // To override the existing tags
820
- runningEnv: "server" // It can be either client or server
821
- };
822
- </pre>
823
-
824
- ### End
825
-
826
- This API is used to log telemetry while the user is closing or exiting the content or game
827
-
828
- <pre>
829
- end: function(data, options) { }
830
- </pre>
831
-
832
- Request Arguments:
833
-
834
- <pre>
835
- let data = { // Required
836
- "contentId": "", // Required. Id of the content
837
- "type": "", // Required. app, session, editor, player, workflow, assessment
838
- "duration": "", // Required. Total duration from start to end in seconds
839
- "pageid": "", // Optional. Page/Stage id where the end has happened.
840
- "summary": [{ "key": "value" }] // Optional. Summary of actions done between start and end. For ex: "progress" for player session, "nodesModified" for collection editor
841
- };
842
- </pre>
843
-
844
- <pre>
845
- let options = { // Optional
846
- context: {}, // To override the existing context
847
- object: {}, // To override the existing object
848
- actor: {}, // To override the existing actor
849
- tags: {}, // To override the existing tags
850
- runningEnv: "server" // It can be either client or server
851
- };
852
- </pre>
853
-
854
-
855
- ### ResetContext
856
- This is used to reset the current context value with new context object.
857
-
858
- <pre>
859
- @param {context} Object - If context is undefined then library is reset to previous event context value.
860
- $t.resetContext(context)
861
- </pre>
862
-
863
- ### ResetObject
864
- Which is used reset the current object value with new obj
865
-
866
- <pre>
867
- @param {obj} Object - If the Object is undefined then library is reset to previous event object value.
868
- $t.resetObject(obj)
869
- </pre>
870
-
871
- ### ResetActor
872
- Which is used reset the current actor value with new actor
873
-
874
- <pre>
875
- @param {actor} Object - If the actor is undefined then library is reset to previous event actor value.
876
- $t.resetActor(actor)
877
- </pre>
878
-
879
- ### ResetTags
880
- Which is used to reset the current tag's value with new tag's
881
-
882
- <pre>
883
- @param {tags} Array - If tags are undefined then library is reset to previous event tags value.
884
- $t.resetTags(tags)
885
- </pre>
886
-
887
-
888
-
889
-
890
- ## ChangeLogs
891
-
892
- ****** ******
893
- **[0.0.1](https://github.com/manjudr/telemetry-lib/releases/tag/v1.0.0)**
894
-
895
- * Initial version of the telemetry-sdk [npm](https://www.npmjs.com/package/@project-sunbird/telemetry-sdk)
896
-
897
- **[0.0.2](https://github.com/manjudr/telemetry-lib/releases/tag/v1.0.0)**
898
-
899
- * Bug fixes
900
-
901
- **[0.0.3](https://github.com/manjudr/telemetry-lib/releases/tag/v1.0.1)**
902
-
903
- * Name space changes in the code
904
- * Removed `EkTelemetry` keyword in the code and provided a backward compatibility
905
-
906
- **[0.0.4](https://github.com/manjudr/telemetry-lib/releases/tag/v1.0.2)**
907
-
908
- * Bug fixes - Duration issue fix in both START and END Event (Converting millisecond to seconds)
909
- * Test case setup and improved code coverage
910
- * Travis integration
911
-
912
-
913
-
914
-
915
-
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.