@aicore/core-analytics-client-lib 1.0.8 → 1.0.11
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 +4 -11
- package/dist/analytics.min.js +3 -3
- package/dist/analytics.min.js.map +1 -1
- package/package.json +6 -6
- package/src/analytics.js +53 -31
package/README.md
CHANGED
|
@@ -118,7 +118,6 @@ during the `initAnalyticsSession` call. `initAnalyticsSession()` takes the follo
|
|
|
118
118
|
* `granularitySec` (_Optional_): The smallest time period under which the events can be distinguished. Multiple
|
|
119
119
|
events happening during this time period is aggregated to a count. The default granularity is 3 Seconds or server controlled,
|
|
120
120
|
which means that any events that happen within 3 seconds cannot be distinguished in ordering.
|
|
121
|
-
* `debug` (_Optional_): set to true if you want to see detailed debug logs.
|
|
122
121
|
|
|
123
122
|
#### usageExample
|
|
124
123
|
```javascript
|
|
@@ -139,17 +138,11 @@ initAnalyticsSession("accountID", "appName", "https://localhost:3000", 600, 5);
|
|
|
139
138
|
### Debug logs
|
|
140
139
|
If you want to see detailed logs on what is happening inside analytics lib, use the below code:
|
|
141
140
|
```js
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
initAnalyticsSession("accountID", "appName"
|
|
141
|
+
// Just before initAnalyticsSession call, set or unset window.analytics.debugMode
|
|
142
|
+
window.analytics.debugMode = true;
|
|
143
|
+
initAnalyticsSession("accountID", "appName");
|
|
145
144
|
```
|
|
146
145
|
|
|
147
|
-
To see info level logs that shows actual analytics data being sent to server, set the below property:
|
|
148
|
-
```js
|
|
149
|
-
window.analytics.debugInfoLogsEnable = true;
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
|
|
153
146
|
# Contribute to core-analytics-client-lib
|
|
154
147
|
|
|
155
148
|
## Building
|
|
@@ -159,7 +152,7 @@ Since this is a pure JS template project, build command just runs test with cove
|
|
|
159
152
|
|
|
160
153
|
# Before raising a pull request, run release script and add the generated
|
|
161
154
|
# minified files in dist folder to commits .
|
|
162
|
-
# WARNING!!!: If the minified files are not
|
|
155
|
+
# WARNING!!!: If the minified files are not checked in git push will fail.
|
|
163
156
|
> npm run release
|
|
164
157
|
```
|
|
165
158
|
|
package/dist/analytics.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function initAnalyticsSession(e,t,n,
|
|
2
|
-
typically means that you may be sending too many value events? .`),
|
|
3
|
-
postIntervalSeconds:${
|
|
1
|
+
function initAnalyticsSession(e,t,n,o,a){let i,r,s,l,u,c,d,f,y={};const v="aicore.analytics.userID",g="aicore.analytics.sessionID",p=1e4;let m=null;var w,h,b,C="undefined"==typeof window;let I="https://analytics.core.ai",S,D,E=0,T=!1;function U(...e){window.analytics.debugMode&&console.log("analytics client: ",...e)}function A(...e){window.analytics.debugMode&&console.error("analytics client: ",...e)}if(C)throw new Error("Node environment is not currently supported");function N(){return{schemaVersion:1,accountID:i,appName:r,uuid:s,sessionID:l,unixTimestampUTC:+new Date,numEventsTotal:0,events:{}}}function $(){if(!m)throw new Error("Please call initSession before using any analytics event")}function k(e){e.backoffCount=(e.backoffCount||0)+1,U(`Failed to call core analytics server. Will retry in ${30*e.backoffCount}s: `),setTimeout(()=>{R(e)},3e4*e.backoffCount)}function B(t){var e,n=JSON.stringify(t);n.length>p&&console.warn(`Analytics event generated is very large at greater than ${n.length}B. This
|
|
2
|
+
typically means that you may be sending too many value events? .`),U("Sending Analytics data of length: ",n.length,"B"),e=["Sending data:",t],window.analytics.debugMode&&console.info("analytics client: ",...e),window.navigator.onLine?window.fetch(f,{method:"POST",headers:{"Content-Type":"application/json"},body:n}).then(e=>{200!==e.status&&(400!==e.status?k(t):console.error("Bad Request, this is most likely a problem with the library, update to latest version."))}).catch(e=>{A(e),k(t)}):k(t)}function R(e){T||(e||(e=m,E=0,L(),m=N()),0!==e.numEventsTotal&&B(e))}function L(e){S&&(clearInterval(S),S=null),e||(S=setInterval(()=>{E+=c},1e3*c))}function M(e){L(e),D&&(clearInterval(D),D=null),e||(D=setInterval(R,1e3*u))}function _(n,o,a,i=1,r=0){if(!T){var s=n,l=o,u=a,c=i,d=r;if($(),!s||!l||!u)throw new Error("missing eventType or category or subCategory");if("number"!=typeof c||c<0)throw new Error("invalid count, count should be a positive number");if("number"!=typeof d)throw new Error("invalid value, value should be a number");{s=n;l=o;u=a;let e=m.events;e[s]=e[s]||{},e[s][l]=e[s][l]||{},e[s][l][u]=e[s][l][u]||{time:[],valueCount:[]}}let t=m.events;c=t[n][o][a].time;if((0<c.length?c[c.length-1]:null)===E){d=t[n][o][a].valueCount.length-1;{var s=d,l=n,u=o,c=a,d=i,f=r;let t=m.events;var e="number"==typeof t[l][u][c].valueCount[s];if(e&&0===f)t[l][u][c].valueCount[s]+=d;else if(e&&0!==f){let e={};e[f]=d,e[0]=t[l][u][c].valueCount[s],t[l][u][c].valueCount[s]=e}else if(!e){let e=t[l][u][c].valueCount[s];e[f]=(e[f]||0)+d}m.numEventsTotal+=1}}else{if(t[n][o][a].time.push(E),0===r)t[n][o][a].valueCount.push(i);else{let e={};e[r]=i,t[n][o][a].valueCount.push(e)}m.numEventsTotal+=1}}}if(!e||!t)throw new Error("accountID and appName must exist for init");d=n?n.toString().replace(/\/$/,""):I,i=e,r=t,u=o||60,c=a||3,f=d+"/ingest",s=function(){let e=localStorage.getItem(v);return e||(e=crypto.randomUUID(),localStorage.setItem(v,e)),e}(),l=function(){let e=sessionStorage.getItem(g);return e||(e=Math.random().toString(36).substr(2,10),sessionStorage.setItem(g,e)),e}(),m=N(),M(),w=o,h=a,new Promise(t=>{var e;window.navigator.onLine?(e=d+(`/getAppConfig?accountID=${i}&appName=`+r),window.fetch(e).then(e=>{switch(e.status){case 200:return void e.json().then(e=>{t(e)}).catch(e=>{A("remote response invalid. Continuing with defaults.",e),t({})});case 400:A("Bad Request, check library version compatible?",e),t({});break;default:A("Could not update from remote config. Continuing with defaults.",e),t({})}}).catch(e=>{A("Could not update from remote config. Continuing with defaults.",e),t({})})):t({})}).then(e=>{y!=={}&&(y=e,u=w||y.postIntervalSecondsInit||60,c=h||y.granularitySecInit||3,d=y.analyticsURLInit||d||I,M(T=!0===y.disabled),U(`Init analytics Config from remote. disabled: ${T}
|
|
3
|
+
postIntervalSeconds:${u}, granularitySec: ${c} ,URL: `+d),T&&console.warn(`Core Analytics is disabled from the server for app: ${i}:`+r))});for(b of analytics._initData)_(...b);analytics._initData=[],analytics._getCurrentAnalyticsEvent=function(){return $(),JSON.parse(JSON.stringify(m))},analytics._getAppConfig=function(){return{accountID:i,appName:r,disabled:T,uuid:s,sessionID:l,postIntervalSeconds:u,granularitySec:c,analyticsURL:d,serverConfig:y}},analytics.event=_}window.analytics||(window.analytics={_initData:[],debugMode:!1}),void 0===window.crypto&&(window.crypto={}),"randomUUID"in crypto||(crypto.randomUUID=function(){return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16))});
|
|
4
4
|
//# sourceMappingURL=analytics.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sourceRoot":"src/analytics.js","sources":["src/analytics.js"],"names":["initAnalyticsSession","accountIDInit","appNameInit","analyticsURLInit","postIntervalSecondsInit","granularitySecInit","
|
|
1
|
+
{"version":3,"sourceRoot":"src/analytics.js","sources":["src/analytics.js"],"names":["initAnalyticsSession","accountIDInit","appNameInit","analyticsURLInit","postIntervalSecondsInit","granularitySecInit","let","accountID","appName","userID","sessionID","postIntervalSeconds","granularitySec","analyticsURL","postURL","serverConfig","USERID_LOCAL_STORAGE_KEY","SESSION_ID_LOCAL_STORAGE_KEY","POST_LARGE_DATA_THRESHOLD_BYTES","currentAnalyticsEvent","postIntervalSecondsInitial","granularitySecInitial","eventData","IS_NODE_ENV","window","DEFAULT_BASE_URL","granularityTimer","postTimer","currentQuantisedTime","disabled","debugLog","args","analytics","debugMode","console","log","debugError","error","Error","_createAnalyticsEvent","schemaVersion","uuid","unixTimestampUTC","Date","numEventsTotal","events","_validateCurrentState","_retryPost","eventToSend","backoffCount","setTimeout","_postCurrentAnalyticsEvent","DEFAULT_RETRY_TIME_IN_SECONDS","_postEventWithRetry","textToSend","JSON","stringify","length","warn","debugInfo","info","navigator","onLine","fetch","method","headers","Content-Type","body","then","res","status","catch","_resetGranularityTimer","disable","clearInterval","setInterval","_setupTimers","event","eventType","eventCategory","subCategory","eventCount","eventValue","_validateEvent","category","count","value","_ensureAnalyticsEventExists","time","valueCount","timeArray","modificationIndex","_updateExistingAnalyticsEvent","index","newValue","storedValueIsCount","newValueCount","storedValueObject","push","toString","replace","localUserID","localStorage","getItem","crypto","randomUUID","setItem","_getOrCreateUserID","localSessionID","sessionStorage","Math","random","substr","_getOrCreateSessionID","Promise","configURL","json","serverResponse","resolve","err","updatedServerConfig","_initData","_getCurrentAnalyticsEvent","parse","_getAppConfig","c","getRandomValues","Uint8Array"],"mappings":"AAwCA,SAASA,qBAAqBC,EAAeC,EAAaC,EACtDC,EAAyBC,GACzBC,IAAIC,EAAWC,EAASC,EAAQC,EAAWC,EACvCC,EAAgBC,EAAcC,EAASC,EAAa,GACxD,MAGMC,EAA2B,0BAC3BC,EAA+B,6BAC/BC,EAAkC,IACxCZ,IAAIa,EAAwB,KAC5B,IAkN+BC,EAA4BC,EAwHnDC,EA1UFC,EAAiC,oBAAXC,OAC5BlB,IAAImB,EAAmB,4BAEnBC,EACAC,EACAC,EAAuB,EACvBC,GAAW,EAEf,SAASC,KAAYC,GACbP,OAAOQ,UAAUC,WAGrBC,QAAQC,IAAI,wBAAyBJ,GASzC,SAASK,KAAcL,GACfP,OAAOQ,UAAUC,WAGrBC,QAAQG,MAAM,wBAAyBN,GAI3C,GAAGR,EACC,MAAM,IAAIe,MAAM,+CAGpB,SAASC,IACL,MAAO,CACHC,cAAe,EACfjC,UAAWA,EACXC,QAASA,EACTiC,KAAMhC,EACNC,UAAWA,EACXgC,kBAAmB,IAAIC,KACvBC,eAAgB,EAChBC,OAAQ,IAIhB,SAASC,I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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aicore/core-analytics-client-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "Analytics client library for https://github.com/aicore/Core-Analytics-Server",
|
|
5
5
|
"main": "dist/analytics.min.js",
|
|
6
6
|
"type": "module",
|
|
@@ -47,14 +47,14 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/aicore/core-analytics-client-lib#readme",
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@commitlint/cli": "
|
|
51
|
-
"@commitlint/config-conventional": "
|
|
52
|
-
"c8": "7.11.
|
|
50
|
+
"@commitlint/cli": "17.0.2",
|
|
51
|
+
"@commitlint/config-conventional": "17.0.2",
|
|
52
|
+
"c8": "7.11.3",
|
|
53
53
|
"chai": "4.3.6",
|
|
54
|
-
"eslint": "8.
|
|
54
|
+
"eslint": "8.16.0",
|
|
55
55
|
"husky": "7.0.4",
|
|
56
56
|
"mocha": "9.2.2",
|
|
57
57
|
"uglify-js": "3.15.5",
|
|
58
|
-
"http-server": "14.1.
|
|
58
|
+
"http-server": "14.1.1"
|
|
59
59
|
}
|
|
60
60
|
}
|
package/src/analytics.js
CHANGED
|
@@ -6,7 +6,24 @@
|
|
|
6
6
|
|
|
7
7
|
if(!window.analytics){
|
|
8
8
|
window.analytics = {
|
|
9
|
-
_initData: []
|
|
9
|
+
_initData: [],
|
|
10
|
+
debugMode: false
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (typeof window.crypto === 'undefined'){
|
|
15
|
+
window.crypto = {};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!('randomUUID' in crypto)){
|
|
19
|
+
// https://stackoverflow.com/a/2117523/2800218
|
|
20
|
+
// LICENSE: https://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
21
|
+
crypto.randomUUID = function randomUUID() {
|
|
22
|
+
return (
|
|
23
|
+
[1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,
|
|
24
|
+
// eslint-disable-next-line no-bitwise
|
|
25
|
+
c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
|
26
|
+
);
|
|
10
27
|
};
|
|
11
28
|
}
|
|
12
29
|
|
|
@@ -20,10 +37,9 @@ if(!window.analytics){
|
|
|
20
37
|
* @param granularitySecInit Optional: The smallest time period under which the events can be distinguished. Multiple
|
|
21
38
|
* events happening during this time period is aggregated to a count. The default granularity is 3 Seconds or server
|
|
22
39
|
* controlled, which means that any events that happen within 3 seconds cannot be distinguished in ordering.
|
|
23
|
-
* @param debug set to true if you want to see detailed debug logs.
|
|
24
40
|
*/
|
|
25
41
|
function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
26
|
-
postIntervalSecondsInit, granularitySecInit
|
|
42
|
+
postIntervalSecondsInit, granularitySecInit) {
|
|
27
43
|
let accountID, appName, userID, sessionID, postIntervalSeconds,
|
|
28
44
|
granularitySec, analyticsURL, postURL, serverConfig={};
|
|
29
45
|
const DEFAULT_GRANULARITY_IN_SECONDS = 3;
|
|
@@ -40,23 +56,22 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
40
56
|
let postTimer;
|
|
41
57
|
let currentQuantisedTime = 0;
|
|
42
58
|
let disabled = false;
|
|
43
|
-
let debugMode = false;
|
|
44
59
|
|
|
45
60
|
function debugLog(...args) {
|
|
46
|
-
if(!debugMode){
|
|
61
|
+
if(!window.analytics.debugMode){
|
|
47
62
|
return;
|
|
48
63
|
}
|
|
49
64
|
console.log("analytics client: ", ...args);
|
|
50
65
|
}
|
|
51
66
|
|
|
52
67
|
function debugInfo(...args) {
|
|
53
|
-
if(
|
|
68
|
+
if(window.analytics.debugMode){
|
|
54
69
|
console.info("analytics client: ", ...args);
|
|
55
70
|
}
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
function debugError(...args) {
|
|
59
|
-
if(!debugMode){
|
|
74
|
+
if(!window.analytics.debugMode){
|
|
60
75
|
return;
|
|
61
76
|
}
|
|
62
77
|
console.error("analytics client: ", ...args);
|
|
@@ -131,7 +146,7 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
131
146
|
typically means that you may be sending too many value events? .`);
|
|
132
147
|
}
|
|
133
148
|
debugLog("Sending Analytics data of length: ", textToSend.length, "B");
|
|
134
|
-
debugInfo("Sending data:",
|
|
149
|
+
debugInfo("Sending data:", eventToSend);
|
|
135
150
|
if(!window.navigator.onLine){
|
|
136
151
|
_retryPost(eventToSend);
|
|
137
152
|
// chrome shows all network failure requests in console. In offline mode, we don't want to bomb debug
|
|
@@ -198,7 +213,7 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
198
213
|
postTimer = setInterval(_postCurrentAnalyticsEvent, postIntervalSeconds*1000);
|
|
199
214
|
}
|
|
200
215
|
|
|
201
|
-
|
|
216
|
+
function _getServerConfig() {
|
|
202
217
|
return new Promise((resolve)=>{
|
|
203
218
|
if(!window.navigator.onLine){
|
|
204
219
|
resolve({});
|
|
@@ -207,11 +222,15 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
207
222
|
return;
|
|
208
223
|
}
|
|
209
224
|
let configURL = analyticsURL + `/getAppConfig?accountID=${accountID}&appName=${appName}`;
|
|
210
|
-
window.fetch(configURL).then(
|
|
225
|
+
window.fetch(configURL).then(res=>{
|
|
211
226
|
switch (res.status) {
|
|
212
227
|
case 200:
|
|
213
|
-
|
|
214
|
-
|
|
228
|
+
res.json().then(serverResponse =>{
|
|
229
|
+
resolve(serverResponse);
|
|
230
|
+
}).catch(err => {
|
|
231
|
+
debugError("remote response invalid. Continuing with defaults.", err);
|
|
232
|
+
resolve({});
|
|
233
|
+
});
|
|
215
234
|
return;
|
|
216
235
|
case 400:
|
|
217
236
|
debugError("Bad Request, check library version compatible?", res);
|
|
@@ -240,27 +259,29 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
240
259
|
};
|
|
241
260
|
}
|
|
242
261
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
262
|
+
function _initFromRemoteConfig(postIntervalSecondsInitial, granularitySecInitial) {
|
|
263
|
+
_getServerConfig().then(updatedServerConfig =>{
|
|
264
|
+
if(serverConfig !== {}){
|
|
265
|
+
serverConfig = updatedServerConfig;
|
|
266
|
+
// User init overrides takes precedence over server overrides
|
|
267
|
+
postIntervalSeconds = postIntervalSecondsInitial ||
|
|
268
|
+
serverConfig["postIntervalSecondsInit"] || DEFAULT_POST_INTERVAL_SECONDS;
|
|
269
|
+
granularitySec = granularitySecInitial || serverConfig["granularitySecInit"] || DEFAULT_GRANULARITY_IN_SECONDS;
|
|
270
|
+
// For URLs, the server suggested URL takes precedence over user init values
|
|
271
|
+
analyticsURL = serverConfig["analyticsURLInit"] || analyticsURL || DEFAULT_BASE_URL;
|
|
272
|
+
disabled = serverConfig["disabled"] === true;
|
|
273
|
+
_setupTimers(disabled);
|
|
274
|
+
debugLog(`Init analytics Config from remote. disabled: ${disabled}
|
|
255
275
|
postIntervalSeconds:${postIntervalSeconds}, granularitySec: ${granularitySec} ,URL: ${analyticsURL}`);
|
|
256
|
-
|
|
257
|
-
|
|
276
|
+
if(disabled){
|
|
277
|
+
console.warn(`Core Analytics is disabled from the server for app: ${accountID}:${appName}`);
|
|
278
|
+
}
|
|
258
279
|
}
|
|
259
|
-
}
|
|
280
|
+
});
|
|
260
281
|
}
|
|
261
282
|
|
|
262
283
|
function _stripTrailingSlash(url) {
|
|
263
|
-
return url.replace(/\/$/, "");
|
|
284
|
+
return url.toString().replace(/\/$/, "");
|
|
264
285
|
}
|
|
265
286
|
|
|
266
287
|
function _ensureAnalyticsEventExists(eventType, category, subCategory) {
|
|
@@ -268,8 +289,10 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
268
289
|
events[eventType] = events[eventType] || {};
|
|
269
290
|
events[eventType][category] = events[eventType][category] || {};
|
|
270
291
|
events[eventType][category][subCategory] = events[eventType][category][subCategory] || {
|
|
271
|
-
|
|
272
|
-
|
|
292
|
+
// quantised time
|
|
293
|
+
time: [],
|
|
294
|
+
// value and count array, If a single value, then it is count, else object {"val1":count1, ...}
|
|
295
|
+
valueCount: []
|
|
273
296
|
};
|
|
274
297
|
}
|
|
275
298
|
|
|
@@ -344,7 +367,6 @@ function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
|
344
367
|
analyticsURL = analyticsURLInit? _stripTrailingSlash(analyticsURLInit) : DEFAULT_BASE_URL;
|
|
345
368
|
accountID = accountIDInit;
|
|
346
369
|
appName = appNameInit;
|
|
347
|
-
debugMode = debug || false;
|
|
348
370
|
postIntervalSeconds = postIntervalSecondsInit || DEFAULT_POST_INTERVAL_SECONDS;
|
|
349
371
|
granularitySec = granularitySecInit || DEFAULT_GRANULARITY_IN_SECONDS;
|
|
350
372
|
postURL = analyticsURL + "/ingest";
|