Skip to content

Commit 9802c6d

Browse files
committed
Merge branch 'build/npm' into 'develop'
feat: * Adds additional event types * Adds a session DataContext for capturing user-session details build: * Adds dist directory for tsc built JS files * Adds source maps * Adds type definitions docs: * Updates docs packages: * Moves antD and styled-components to dev-dependencies
2 parents aabc891 + 9664670 commit 9802c6d

File tree

64 files changed

+42044
-14409
lines changed

Some content is hidden

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

64 files changed

+42044
-14409
lines changed

dist/browser/storage.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import UserInteractionResource from '../resources/userInteractionResource';
2+
export declare function retrieveData(key: string): Promise<unknown>;
3+
export declare function saveData(key: string, data: UserInteractionResource[]): Promise<void>;
4+
export declare function clearData(): void;
5+
export declare function initializeBeacon(dataKey: string, apiUrl: string): Promise<void>;
6+
//# sourceMappingURL=storage.d.ts.map

dist/browser/storage.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/browser/storage.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __generator = (this && this.__generator) || function (thisArg, body) {
11+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13+
function verb(n) { return function (v) { return step([n, v]); }; }
14+
function step(op) {
15+
if (f) throw new TypeError("Generator is already executing.");
16+
while (_) try {
17+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18+
if (y = 0, t) op = [op[0] & 2, t.value];
19+
switch (op[0]) {
20+
case 0: case 1: t = op; break;
21+
case 4: _.label++; return { value: op[1], done: false };
22+
case 5: _.label++; y = op[1]; op = [0]; continue;
23+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
24+
default:
25+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29+
if (t[2]) _.ops.pop();
30+
_.trys.pop(); continue;
31+
}
32+
op = body.call(thisArg, _);
33+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35+
}
36+
};
37+
import localforage from 'localforage';
38+
export function retrieveData(key) {
39+
return localforage.getItem(key);
40+
}
41+
export function saveData(key, data) {
42+
return localforage.setItem(key, data)
43+
.then(function (result) {
44+
// console.log(result)
45+
})
46+
.catch(function (err) {
47+
console.error("Error saving data", err);
48+
});
49+
}
50+
export function clearData() {
51+
localforage.clear();
52+
}
53+
export function initializeBeacon(dataKey, apiUrl) {
54+
return __awaiter(this, void 0, void 0, function () {
55+
return __generator(this, function (_a) {
56+
document.addEventListener('visibilitychange', function () {
57+
return __awaiter(this, void 0, void 0, function () {
58+
var eventsData, blob, returnValue;
59+
return __generator(this, function (_a) {
60+
switch (_a.label) {
61+
case 0:
62+
if (!(document.visibilityState === 'hidden')) return [3 /*break*/, 2];
63+
return [4 /*yield*/, retrieveData(dataKey)];
64+
case 1:
65+
eventsData = _a.sent();
66+
if (eventsData) {
67+
blob = new Blob([JSON.stringify(eventsData)], { type: "text/plain" });
68+
returnValue = navigator.sendBeacon(apiUrl, blob);
69+
if (returnValue) {
70+
clearData();
71+
}
72+
}
73+
_a.label = 2;
74+
case 2: return [2 /*return*/];
75+
}
76+
});
77+
});
78+
});
79+
return [2 /*return*/];
80+
});
81+
});
82+
}

dist/browser/user-interaction.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import UserInteractionResource from '../resources/userInteractionResource';
2+
export interface StorageSettings {
3+
resourceLimit: number;
4+
ttl: number;
5+
apiUrl: string;
6+
dataKey?: string;
7+
}
8+
export declare function start(options: StorageSettings): Promise<void>;
9+
export declare function init(options: StorageSettings): void;
10+
export declare function getConfig(): Required<StorageSettings>;
11+
export declare function handle(data: UserInteractionResource): Promise<void>;
12+
export declare function onAppClose(): void;
13+
export declare function syncData(url: string, data: Uint8Array): Promise<any>;
14+
//# sourceMappingURL=user-interaction.d.ts.map

dist/browser/user-interaction.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/browser/user-interaction.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
var __assign = (this && this.__assign) || function () {
2+
__assign = Object.assign || function(t) {
3+
for (var s, i = 1, n = arguments.length; i < n; i++) {
4+
s = arguments[i];
5+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6+
t[p] = s[p];
7+
}
8+
return t;
9+
};
10+
return __assign.apply(this, arguments);
11+
};
12+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14+
return new (P || (P = Promise))(function (resolve, reject) {
15+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18+
step((generator = generator.apply(thisArg, _arguments || [])).next());
19+
});
20+
};
21+
var __generator = (this && this.__generator) || function (thisArg, body) {
22+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24+
function verb(n) { return function (v) { return step([n, v]); }; }
25+
function step(op) {
26+
if (f) throw new TypeError("Generator is already executing.");
27+
while (_) try {
28+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29+
if (y = 0, t) op = [op[0] & 2, t.value];
30+
switch (op[0]) {
31+
case 0: case 1: t = op; break;
32+
case 4: _.label++; return { value: op[1], done: false };
33+
case 5: _.label++; y = op[1]; op = [0]; continue;
34+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
35+
default:
36+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40+
if (t[2]) _.ops.pop();
41+
_.trys.pop(); continue;
42+
}
43+
op = body.call(thisArg, _);
44+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46+
}
47+
};
48+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
49+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
50+
to[j] = from[i];
51+
return to;
52+
};
53+
import { gzip } from '../data-processing/compression';
54+
import { retrieveData, saveData, clearData } from '../browser/storage';
55+
(function (self) {
56+
console.log("WorkerGlobalScope: ", WorkerGlobalScope);
57+
console.log("self: ", self);
58+
})(self);
59+
var STORAGE_SETTINGS_DEFAULTS = {
60+
resourceLimit: 0,
61+
ttl: 5000,
62+
apiUrl: "",
63+
dataKey: "events",
64+
};
65+
var storageSettings = STORAGE_SETTINGS_DEFAULTS;
66+
var isAppLoaded = true;
67+
export function start(options) {
68+
return __awaiter(this, void 0, void 0, function () {
69+
return __generator(this, function (_a) {
70+
init(options);
71+
reAttemptSync(storageSettings.apiUrl, storageSettings.ttl, storageSettings.dataKey);
72+
return [2 /*return*/];
73+
});
74+
});
75+
}
76+
export function init(options) {
77+
if (storageSettings.resourceLimit === 0) {
78+
storageSettings = __assign(__assign({}, options), { dataKey: options.dataKey ? options.dataKey : storageSettings.dataKey });
79+
}
80+
}
81+
export function getConfig() {
82+
return storageSettings;
83+
}
84+
export function handle(data) {
85+
return __awaiter(this, void 0, void 0, function () {
86+
var eventsData, compressedData;
87+
return __generator(this, function (_a) {
88+
switch (_a.label) {
89+
case 0: return [4 /*yield*/, retrieveData(storageSettings.dataKey)];
90+
case 1:
91+
eventsData = _a.sent();
92+
if (eventsData) {
93+
if (eventsData.length < storageSettings.resourceLimit) {
94+
saveData(storageSettings.dataKey, __spreadArray([data], eventsData));
95+
}
96+
else {
97+
if (self.navigator.onLine) {
98+
compressedData = gzip(eventsData);
99+
syncData(storageSettings.apiUrl, compressedData)
100+
.then(function (res) {
101+
clearData();
102+
saveData(storageSettings.dataKey, [data]);
103+
})
104+
.catch(function (err) {
105+
saveData(storageSettings.dataKey, __spreadArray([data], eventsData));
106+
});
107+
}
108+
else {
109+
saveData(storageSettings.dataKey, __spreadArray([data], eventsData));
110+
}
111+
}
112+
}
113+
else {
114+
saveData(storageSettings.dataKey, [data]);
115+
}
116+
return [2 /*return*/];
117+
}
118+
});
119+
});
120+
}
121+
export function onAppClose() {
122+
isAppLoaded = false;
123+
reAttemptSync(storageSettings.apiUrl, storageSettings.ttl, storageSettings.dataKey);
124+
}
125+
function reAttemptSync(url, ttl, key) {
126+
function compressAndSend() {
127+
return __awaiter(this, void 0, void 0, function () {
128+
var eventsData, compressedData;
129+
return __generator(this, function (_a) {
130+
switch (_a.label) {
131+
case 0:
132+
if (!self.navigator.onLine) return [3 /*break*/, 2];
133+
return [4 /*yield*/, retrieveData(key)];
134+
case 1:
135+
eventsData = _a.sent();
136+
if (eventsData) {
137+
compressedData = gzip(eventsData);
138+
syncData(url, compressedData)
139+
.then(function (res) {
140+
clearData();
141+
})
142+
.catch(function (err) {
143+
console.log("error is", err);
144+
});
145+
}
146+
else {
147+
if (!isAppLoaded) {
148+
// @ts-ignore
149+
self.close(); // kill web worker
150+
}
151+
}
152+
_a.label = 2;
153+
case 2: return [2 /*return*/];
154+
}
155+
});
156+
});
157+
}
158+
var interval = setInterval(compressAndSend, ttl);
159+
}
160+
export function syncData(url, data) {
161+
return __awaiter(this, void 0, void 0, function () {
162+
var response;
163+
return __generator(this, function (_a) {
164+
switch (_a.label) {
165+
case 0: return [4 /*yield*/, fetch(url, {
166+
method: 'POST',
167+
//mode: 'cors',
168+
cache: 'no-cache',
169+
headers: {
170+
'Content-Type': 'application/json',
171+
'Content-Encoding': "gzip",
172+
},
173+
redirect: 'follow',
174+
referrerPolicy: 'no-referrer',
175+
body: data
176+
})];
177+
case 1:
178+
response = _a.sent();
179+
return [2 /*return*/, response.json()];
180+
}
181+
});
182+
});
183+
}

dist/browser/utils.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface OSDetails {
2+
name: string;
3+
version: string;
4+
}
5+
/**
6+
* Returns the Operating System information
7+
*
8+
*/
9+
export declare function getUserOS(): OSDetails;
10+
//# sourceMappingURL=utils.d.ts.map

dist/browser/utils.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/browser/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Returns the Operating System information
3+
*
4+
*/
5+
export function getUserOS() {
6+
var userAgent = window.navigator.userAgent;
7+
var platform = window.navigator.platform;
8+
var macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K"];
9+
var windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"];
10+
var iosPlatforms = ["iPhone", "iPad", "iPod"];
11+
var name = "";
12+
if (macosPlatforms.includes(platform)) {
13+
name = "Mac OS";
14+
}
15+
else if (iosPlatforms.includes(platform)) {
16+
name = "iOS";
17+
}
18+
else if (windowsPlatforms.includes(platform)) {
19+
name = "Windows";
20+
}
21+
else if (/Android/.test(userAgent)) {
22+
name = "Android";
23+
}
24+
else if (!name && /Linux/.test(platform)) {
25+
name = "Linux";
26+
}
27+
return {
28+
name: name,
29+
version: ""
30+
};
31+
}

dist/data-processing/compression.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare function gzip(data: any): Uint8Array;
2+
//# sourceMappingURL=compression.d.ts.map

dist/data-processing/compression.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/data-processing/compression.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pako from "pako";
2+
export function gzip(data) {
3+
return pako.gzip(JSON.stringify(data));
4+
}

dist/data-processing/web-worker.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare const workerInstance: {
2+
getInstance: () => any;
3+
};
4+
//# sourceMappingURL=web-worker.d.ts.map

dist/data-processing/web-worker.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/data-processing/web-worker.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var worker = require("workerize-loader!../browser/user-interaction"); // eslint-disable-line import/no-webpack-loader-syntax
2+
export var workerInstance = (function () {
3+
var instance;
4+
function getInstance() {
5+
if (instance) {
6+
return instance;
7+
}
8+
instance = worker();
9+
return instance;
10+
}
11+
return {
12+
getInstance: getInstance
13+
};
14+
})();

dist/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import UserInteractionResource, { UserInteraction } from './resources/userInteractionResource';
2+
import { withTracking, TrackerProps } from './react/components/withTracking';
3+
import { withDataContext } from './react/components/withDataContext';
4+
import { DataContext } from './react/contexts/dataContext';
5+
import { workerInstance } from './data-processing/web-worker';
6+
import { initializeBeacon } from './browser/storage';
7+
export { UserInteraction, withTracking, withDataContext, DataContext, workerInstance, initializeBeacon, };
8+
export type { UserInteractionResource };
9+
export type { TrackerProps };
10+
//# sourceMappingURL=index.d.ts.map

dist/index.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)