@aicore/core-analytics-client-lib 1.0.4 → 1.0.7
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 +94 -38
- package/dist/analytics.min.js +3 -3
- package/dist/analytics.min.js.map +1 -1
- package/package.json +11 -9
- package/src/analytics.js +287 -267
package/README.md
CHANGED
|
@@ -19,71 +19,127 @@ events for [Core-Analytics-Server](https://github.com/aicore/Core-Analytics-Serv
|
|
|
19
19
|
|
|
20
20
|
# Usage
|
|
21
21
|
|
|
22
|
-
##
|
|
23
|
-
Embed the script in your HTML file
|
|
22
|
+
## Load the Library
|
|
23
|
+
Embed the script in your HTML file and replace `your_analytics_account_ID` and `appName`
|
|
24
|
+
in the `initAnalyticsSession` call below:
|
|
24
25
|
```html
|
|
25
|
-
|
|
26
|
-
<script
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
<!-- Global window.analytics object - core.ai analytics services -->
|
|
27
|
+
<script async src="https://unpkg.com/@aicore/core-analytics-client-lib/src/analytics.js"
|
|
28
|
+
onload="analyticsLibLoaded()"></script>
|
|
29
|
+
<script>
|
|
30
|
+
if(!window.analytics){ window.analytics = {
|
|
31
|
+
_initData : [], loadStartTime: new Date().getTime(),
|
|
32
|
+
event: function (){window.analytics._initData.push(arguments);}
|
|
33
|
+
};}
|
|
34
|
+
function analyticsLibLoaded() {
|
|
35
|
+
initAnalyticsSession('your_analytics_account_ID', 'appName');
|
|
36
|
+
analytics.event("core-analytics", "client-lib", "loadTime", 1, (new Date().getTime())-analytics.loadStartTime);
|
|
37
|
+
}
|
|
31
38
|
</script>
|
|
32
|
-
</html>
|
|
33
39
|
```
|
|
40
|
+
This will create a global `analytics` variable which can be used to access the analytics APIs.
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* `accountID`: Your analytics account id as configured in the server or core.ai analytics
|
|
38
|
-
* `appName`: The app name to log the events against. Eg: "phoenixCode"
|
|
39
|
-
* `postIntervalSeconds` (_Optional_): This defines the interval between sending analytics events to the server. Default is 10 minutes
|
|
40
|
-
* `granularitySec` (_Optional_): The smallest time period under which the events can be distinguished. Multiple
|
|
41
|
-
events happening during this time period is aggregated to a count. The default granularity is 3 Seconds, which means
|
|
42
|
-
that any events that happen within 3 seconds cannot be distinguished in ordering.
|
|
43
|
-
* `analyticsURL` (_Optional_): Provide your own analytics server address if you self-hosted the server
|
|
44
|
-
* `debug` (_Optional_): set to true if you want to see detailed debug logs.
|
|
45
|
-
|
|
46
|
-
```javascript
|
|
47
|
-
// Example for custom initSession where the analytics aggregated data
|
|
48
|
-
// is posted to custom server https://localhost:3000 every 600 secs
|
|
49
|
-
// with a granularity(resolution) of 5 seconds.
|
|
50
|
-
|
|
51
|
-
initSession("accountID", "appName", "https://localhost:3000", 600, 5);
|
|
52
|
-
|
|
53
|
-
// To initSession in debug mode set debug arg in init to true:
|
|
54
|
-
initSession("accountID", "appName", "https://localhost:3000", 600, 5, true);
|
|
55
|
-
```
|
|
42
|
+
NB: The script is loaded async, so it will not block other js scripts. `analytics.event` api can be called anytime
|
|
43
|
+
after the above code and need not wait for the script load to complete.
|
|
56
44
|
|
|
57
45
|
## Raising analytics events
|
|
58
|
-
|
|
59
|
-
The
|
|
46
|
+
We can now start logging analytics events by calling `analytics.event` API.
|
|
47
|
+
The events will be aggregated and send to the analytics server periodically.
|
|
60
48
|
|
|
61
49
|
```javascript
|
|
62
50
|
// analyticsEvent(eventType, eventCategory, subCategory, eventCount, eventValue);
|
|
63
51
|
|
|
64
52
|
// Eg: event without counts and values
|
|
65
|
-
|
|
53
|
+
analytics.event("platform", "os", "linux");
|
|
66
54
|
|
|
67
55
|
// Eg: event with count, here it logs that html file is opened 100 times
|
|
68
|
-
|
|
56
|
+
analytics.event("file", "opened", "html", 100);
|
|
69
57
|
|
|
70
58
|
// Eg: event with count and value, here it logs that the startup time is 250 milliseconds.
|
|
71
59
|
// Note that the value is unitless from analytics perspective. unit is deduced from subCategory name
|
|
72
|
-
|
|
60
|
+
analytics.event("platform", "performance", "startupTimeMs", 1, 250);
|
|
73
61
|
|
|
74
62
|
// Eg: event with fractional value.
|
|
75
|
-
|
|
63
|
+
analytics.event("platform", "CPU", "utilization", 1, .45);
|
|
76
64
|
// Eg. Here we register that the system has 8 cores with each core having 2300MHz frequency.
|
|
77
|
-
|
|
65
|
+
analytics.event("platform", "CPU", "coreCountsAndFrequencyMhz", 8, 2300);
|
|
78
66
|
```
|
|
79
67
|
### API parameters
|
|
80
68
|
* `eventType` - A string, required
|
|
81
69
|
* `eventCategory` - A string, required
|
|
82
70
|
* `subCategory` - A string, required
|
|
83
71
|
* `eventCount` (_Optional_) : A non-negative number indicating the number of times the event (or an event with a
|
|
84
|
-
particular value if a value is specified) happened. defaults to 1.
|
|
72
|
+
particular value if a value is specified) happened. defaults to 1.
|
|
85
73
|
* `eventValue` (_Optional_) : A number value associated with the event. defaults to 0
|
|
86
74
|
|
|
75
|
+
|
|
76
|
+
## Advanced Usages
|
|
77
|
+
|
|
78
|
+
### Pure JS loading instead of HTML scripts
|
|
79
|
+
|
|
80
|
+
There may be cases where you would want to load the script from JS alone. For Eg. you
|
|
81
|
+
may want to delay library loading till user consents GDPR. For such use cases, use the below code.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
function _initCoreAnalytics() {
|
|
85
|
+
// Load core analytics scripts
|
|
86
|
+
if(!window.analytics){ window.analytics = {
|
|
87
|
+
_initData: [], loadStartTime: new Date().getTime(),
|
|
88
|
+
event: function (){window.analytics._initData.push(arguments);}
|
|
89
|
+
};}
|
|
90
|
+
let script = document.createElement('script');
|
|
91
|
+
script.type = 'text/javascript';
|
|
92
|
+
script.async = true;
|
|
93
|
+
script.onload = function(){
|
|
94
|
+
// replace `your_analytics_account_ID` and `appName` below with your values
|
|
95
|
+
window.initAnalyticsSession('your_analytics_account_ID', 'appName'); // if you have a custom analytics server
|
|
96
|
+
window.analytics.event("core-analytics", "client-lib", "loadTime", 1,
|
|
97
|
+
(new Date().getTime())- window.analytics.loadStartTime);
|
|
98
|
+
};
|
|
99
|
+
script.src = 'https://unpkg.com/@aicore/core-analytics-client-lib/dist/analytics.min.js';
|
|
100
|
+
document.getElementsByTagName('head')[0].appendChild(script);
|
|
101
|
+
}
|
|
102
|
+
_initCoreAnalytics();
|
|
103
|
+
```
|
|
104
|
+
To load the library, just call `_initCoreAnalytics()` from JS. Note that you may not be able
|
|
105
|
+
to use `analytics.event()` APIs before `_initCoreAnalytics()` call is made.
|
|
106
|
+
|
|
107
|
+
### initAnalyticsSession: modify when, where and how analytics lib sends events
|
|
108
|
+
If you want to modify how analytics library collects and sends information, it is recommended to do so
|
|
109
|
+
with analytics server [accountConfig](https://github.com/aicore/Core-Analytics-Server#accountconfig-configuration).
|
|
110
|
+
|
|
111
|
+
Alternatively for one off development time uses, the behavior of the library can be configured
|
|
112
|
+
during the `initAnalyticsSession` call. `initAnalyticsSession()` takes the following parameters:
|
|
113
|
+
|
|
114
|
+
* `accountID`: Your analytics account id as configured in the server or core.ai analytics
|
|
115
|
+
* `appName`: The app name to log the events against. Eg: "phoenixCode"
|
|
116
|
+
* `postIntervalSeconds` (_Optional_): This defines the interval between sending analytics events to the server. Default is 10 minutes
|
|
117
|
+
* `granularitySec` (_Optional_): The smallest time period under which the events can be distinguished. Multiple
|
|
118
|
+
events happening during this time period is aggregated to a count. The default granularity is 3 Seconds, which means
|
|
119
|
+
that any events that happen within 3 seconds cannot be distinguished in ordering.
|
|
120
|
+
* `analyticsURL` (_Optional_): Provide your own analytics server address if you self-hosted the server
|
|
121
|
+
* `debug` (_Optional_): set to true if you want to see detailed debug logs.
|
|
122
|
+
|
|
123
|
+
### usageExample
|
|
124
|
+
```javascript
|
|
125
|
+
// Init with default values and server controlled config. use the following `analyticsLibLoaded` function
|
|
126
|
+
function analyticsLibLoaded() {
|
|
127
|
+
initAnalyticsSession('your_analytics_account_ID', 'appName');
|
|
128
|
+
analytics.event("core-analytics", "client-lib", "loadTime", 1, (new Date().getTime())-analytics.loadStartTime);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//Replace initAnalyticsSession in analyticsLibLoaded function for the below use cases.
|
|
132
|
+
|
|
133
|
+
// Example for custom initSession where the analytics aggregated data
|
|
134
|
+
// is posted to custom server https://localhost:3000 every 600 secs
|
|
135
|
+
// with a granularity(resolution) of 5 seconds.
|
|
136
|
+
initAnalyticsSession("accountID", "appName", "https://localhost:3000", 600, 5);
|
|
137
|
+
|
|
138
|
+
// To initSession in debug mode set debug arg in init to true. In debug mode, details logs
|
|
139
|
+
// about analytics library events will be emitted.
|
|
140
|
+
initAnalyticsSession("accountID", "appName", "https://localhost:3000", 600, 5, true);
|
|
141
|
+
```
|
|
142
|
+
|
|
87
143
|
# Contribute to core-analytics-client-lib
|
|
88
144
|
|
|
89
145
|
## Building
|
package/dist/analytics.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
let
|
|
2
|
-
typically means that you may be sending too many value events? .`),
|
|
3
|
-
postIntervalSeconds:${
|
|
1
|
+
function initAnalyticsSession(e,t,n,a,o,i){let s,r,l,u,c,f,y,v,d={};const m=30,p="aicore.analytics.userID",g="aicore.analytics.sessionID",h=1e4;let w=null;var C,b="undefined"==typeof window;let I="https://analytics.core.ai",S,D,E=0,T=!1,A=!1;function N(...e){A&&console.log(...e)}if(b)throw new Error("Node environment is not currently supported");function $(){return{schemaVersion:1,accountID:s,appName:r,uuid:l,sessionID:u,unixTimestampUTC:+new Date,numEventsTotal:0,events:{}}}function k(){if(!w)throw new Error("Please call initSession before using any analytics event")}function U(e){e.backoffCount=(e.backoffCount||0)+1,N(`Failed to call core analytics server. Will retry in ${m*e.backoffCount}s: `),setTimeout(()=>{R(e)},1e3*m*e.backoffCount)}function R(t){var e;T||(t||(t=w,E=0,_(),w=$()),0!==t.numEventsTotal&&((e=JSON.stringify(t)).length>h&&console.warn(`Analytics event generated is very large at greater than ${e.length}B. This
|
|
2
|
+
typically means that you may be sending too many value events? .`),N("Sending Analytics data of length: ",e.length,"B"),window.fetch(v,{method:"POST",headers:{"Content-Type":"application/json"},body:e}).then(e=>{200!==e.status&&(400!==e.status?U(t):console.error("Analytics client: Bad Request, this is most likely a problem with the library, update to latest version."))}).catch(e=>{e=[e],A&&console.error(...e),U(t)})))}function _(e){S&&(clearInterval(S),S=null),e||(S=setInterval(()=>{E+=f},1e3*f))}function B(e){_(e),D&&(clearInterval(D),D=null),e||(D=setInterval(R,1e3*c))}function O(n,a,o,i=1,s=0){if(!T){var r=n,l=a,u=o,c=i,f=s;if(k(),!r||!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 f)throw new Error("invalid value, value should be a number");{r=n;l=a;u=o;let e=w.events;e[r]=e[r]||{},e[r][l]=e[r][l]||{},e[r][l][u]=e[r][l][u]||{time:[],valueCount:[]}}let t=w.events;c=t[n][a][o].time;if((0<c.length?c[c.length-1]:null)===E){f=t[n][a][o].valueCount.length-1;{var r=f,l=n,u=a,c=o,f=i,y=s;let t=w.events;var e="number"==typeof t[l][u][c].valueCount[r];if(e&&0===y)t[l][u][c].valueCount[r]+=f;else if(e&&0!==y){let e={};e[y]=f,e[0]=t[l][u][c].valueCount[r],t[l][u][c].valueCount[r]=e}else if(!e){let e=t[l][u][c].valueCount[r];e[y]=(e[y]||0)+f}w.numEventsTotal+=1}}else{if(t[n][a][o].time.push(E),0===s)t[n][a][o].valueCount.push(i);else{let e={};e[s]=i,t[n][a][o].valueCount.push(e)}w.numEventsTotal+=1}}}if(!e||!t)throw new Error("accountID and appName must exist for init");y=n?n.replace(/\/$/,""):I,s=e,r=t,A=i||!1,c=a||600,f=o||3,v=y+"/ingest",l=function(){let e=localStorage.getItem(p);return e||(e=crypto.randomUUID(),localStorage.setItem(p,e)),e}(),u=function(){let e=sessionStorage.getItem(g);return e||(e=Math.random().toString(36).substr(2,10),sessionStorage.setItem(g,e)),e}(),w=$(),B(),async function(e,t){(d=await new Promise((n,a)=>{var e=y+(`/getAppConfig?accountID=${s}&appName=`+r);window.fetch(e).then(async e=>{switch(e.status){case 200:var t=await e.json();return void n(t);case 400:a("Bad Request, check library version compatible?",e);break;default:a("analytics client: Could not update from remote config. Continuing with defaults.",e)}}).catch(e=>{a("analytics client: Could not update from remote config. Continuing with defaults.",e)})}))!=={}&&(c=e||d.postIntervalSecondsInit||600,f=t||d.granularitySecInit||3,y=d.analyticsURLInit||y||I,B(T=!0===d.disabled),N(`Init analytics Config from remote. disabled: ${T}
|
|
3
|
+
postIntervalSeconds:${c}, granularitySec: ${f} ,URL: `+y),T&&console.warn(`Core Analytics is disabled from the server for app: ${s}:`+r))}(a,o);for(C of analytics._initData)O(...C);analytics._initData=[],analytics._getCurrentAnalyticsEvent=function(){return k(),JSON.parse(JSON.stringify(w))},analytics._getAppConfig=function(){return{accountID:s,appName:r,disabled:T,uuid:l,sessionID:u,postIntervalSeconds:c,granularitySec:f,analyticsURL:y,serverConfig:d}},analytics.event=O}window.analytics||(window.analytics={_initData:[]});
|
|
4
4
|
//# sourceMappingURL=analytics.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sourceRoot":"src/analytics.js","sources":["src/analytics.js"],"names":["let","accountID","appName","userID","sessionID","postIntervalSeconds","granularitySec","analyticsURL","postURL","serverConfig","
|
|
1
|
+
{"version":3,"sourceRoot":"src/analytics.js","sources":["src/analytics.js"],"names":["initAnalyticsSession","accountIDInit","appNameInit","analyticsURLInit","postIntervalSecondsInit","granularitySecInit","debug","let","accountID","appName","userID","sessionID","postIntervalSeconds","granularitySec","analyticsURL","postURL","serverConfig","DEFAULT_RETRY_TIME_IN_SECONDS","USERID_LOCAL_STORAGE_KEY","SESSION_ID_LOCAL_STORAGE_KEY","POST_LARGE_DATA_THRESHOLD_BYTES","currentAnalyticsEvent","eventData","IS_NODE_ENV","window","DEFAULT_BASE_URL","granularityTimer","postTimer","currentQuantisedTime","disabled","debugMode","debugLog","args","console","log","Error","_createAnalyticsEvent","schemaVersion","uuid","unixTimestampUTC","Date","numEventsTotal","events","_validateCurrentState","_retryPost","eventToSend","backoffCount","setTimeout","_postCurrentAnalyticsEvent","textToSend","_resetGranularityTimer","JSON","stringify","length","warn","fetch","method","headers","Content-Type","body","then","res","status","error","catch","debugError","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","replace","localUserID","localStorage","getItem","crypto","randomUUID","setItem","_getOrCreateUserID","localSessionID","sessionStorage","Math","random","toString","substr","_getOrCreateSessionID","async","postIntervalSecondsInitial","granularitySecInitial","Promise","resolve","reject","configURL","serverResponse","json","err","_initFromRemoteConfig","analytics","_initData","_getCurrentAnalyticsEvent","parse","_getAppConfig"],"mappings":"AAwBA,SAASA,qBAAqBC,EAAeC,EAAaC,EACtDC,EAAyBC,EAAoBC,GAC7CC,IAAIC,EAAWC,EAASC,EAAQC,EAAWC,EACvCC,EAAgBC,EAAcC,EAASC,EAAa,GACxD,MACMC,EAAgC,GAEhCC,EAA2B,0BAC3BC,EAA+B,6BAC/BC,EAAkC,IACxCb,IAAIc,EAAwB,KAC5B,IA2SQC,EA3SFC,EAAiC,oBAAXC,OAC5BjB,IAAIkB,EAAmB,4BAEnBC,EACAC,EACAC,EAAuB,EACvBC,GAAW,EACXC,GAAY,EAEhB,SAASC,KAAYC,GACbF,GAGJG,QAAQC,OAAOF,GAWnB,GAAGT,EACC,MAAM,IAAIY,MAAM,+CAGpB,SAASC,IACL,MAAO,CACHC,cAAe,EACf7B,UAAWA,EACXC,QAASA,EACT6B,KAAM5B,EACNC,UAAWA,EACX4B,kBAAmB,IAAIC,KACvBC,eAAgB,EAChBC,OAAQ,IAIhB,SAASC,IACL,IAAItB,EACA,MAAM,IAAIc,MAAM,4DAiCxB,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G,GAClB5F,EAAsC,yBAhMZ,IAiM9BH,EAAiBgG,GAAyB7F,EAAiC,oBAnM5C,EAqM/BF,EAAeE,EAA+B,kBAAKF,GAAgBW,EAEnE4C,EADAxC,GAAwC,IAA7Bb,EAAuB,UAElCe,kDAAyDF;8BACvCjB,sBAAwCC,WAAwBC,GAC/Ee,GACCI,QAAQqB,4DAA4D9C,KAAaC,IAiG7F4G,CAAsBjH,EAAyBC,GAM/C,IAAQiB,KAAagG,UAAUC,UAC3BjD,KAAShD,GAEbgG,UAAUC,UAAY,GAGtBD,UAAUE,0BAlQV,WAGI,OAFA7E,IAEOQ,KAAKsE,MAAMtE,KAAKC,UAAU/B,KAgQrCiG,UAAUI,cApIV,WACI,MAAO,CACHlH,UAAAA,EAAWC,QAAAA,EAASoB,SAAAA,EACpBS,KAAM5B,EAAQC,UAAAA,EACdC,oBAAAA,EAAqBC,eAAAA,EAAgBC,aAAAA,EAAcE,aAAAA,IAmI3DsG,UAAUhD,MAAQA,EAlVlB9C,OAAO8F,YACP9F,OAAO8F,UAAY,CACfC,UAAW"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aicore/core-analytics-client-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
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",
|
|
@@ -19,17 +19,18 @@
|
|
|
19
19
|
"lint": "eslint --quiet src test",
|
|
20
20
|
"lint:fix": "eslint --quiet --fix src test",
|
|
21
21
|
"prepare": "husky install",
|
|
22
|
-
"test": "echo please open test/
|
|
23
|
-
"test:unit": "
|
|
22
|
+
"test": "echo please run - npm run serve - and then open test/unit-test.html in browser to run tests",
|
|
23
|
+
"test:unit": "npm run test",
|
|
24
24
|
"test:integ": "echo integration tests are disabled in this repo",
|
|
25
|
-
"cover": "echo
|
|
26
|
-
"cover:unit": "echo
|
|
27
|
-
"cover:integ": "echo
|
|
28
|
-
"build": "",
|
|
25
|
+
"cover": "echo no coverage for now",
|
|
26
|
+
"cover:unit": "echo no coverage for now",
|
|
27
|
+
"cover:integ": "echo no coverage for now",
|
|
28
|
+
"build": "npm run minify",
|
|
29
29
|
"minify": "echo creating minified package dist/analytics.min.js && mkdir -p dist && uglifyjs src/analytics.js --compress --mangle -o dist/analytics.min.js -c -m --source-map \"root='src/analytics.js',url='analytics.min.js.map'\"",
|
|
30
30
|
"bumpPatchVersion": "npm --no-git-tag-version version patch",
|
|
31
31
|
"bumpPatchVersionWithGitTag": "npm version patch",
|
|
32
|
-
"release": "npm run minify && npm run bumpPatchVersionWithGitTag"
|
|
32
|
+
"release": "npm run minify && npm run bumpPatchVersionWithGitTag",
|
|
33
|
+
"serve": "http-server . -p 8000 -c-1"
|
|
33
34
|
},
|
|
34
35
|
"files": [
|
|
35
36
|
"src",
|
|
@@ -53,6 +54,7 @@
|
|
|
53
54
|
"eslint": "8.15.0",
|
|
54
55
|
"husky": "7.0.4",
|
|
55
56
|
"mocha": "9.2.2",
|
|
56
|
-
"uglify-js": "3.15.5"
|
|
57
|
+
"uglify-js": "3.15.5",
|
|
58
|
+
"http-server": "14.1.0"
|
|
57
59
|
}
|
|
58
60
|
}
|
package/src/analytics.js
CHANGED
|
@@ -1,233 +1,318 @@
|
|
|
1
1
|
// GNU AGPL-3.0 License Copyright (c) 2021 - present core.ai . All rights reserved.
|
|
2
2
|
|
|
3
3
|
// jshint ignore: start
|
|
4
|
-
/*global localStorage, sessionStorage, crypto*/
|
|
4
|
+
/*global localStorage, sessionStorage, crypto, analytics*/
|
|
5
5
|
|
|
6
|
-
let accountID, appName, userID, sessionID, postIntervalSeconds, granularitySec, analyticsURL, postURL, serverConfig;
|
|
7
|
-
const DEFAULT_GRANULARITY_IN_SECONDS = 3;
|
|
8
|
-
const DEFAULT_RETRY_TIME_IN_SECONDS = 30;
|
|
9
|
-
const DEFAULT_POST_INTERVAL_SECONDS = 600; // 10 minutes
|
|
10
|
-
const USERID_LOCAL_STORAGE_KEY = 'aicore.analytics.userID';
|
|
11
|
-
const POST_LARGE_DATA_THRESHOLD_BYTES = 10000;
|
|
12
|
-
let currentAnalyticsEvent = null;
|
|
13
|
-
const IS_NODE_ENV = (typeof window === 'undefined');
|
|
14
|
-
let DEFAULT_BASE_URL = "https://analytics.core.ai";
|
|
15
6
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
let debugMode = false;
|
|
21
|
-
|
|
22
|
-
function debugLog(...args) {
|
|
23
|
-
if(!debugMode){
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
console.log(...args);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function debugError(...args) {
|
|
30
|
-
if(!debugMode){
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
console.error(...args);
|
|
7
|
+
if(!window.analytics){
|
|
8
|
+
window.analytics = {
|
|
9
|
+
_initData: []
|
|
10
|
+
};
|
|
34
11
|
}
|
|
35
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the analytics session
|
|
15
|
+
* @param accountIDInit Your analytics account id as configured in the server or core.ai analytics
|
|
16
|
+
* @param appNameInit The app name to log the events against.
|
|
17
|
+
* @param analyticsURLInit Optional: Provide your own analytics server address if you self-hosted the server
|
|
18
|
+
* @param postIntervalSecondsInit Optional: This defines the interval between sending analytics events to the server.
|
|
19
|
+
* Default is 10 minutes
|
|
20
|
+
* @param granularitySecInit Optional: The smallest time period under which the events can be distinguished. Multiple
|
|
21
|
+
* events happening during this time period is aggregated to a count. The default granularity is 3 Seconds, which means
|
|
22
|
+
* 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
|
+
*/
|
|
25
|
+
function initAnalyticsSession(accountIDInit, appNameInit, analyticsURLInit,
|
|
26
|
+
postIntervalSecondsInit, granularitySecInit, debug) {
|
|
27
|
+
let accountID, appName, userID, sessionID, postIntervalSeconds,
|
|
28
|
+
granularitySec, analyticsURL, postURL, serverConfig={};
|
|
29
|
+
const DEFAULT_GRANULARITY_IN_SECONDS = 3;
|
|
30
|
+
const DEFAULT_RETRY_TIME_IN_SECONDS = 30;
|
|
31
|
+
const DEFAULT_POST_INTERVAL_SECONDS = 600; // 10 minutes
|
|
32
|
+
const USERID_LOCAL_STORAGE_KEY = 'aicore.analytics.userID';
|
|
33
|
+
const SESSION_ID_LOCAL_STORAGE_KEY = 'aicore.analytics.sessionID';
|
|
34
|
+
const POST_LARGE_DATA_THRESHOLD_BYTES = 10000;
|
|
35
|
+
let currentAnalyticsEvent = null;
|
|
36
|
+
const IS_NODE_ENV = (typeof window === 'undefined');
|
|
37
|
+
let DEFAULT_BASE_URL = "https://analytics.core.ai";
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
let granularityTimer;
|
|
40
|
+
let postTimer;
|
|
41
|
+
let currentQuantisedTime = 0;
|
|
42
|
+
let disabled = false;
|
|
43
|
+
let debugMode = false;
|
|
40
44
|
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
sessionID: sessionID,
|
|
48
|
-
unixTimestampUTC: +new Date(),
|
|
49
|
-
numEventsTotal: 0,
|
|
50
|
-
events: {}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
45
|
+
function debugLog(...args) {
|
|
46
|
+
if(!debugMode){
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
console.log(...args);
|
|
50
|
+
}
|
|
53
51
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
function debugError(...args) {
|
|
53
|
+
if(!debugMode){
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
console.error(...args);
|
|
57
57
|
}
|
|
58
|
-
}
|
|
59
58
|
|
|
60
|
-
function getCurrentAnalyticsEvent() {
|
|
61
|
-
_validateCurrentState();
|
|
62
|
-
// return a clone
|
|
63
|
-
return JSON.parse(JSON.stringify(currentAnalyticsEvent));
|
|
64
|
-
}
|
|
65
59
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if(!localUserID){
|
|
69
|
-
localUserID = crypto.randomUUID();
|
|
70
|
-
localStorage.setItem(USERID_LOCAL_STORAGE_KEY, localUserID);
|
|
60
|
+
if(IS_NODE_ENV){
|
|
61
|
+
throw new Error("Node environment is not currently supported");
|
|
71
62
|
}
|
|
72
|
-
return localUserID;
|
|
73
|
-
}
|
|
74
63
|
|
|
75
|
-
function
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
function _createAnalyticsEvent() {
|
|
65
|
+
return {
|
|
66
|
+
schemaVersion: 1,
|
|
67
|
+
accountID: accountID,
|
|
68
|
+
appName: appName,
|
|
69
|
+
uuid: userID,
|
|
70
|
+
sessionID: sessionID,
|
|
71
|
+
unixTimestampUTC: +new Date(),
|
|
72
|
+
numEventsTotal: 0,
|
|
73
|
+
events: {}
|
|
74
|
+
};
|
|
80
75
|
}
|
|
81
|
-
return localSessionID;
|
|
82
|
-
}
|
|
83
76
|
|
|
84
|
-
function
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
77
|
+
function _validateCurrentState() {
|
|
78
|
+
if(!currentAnalyticsEvent){
|
|
79
|
+
throw new Error("Please call initSession before using any analytics event");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
88
82
|
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
_postCurrentAnalyticsEvent(eventToSend);
|
|
95
|
-
}, DEFAULT_RETRY_TIME_IN_SECONDS * 1000 * eventToSend.backoffCount);
|
|
96
|
-
}
|
|
83
|
+
function _getCurrentAnalyticsEvent() {
|
|
84
|
+
_validateCurrentState();
|
|
85
|
+
// return a clone
|
|
86
|
+
return JSON.parse(JSON.stringify(currentAnalyticsEvent));
|
|
87
|
+
}
|
|
97
88
|
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
89
|
+
function _getOrCreateUserID() {
|
|
90
|
+
let localUserID = localStorage.getItem(USERID_LOCAL_STORAGE_KEY);
|
|
91
|
+
if(!localUserID){
|
|
92
|
+
localUserID = crypto.randomUUID();
|
|
93
|
+
localStorage.setItem(USERID_LOCAL_STORAGE_KEY, localUserID);
|
|
94
|
+
}
|
|
95
|
+
return localUserID;
|
|
101
96
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
97
|
+
|
|
98
|
+
function _getOrCreateSessionID() {
|
|
99
|
+
let localSessionID = sessionStorage.getItem(SESSION_ID_LOCAL_STORAGE_KEY);
|
|
100
|
+
if(!localSessionID){
|
|
101
|
+
localSessionID = Math.random().toString(36).substr(2, 10);
|
|
102
|
+
sessionStorage.setItem(SESSION_ID_LOCAL_STORAGE_KEY, localSessionID);
|
|
103
|
+
}
|
|
104
|
+
return localSessionID;
|
|
107
105
|
}
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
|
|
107
|
+
function _setupIDs() {
|
|
108
|
+
userID = _getOrCreateUserID();
|
|
109
|
+
sessionID = _getOrCreateSessionID();
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
|
|
112
|
+
function _retryPost(eventToSend) {
|
|
113
|
+
eventToSend.backoffCount = (eventToSend.backoffCount || 0) + 1;
|
|
114
|
+
debugLog(`Failed to call core analytics server. Will retry in ${
|
|
115
|
+
DEFAULT_RETRY_TIME_IN_SECONDS * eventToSend.backoffCount}s: `);
|
|
116
|
+
setTimeout(()=>{
|
|
117
|
+
_postCurrentAnalyticsEvent(eventToSend);
|
|
118
|
+
}, DEFAULT_RETRY_TIME_IN_SECONDS * 1000 * eventToSend.backoffCount);
|
|
115
119
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
headers: {'Content-Type': 'application/json'},
|
|
120
|
-
body: textToSend
|
|
121
|
-
}).then(res=>{
|
|
122
|
-
if(res.status === 200){
|
|
120
|
+
|
|
121
|
+
function _postCurrentAnalyticsEvent(eventToSend) {
|
|
122
|
+
if(disabled){
|
|
123
123
|
return;
|
|
124
124
|
}
|
|
125
|
-
if(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
if(!eventToSend){
|
|
126
|
+
eventToSend = currentAnalyticsEvent;
|
|
127
|
+
currentQuantisedTime = 0;
|
|
128
|
+
_resetGranularityTimer();
|
|
129
|
+
currentAnalyticsEvent = _createAnalyticsEvent();
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
131
|
+
if(eventToSend.numEventsTotal === 0 ){
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
let textToSend = JSON.stringify(eventToSend);
|
|
135
|
+
if(textToSend.length > POST_LARGE_DATA_THRESHOLD_BYTES){
|
|
136
|
+
console.warn(`Analytics event generated is very large at greater than ${textToSend.length}B. This
|
|
137
|
+
typically means that you may be sending too many value events? .`);
|
|
138
|
+
}
|
|
139
|
+
debugLog("Sending Analytics data of length: ", textToSend.length, "B");
|
|
140
|
+
window.fetch(postURL, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
headers: {'Content-Type': 'application/json'},
|
|
143
|
+
body: textToSend
|
|
144
|
+
}).then(res=>{
|
|
145
|
+
if(res.status === 200){
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if(res.status !== 400){ // we don't retry bad requests
|
|
149
|
+
_retryPost(eventToSend);
|
|
150
|
+
} else {
|
|
151
|
+
console.error("Analytics client: " +
|
|
152
|
+
"Bad Request, this is most likely a problem with the library, update to latest version.");
|
|
153
|
+
}
|
|
154
|
+
}).catch(res => {
|
|
155
|
+
debugError(res);
|
|
156
|
+
_retryPost(eventToSend);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
136
159
|
|
|
137
|
-
function _resetGranularityTimer(disable) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
160
|
+
function _resetGranularityTimer(disable) {
|
|
161
|
+
if(granularityTimer){
|
|
162
|
+
clearInterval(granularityTimer);
|
|
163
|
+
granularityTimer = null;
|
|
164
|
+
}
|
|
165
|
+
if(disable){
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
granularityTimer = setInterval(()=>{
|
|
169
|
+
currentQuantisedTime = currentQuantisedTime + granularitySec;
|
|
170
|
+
}, granularitySec*1000);
|
|
141
171
|
}
|
|
142
|
-
|
|
143
|
-
|
|
172
|
+
|
|
173
|
+
function _setupTimers(disable) {
|
|
174
|
+
_resetGranularityTimer(disable);
|
|
175
|
+
if(postTimer){
|
|
176
|
+
clearInterval(postTimer);
|
|
177
|
+
postTimer = null;
|
|
178
|
+
}
|
|
179
|
+
if(disable){
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
postTimer = setInterval(_postCurrentAnalyticsEvent, postIntervalSeconds*1000);
|
|
144
183
|
}
|
|
145
|
-
granularityTimer = setInterval(()=>{
|
|
146
|
-
currentQuantisedTime = currentQuantisedTime + granularitySec;
|
|
147
|
-
}, granularitySec*1000);
|
|
148
|
-
}
|
|
149
184
|
|
|
150
|
-
function
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
185
|
+
async function _getServerConfig() {
|
|
186
|
+
return new Promise((resolve, reject)=>{
|
|
187
|
+
let configURL = analyticsURL + `/getAppConfig?accountID=${accountID}&appName=${appName}`;
|
|
188
|
+
window.fetch(configURL).then(async res=>{
|
|
189
|
+
switch (res.status) {
|
|
190
|
+
case 200:
|
|
191
|
+
let serverResponse = await res.json();
|
|
192
|
+
resolve(serverResponse);
|
|
193
|
+
return;
|
|
194
|
+
case 400:
|
|
195
|
+
reject("Bad Request, check library version compatible?", res);
|
|
196
|
+
break;
|
|
197
|
+
default:
|
|
198
|
+
reject("analytics client: Could not update from remote config. Continuing with defaults.", res);
|
|
199
|
+
}
|
|
200
|
+
}).catch(err => {
|
|
201
|
+
reject("analytics client: Could not update from remote config. Continuing with defaults.", err);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
155
204
|
}
|
|
156
|
-
|
|
157
|
-
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Returns the analytics config for the app
|
|
208
|
+
* @returns {Object}
|
|
209
|
+
*/
|
|
210
|
+
function _getAppConfig() {
|
|
211
|
+
return {
|
|
212
|
+
accountID, appName, disabled,
|
|
213
|
+
uuid: userID, sessionID,
|
|
214
|
+
postIntervalSeconds, granularitySec, analyticsURL, serverConfig
|
|
215
|
+
};
|
|
158
216
|
}
|
|
159
|
-
postTimer = setInterval(_postCurrentAnalyticsEvent, postIntervalSeconds*1000);
|
|
160
|
-
}
|
|
161
217
|
|
|
162
|
-
async function
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
218
|
+
async function _initFromRemoteConfig(postIntervalSecondsInitial, granularitySecInitial) {
|
|
219
|
+
serverConfig = await _getServerConfig();
|
|
220
|
+
if(serverConfig !== {}){
|
|
221
|
+
// User init overrides takes precedence over server overrides
|
|
222
|
+
postIntervalSeconds = postIntervalSecondsInitial ||
|
|
223
|
+
serverConfig["postIntervalSecondsInit"] || DEFAULT_POST_INTERVAL_SECONDS;
|
|
224
|
+
granularitySec = granularitySecInitial || serverConfig["granularitySecInit"] || DEFAULT_GRANULARITY_IN_SECONDS;
|
|
225
|
+
// For URLs, the server suggested URL takes precedence over user init values
|
|
226
|
+
analyticsURL = serverConfig["analyticsURLInit"] || analyticsURL || DEFAULT_BASE_URL;
|
|
227
|
+
disabled = serverConfig["disabled"] === true;
|
|
228
|
+
_setupTimers(disabled);
|
|
229
|
+
debugLog(`Init analytics Config from remote. disabled: ${disabled}
|
|
230
|
+
postIntervalSeconds:${postIntervalSeconds}, granularitySec: ${granularitySec} ,URL: ${analyticsURL}`);
|
|
231
|
+
if(disabled){
|
|
232
|
+
console.warn(`Core Analytics is disabled from the server for app: ${accountID}:${appName}`);
|
|
176
233
|
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
182
236
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
*/
|
|
187
|
-
function getAppConfig() {
|
|
188
|
-
return {
|
|
189
|
-
accountID, appName, disabled,
|
|
190
|
-
uuid: userID, sessionID,
|
|
191
|
-
postIntervalSeconds, granularitySec, analyticsURL, serverConfig
|
|
192
|
-
};
|
|
193
|
-
}
|
|
237
|
+
function _stripTrailingSlash(url) {
|
|
238
|
+
return url.replace(/\/$/, "");
|
|
239
|
+
}
|
|
194
240
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
241
|
+
function _ensureAnalyticsEventExists(eventType, category, subCategory) {
|
|
242
|
+
let events = currentAnalyticsEvent.events;
|
|
243
|
+
events[eventType] = events[eventType] || {};
|
|
244
|
+
events[eventType][category] = events[eventType][category] || {};
|
|
245
|
+
events[eventType][category][subCategory] = events[eventType][category][subCategory] || {
|
|
246
|
+
time: [], // quantised time
|
|
247
|
+
valueCount: [] // value and count array, If a single value, then it is count, else object {"val1":count1, ...}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function _validateEvent(eventType, category, subCategory, count, value) {
|
|
252
|
+
_validateCurrentState();
|
|
253
|
+
if(!eventType || !category || !subCategory){
|
|
254
|
+
throw new Error("missing eventType or category or subCategory");
|
|
255
|
+
}
|
|
256
|
+
if(typeof(count)!== 'number' || count <0){
|
|
257
|
+
throw new Error("invalid count, count should be a positive number");
|
|
258
|
+
}
|
|
259
|
+
if(typeof(value)!== 'number'){
|
|
260
|
+
throw new Error("invalid value, value should be a number");
|
|
210
261
|
}
|
|
211
262
|
}
|
|
212
|
-
}
|
|
213
263
|
|
|
214
|
-
function
|
|
215
|
-
|
|
216
|
-
|
|
264
|
+
function _updateExistingAnalyticsEvent(index, eventType, category, subCategory, count, newValue) {
|
|
265
|
+
let events = currentAnalyticsEvent.events;
|
|
266
|
+
const storedValueIsCount = typeof(events[eventType][category][subCategory]["valueCount"][index]) === 'number';
|
|
267
|
+
if(storedValueIsCount && newValue === 0){
|
|
268
|
+
events[eventType][category][subCategory]["valueCount"][index] += count;
|
|
269
|
+
} else if(storedValueIsCount && newValue !== 0){
|
|
270
|
+
let newValueCount = {};
|
|
271
|
+
newValueCount[newValue] = count;
|
|
272
|
+
newValueCount[0] = events[eventType][category][subCategory]["valueCount"][index];
|
|
273
|
+
events[eventType][category][subCategory]["valueCount"][index] = newValueCount;
|
|
274
|
+
} else if(!storedValueIsCount){
|
|
275
|
+
let storedValueObject = events[eventType][category][subCategory]["valueCount"][index];
|
|
276
|
+
storedValueObject[newValue] = (storedValueObject[newValue] || 0) + count;
|
|
277
|
+
}
|
|
278
|
+
currentAnalyticsEvent.numEventsTotal += 1;
|
|
279
|
+
}
|
|
217
280
|
|
|
218
|
-
/**
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Register an analytics event. The events will be aggregated and send to the analytics server periodically.
|
|
283
|
+
* @param eventType - String, required
|
|
284
|
+
* @param eventCategory - String, required
|
|
285
|
+
* @param subCategory - String, required
|
|
286
|
+
* @param eventCount (Optional) : A non-negative number indicating the number of times the event (or an event with a
|
|
287
|
+
* particular value if a value is specified) happened. defaults to 1.
|
|
288
|
+
* @param eventValue (Optional) : A number value associated with the event. defaults to 0
|
|
289
|
+
*/
|
|
290
|
+
function event(eventType, eventCategory, subCategory, eventCount=1, eventValue=0) {
|
|
291
|
+
if(disabled){
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
_validateEvent(eventType, eventCategory, subCategory, eventCount, eventValue);
|
|
295
|
+
_ensureAnalyticsEventExists(eventType, eventCategory, subCategory);
|
|
296
|
+
let events = currentAnalyticsEvent.events;
|
|
297
|
+
let timeArray = events[eventType][eventCategory][subCategory]["time"];
|
|
298
|
+
let lastTime = timeArray.length>0? timeArray[timeArray.length-1] : null;
|
|
299
|
+
if(lastTime !== currentQuantisedTime){
|
|
300
|
+
events[eventType][eventCategory][subCategory]["time"].push(currentQuantisedTime);
|
|
301
|
+
if(eventValue===0){
|
|
302
|
+
events[eventType][eventCategory][subCategory]["valueCount"].push(eventCount);
|
|
303
|
+
} else {
|
|
304
|
+
let valueCount = {};
|
|
305
|
+
valueCount[eventValue] = eventCount;
|
|
306
|
+
events[eventType][eventCategory][subCategory]["valueCount"].push(valueCount);
|
|
307
|
+
}
|
|
308
|
+
currentAnalyticsEvent.numEventsTotal += 1;
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
let modificationIndex = events[eventType][eventCategory][subCategory]["valueCount"].length -1;
|
|
312
|
+
_updateExistingAnalyticsEvent(modificationIndex, eventType, eventCategory, subCategory, eventCount, eventValue);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Init Analytics
|
|
231
316
|
if(!accountIDInit || !appNameInit){
|
|
232
317
|
throw new Error("accountID and appName must exist for init");
|
|
233
318
|
}
|
|
@@ -242,85 +327,20 @@ function initSession(accountIDInit, appNameInit, analyticsURLInit, postIntervalS
|
|
|
242
327
|
currentAnalyticsEvent = _createAnalyticsEvent();
|
|
243
328
|
_setupTimers();
|
|
244
329
|
_initFromRemoteConfig(postIntervalSecondsInit, granularitySecInit);
|
|
245
|
-
}
|
|
246
330
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
valueCount: [] // value and count array, If a single value, then it is count, else object {"val1":count1, ...}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function _validateEvent(eventType, category, subCategory, count, value) {
|
|
258
|
-
_validateCurrentState();
|
|
259
|
-
if(!eventType || !category || !subCategory){
|
|
260
|
-
throw new Error("missing eventType or category or subCategory");
|
|
261
|
-
}
|
|
262
|
-
if(typeof(count)!== 'number' || count <0){
|
|
263
|
-
throw new Error("invalid count");
|
|
331
|
+
// As analytics lib load is async, our loading scripts will push event data into initData array till the lib load
|
|
332
|
+
// is complete. Now as load is done, we need to push these pending analytics events. Assuming that the async load of
|
|
333
|
+
// the lib should mostly happen under a second, we should not lose any accuracy of event times within the initial
|
|
334
|
+
// 3-second granularity. For more precision use cases, load the lib sync.
|
|
335
|
+
for(let eventData of analytics._initData){
|
|
336
|
+
event(...eventData);
|
|
264
337
|
}
|
|
265
|
-
|
|
266
|
-
throw new Error("invalid value");
|
|
267
|
-
}
|
|
268
|
-
}
|
|
338
|
+
analytics._initData = [];
|
|
269
339
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if(storedValueIsCount && newValue === 0){
|
|
274
|
-
events[eventType][category][subCategory]["valueCount"][index] += count;
|
|
275
|
-
} else if(storedValueIsCount && newValue !== 0){
|
|
276
|
-
let newValueCount = {};
|
|
277
|
-
newValueCount[newValue] = count;
|
|
278
|
-
newValueCount[0] = events[eventType][category][subCategory]["valueCount"][index];
|
|
279
|
-
events[eventType][category][subCategory]["valueCount"][index] = newValueCount;
|
|
280
|
-
} else if(!storedValueIsCount){
|
|
281
|
-
let storedValueObject = events[eventType][category][subCategory]["valueCount"][index];
|
|
282
|
-
storedValueObject[newValue] = (storedValueObject[newValue] || 0) + count;
|
|
283
|
-
}
|
|
284
|
-
currentAnalyticsEvent.numEventsTotal += 1;
|
|
285
|
-
}
|
|
340
|
+
// Private API for tests
|
|
341
|
+
analytics._getCurrentAnalyticsEvent = _getCurrentAnalyticsEvent;
|
|
342
|
+
analytics._getAppConfig = _getAppConfig;
|
|
286
343
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
* @param eventType - String, required
|
|
290
|
-
* @param eventCategory - String, required
|
|
291
|
-
* @param subCategory - String, required
|
|
292
|
-
* @param eventCount (Optional) : A non-negative number indicating the number of times the event (or an event with a
|
|
293
|
-
* particular value if a value is specified) happened. defaults to 1.
|
|
294
|
-
* @param eventValue (Optional) : A number value associated with the event. defaults to 0
|
|
295
|
-
*/
|
|
296
|
-
function analyticsEvent(eventType, eventCategory, subCategory, eventCount=1, eventValue=0) {
|
|
297
|
-
if(disabled){
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
_validateEvent(eventType, eventCategory, subCategory, eventCount, eventValue);
|
|
301
|
-
_ensureAnalyticsEventExists(eventType, eventCategory, subCategory);
|
|
302
|
-
let events = currentAnalyticsEvent.events;
|
|
303
|
-
let timeArray = events[eventType][eventCategory][subCategory]["time"];
|
|
304
|
-
let lastTime = timeArray.length>0? timeArray[timeArray.length-1] : null;
|
|
305
|
-
if(lastTime !== currentQuantisedTime){
|
|
306
|
-
events[eventType][eventCategory][subCategory]["time"].push(currentQuantisedTime);
|
|
307
|
-
if(eventValue===0){
|
|
308
|
-
events[eventType][eventCategory][subCategory]["valueCount"].push(eventCount);
|
|
309
|
-
} else {
|
|
310
|
-
let valueCount = {};
|
|
311
|
-
valueCount[eventValue] = eventCount;
|
|
312
|
-
events[eventType][eventCategory][subCategory]["valueCount"].push(valueCount);
|
|
313
|
-
}
|
|
314
|
-
currentAnalyticsEvent.numEventsTotal += 1;
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
let modificationIndex = events[eventType][eventCategory][subCategory]["valueCount"].length -1;
|
|
318
|
-
_updateExistingAnalyticsEvent(modificationIndex, eventType, eventCategory, subCategory, eventCount, eventValue);
|
|
344
|
+
// Public API
|
|
345
|
+
analytics.event = event;
|
|
319
346
|
}
|
|
320
|
-
|
|
321
|
-
export {
|
|
322
|
-
initSession,
|
|
323
|
-
getCurrentAnalyticsEvent,
|
|
324
|
-
analyticsEvent,
|
|
325
|
-
getAppConfig
|
|
326
|
-
};
|