sn-signals 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2021
2
+
3
+ Permission is hereby granted, free
4
+ of charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import GlideSignals from './src/signals';
2
+
3
+ export default GlideSignals;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "sn-signals",
3
+ "version": "0.0.11",
4
+ "private": false,
5
+ "description": "",
6
+ "license": "MIT",
7
+ "author": "hns-signal",
8
+ "main": "index.js",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "scripts": {
13
+ "mkdir": "node mkdir.js",
14
+ "build": "npm run mkdir && npx babel src --out-dir lib",
15
+ "prepublishOnly": "npm run build",
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ },
18
+ "devDependencies": {
19
+ "babel-cli": "6.26.0",
20
+ "babel-core": "6.26.0",
21
+ "babel-preset-env": "1.6.1",
22
+ "babel-plugin-transform-object-rest-spread": "6.26.0"
23
+ }
24
+ }
package/src/queue.js ADDED
@@ -0,0 +1,47 @@
1
+ const API_URL = '/api/now/analytics/events';
2
+ const queueLimit = 100;
3
+ const timeout = 3000;
4
+
5
+ let eventQueue = [];
6
+ let timer = null;
7
+
8
+ const postAllEvents = () => {
9
+ eventQueue.forEach(function(event) {
10
+ postEvent(event);
11
+ });
12
+
13
+ eventQueue = [];
14
+ timer = null;
15
+ }
16
+
17
+ const postEvent = ({ eventAction, eventPriority, eventBody }) => {
18
+ const xmlhttp = new XMLHttpRequest();
19
+ const queryParams = `?sysparm_event_action=${eventAction}&sysparm_event_priority=${eventPriority}`;
20
+
21
+ xmlhttp.open('POST', API_URL + queryParams);
22
+ xmlhttp.setRequestHeader('Content-Type', 'application/json');
23
+
24
+ if (window.g_ck)
25
+ xmlhttp.setRequestHeader('X-UserToken', g_ck);
26
+
27
+ xmlhttp.send(JSON.stringify(eventBody));
28
+ }
29
+
30
+ const pushEvent = (event) => {
31
+ eventQueue.push(event);
32
+
33
+ if (eventQueue.length === queueLimit)
34
+ postAllEvents();
35
+ else {
36
+ // timer is already present we just need to add to the queue
37
+ // until it flushes itself
38
+ if (timer)
39
+ return;
40
+
41
+ timer = setTimeout(function() {
42
+ postAllEvents();
43
+ }, timeout);
44
+ }
45
+ }
46
+
47
+ export default pushEvent;
package/src/signals.js ADDED
@@ -0,0 +1,58 @@
1
+ import { getUserInfo, fetchUserLocationAsync } from './user';
2
+ import pushEvent from './queue';
3
+
4
+ /**
5
+ * Glide Signals to track events and collect user analytics
6
+ *
7
+ * #trackEvent(action, priority, eventModel, delayInMs) allows to send a custom event to Glide Signals
8
+ */
9
+ const GlideSignals = {
10
+ _location: {},
11
+ priority: {
12
+ INFO: 'INFO',
13
+ WARN: 'WARN',
14
+ ERROR: 'ERROR',
15
+ },
16
+
17
+ postContextProperty(propertyName, propertyVal) {
18
+ const xmlhttp = new XMLHttpRequest();
19
+ const uri = '/api/now/analytics/updateContext';
20
+
21
+ const body = {
22
+ sys_context_property_name: propertyName,
23
+ sys_context_property_value: propertyVal,
24
+ };
25
+ xmlhttp.open('POST', uri);
26
+ xmlhttp.setRequestHeader('Content-Type', 'application/json');
27
+
28
+ if (window.g_ck) xmlhttp.setRequestHeader('X-UserToken', g_ck);
29
+
30
+ xmlhttp.send(JSON.stringify(body));
31
+ },
32
+
33
+ trackEvent(eventAction, eventPriority, eventModel) {
34
+ const user = getUserInfo(this._location);
35
+ pushEvent({
36
+ eventAction,
37
+ eventPriority,
38
+ eventBody: {
39
+ ...eventModel,
40
+ ...user,
41
+ },
42
+ });
43
+ },
44
+
45
+ async init() {
46
+ try {
47
+ this._location = await fetchUserLocationAsync();
48
+ if (this._location.hasOwnProperty('latitude')) {
49
+ this.postContextProperty('latitude', this._location.latitude);
50
+ }
51
+ if (this._location.hasOwnProperty('longitude')) {
52
+ this.postContextProperty('longitude', this._location.longitude);
53
+ }
54
+ } catch (e) {}
55
+ },
56
+ };
57
+
58
+ export default GlideSignals;
package/src/user.js ADDED
@@ -0,0 +1,46 @@
1
+ const ONE_MINUTE = 60000; // milliseconds
2
+
3
+ const getUserInfo = location => ({
4
+ userSessionId: getUserSessionId(),
5
+ userLanguage: getUserLanguage(),
6
+ browserInfo: getUserBrowserInfo(),
7
+ latitude: location.hasOwnProperty('latitude') ? location.latitude : null,
8
+ longitude: location.hasOwnProperty('longitude') ? location.longitude : null,
9
+ });
10
+
11
+ const getUserSessionId = () => {
12
+ return window.NOW && window.NOW.session_id ? window.NOW.session_id : '';
13
+ };
14
+
15
+ const getUserLanguage = () => {
16
+ if (window.NOW && window.NOW.language && window.NOW.language != '')
17
+ return window.NOW.language;
18
+
19
+ return '';
20
+ };
21
+
22
+ const getUserBrowserInfo = () => {
23
+ return window.navigator.userAgent;
24
+ };
25
+
26
+ async function fetchUserLocationAsync() {
27
+ return new Promise((resolve, reject) => {
28
+ const onSuccess = pos => {
29
+ resolve({
30
+ latitude: pos.coords.latitude,
31
+ longitude: pos.coords.longitude,
32
+ });
33
+ };
34
+
35
+ const onError = () => {
36
+ reject({ latitude: null, longitude: null });
37
+ };
38
+
39
+ window.navigator.geolocation.getCurrentPosition(onSuccess, onError, {
40
+ enableHighAccuracy: true,
41
+ timeout: ONE_MINUTE,
42
+ });
43
+ });
44
+ }
45
+
46
+ export { getUserInfo, fetchUserLocationAsync };