surveysparrow-ionic-plugin 2.0.3-beta.1 → 2.0.4-beta.1
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/dist/angular-ui/fesm2022/angular-ui.mjs +1203 -998
- package/dist/angular-ui/fesm2022/angular-ui.mjs.map +1 -1
- package/dist/angular-ui/index.d.ts +119 -5
- package/dist/docs.json +13 -5
- package/dist/esm/angular-ui/lib/public-api.js.map +1 -1
- package/dist/esm/angular-ui/lib/spotcheck.component.d.ts +30 -1
- package/dist/esm/angular-ui/lib/spotcheck.component.js +194 -4
- package/dist/esm/angular-ui/lib/spotcheck.component.js.map +1 -1
- package/dist/esm/angular-ui/lib/spotchecks/components/builder.component.d.ts +57 -0
- package/dist/esm/angular-ui/lib/spotchecks/components/builder.component.js +284 -0
- package/dist/esm/angular-ui/lib/spotchecks/components/builder.component.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/binding.d.ts +25 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/binding.js +107 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/binding.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/device.d.ts +11 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/device.js +48 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/device.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/executables.d.ts +4 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/executables.js +113 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/executables.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/listener.d.ts +9 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/listener.js +55 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/listener.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/render.d.ts +5 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/render.js +43 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/render.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/sentry.d.ts +7 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/sentry.js +115 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/sentry.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/storage.d.ts +3 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/storage.js +40 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/storage.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/survey-page.d.ts +22 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/survey-page.js +128 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/survey-page.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/version.d.ts +2 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/version.js +4 -0
- package/dist/esm/angular-ui/lib/spotchecks/helpers/version.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/index.d.ts +4 -3
- package/dist/esm/angular-ui/lib/spotchecks/index.js +2 -2
- package/dist/esm/angular-ui/lib/spotchecks/index.js.map +1 -1
- package/dist/esm/angular-ui/lib/spotchecks/spotcheck.d.ts +8 -0
- package/dist/esm/angular-ui/lib/spotchecks/spotcheck.js +147 -0
- package/dist/esm/angular-ui/lib/spotchecks/spotcheck.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/state/store.d.ts +14 -0
- package/dist/esm/angular-ui/lib/spotchecks/state/store.js +109 -0
- package/dist/esm/angular-ui/lib/spotchecks/state/store.js.map +1 -0
- package/dist/esm/angular-ui/lib/spotchecks/types.d.ts +22 -58
- package/dist/esm/angular-ui/lib/spotchecks/types.js.map +1 -1
- package/dist/plugin.cjs.js +1211 -51762
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +1209 -51761
- package/dist/plugin.js.map +1 -1
- package/package.json +4 -8
- package/src/angular-ui/lib/public-api.ts +1 -1
- package/src/angular-ui/lib/spotcheck.component.ts +202 -5
- package/src/angular-ui/lib/spotchecks/components/builder.component.ts +254 -0
- package/src/angular-ui/lib/spotchecks/helpers/binding.ts +126 -0
- package/src/angular-ui/lib/spotchecks/helpers/device.ts +51 -0
- package/src/angular-ui/lib/spotchecks/helpers/executables.ts +124 -0
- package/src/angular-ui/lib/spotchecks/helpers/listener.ts +61 -0
- package/src/angular-ui/lib/spotchecks/helpers/render.ts +44 -0
- package/src/angular-ui/lib/spotchecks/helpers/sentry.ts +127 -0
- package/src/angular-ui/lib/spotchecks/helpers/storage.ts +37 -0
- package/src/angular-ui/lib/spotchecks/helpers/survey-page.ts +120 -0
- package/src/angular-ui/lib/spotchecks/helpers/version.ts +3 -0
- package/src/angular-ui/lib/spotchecks/index.ts +11 -3
- package/src/angular-ui/lib/spotchecks/spotcheck.ts +150 -0
- package/src/angular-ui/lib/spotchecks/state/store.ts +126 -0
- package/src/angular-ui/lib/spotchecks/types.ts +26 -59
- package/src/angular-ui/ng-package.json +9 -12
- package/src/angular-ui/package.json +9 -8
- package/dist/angular-ui/esm2022/angular-ui.mjs +0 -5
- package/dist/angular-ui/esm2022/public-api.mjs +0 -3
- package/dist/angular-ui/esm2022/spotcheck.component.mjs +0 -17
- package/dist/angular-ui/esm2022/spotchecks/SpotCheck.mjs +0 -51
- package/dist/angular-ui/esm2022/spotchecks/SpotCheckComponent.mjs +0 -410
- package/dist/angular-ui/esm2022/spotchecks/SpotCheckEventListener.mjs +0 -26
- package/dist/angular-ui/esm2022/spotchecks/SpotcheckStateService.mjs +0 -67
- package/dist/angular-ui/esm2022/spotchecks/api.mjs +0 -230
- package/dist/angular-ui/esm2022/spotchecks/helpers.mjs +0 -338
- package/dist/angular-ui/esm2022/spotchecks/index.mjs +0 -3
- package/dist/angular-ui/esm2022/spotchecks/storage.mjs +0 -7
- package/dist/angular-ui/esm2022/spotchecks/types.mjs +0 -2
- package/dist/angular-ui/public-api.d.ts +0 -2
- package/dist/angular-ui/spotcheck.component.d.ts +0 -5
- package/dist/angular-ui/spotchecks/SpotCheck.d.ts +0 -4
- package/dist/angular-ui/spotchecks/SpotCheckComponent.d.ts +0 -65
- package/dist/angular-ui/spotchecks/SpotCheckEventListener.d.ts +0 -16
- package/dist/angular-ui/spotchecks/SpotcheckStateService.d.ts +0 -12
- package/dist/angular-ui/spotchecks/api.d.ts +0 -15
- package/dist/angular-ui/spotchecks/helpers.d.ts +0 -30
- package/dist/angular-ui/spotchecks/index.d.ts +0 -3
- package/dist/angular-ui/spotchecks/storage.d.ts +0 -2
- package/dist/angular-ui/spotchecks/types.d.ts +0 -80
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheck.d.ts +0 -4
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheck.js +0 -51
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheck.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckComponent.d.ts +0 -56
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckComponent.js +0 -397
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckComponent.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckEventListener.d.ts +0 -16
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckEventListener.js +0 -28
- package/dist/esm/angular-ui/lib/spotchecks/SpotCheckEventListener.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/SpotcheckStateService.d.ts +0 -9
- package/dist/esm/angular-ui/lib/spotchecks/SpotcheckStateService.js +0 -72
- package/dist/esm/angular-ui/lib/spotchecks/SpotcheckStateService.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/api.d.ts +0 -15
- package/dist/esm/angular-ui/lib/spotchecks/api.js +0 -232
- package/dist/esm/angular-ui/lib/spotchecks/api.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/helpers.d.ts +0 -30
- package/dist/esm/angular-ui/lib/spotchecks/helpers.js +0 -304
- package/dist/esm/angular-ui/lib/spotchecks/helpers.js.map +0 -1
- package/dist/esm/angular-ui/lib/spotchecks/storage.d.ts +0 -2
- package/dist/esm/angular-ui/lib/spotchecks/storage.js +0 -7
- package/dist/esm/angular-ui/lib/spotchecks/storage.js.map +0 -1
- package/src/angular-ui/lib/spotchecks/SpotCheck.ts +0 -54
- package/src/angular-ui/lib/spotchecks/SpotCheckComponent.css +0 -7
- package/src/angular-ui/lib/spotchecks/SpotCheckComponent.html +0 -27
- package/src/angular-ui/lib/spotchecks/SpotCheckComponent.ts +0 -389
- package/src/angular-ui/lib/spotchecks/SpotCheckEventListener.ts +0 -50
- package/src/angular-ui/lib/spotchecks/SpotcheckStateService.ts +0 -66
- package/src/angular-ui/lib/spotchecks/api.ts +0 -286
- package/src/angular-ui/lib/spotchecks/helpers.ts +0 -414
- package/src/angular-ui/lib/spotchecks/storage.ts +0 -7
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core';
|
|
2
|
+
import { Device } from '@capacitor/device';
|
|
3
|
+
|
|
4
|
+
let deviceInfo: any = null;
|
|
5
|
+
|
|
6
|
+
const getDeviceInfo = async () => {
|
|
7
|
+
if (deviceInfo) return deviceInfo;
|
|
8
|
+
try {
|
|
9
|
+
deviceInfo = await Device.getInfo();
|
|
10
|
+
} catch {
|
|
11
|
+
deviceInfo = { platform: Capacitor.getPlatform(), operatingSystem: 'unknown', osVersion: '' };
|
|
12
|
+
}
|
|
13
|
+
return deviceInfo;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Legacy Ionic visitor: operatingSystem from @capacitor/device (cached at init), size from the window.
|
|
17
|
+
export const getVisitorInfo = () => {
|
|
18
|
+
const info = deviceInfo || {};
|
|
19
|
+
return {
|
|
20
|
+
deviceType: 'MOBILE',
|
|
21
|
+
operatingSystem: info.operatingSystem || 'unknown',
|
|
22
|
+
screenResolution: {
|
|
23
|
+
width: Math.round(window.innerWidth),
|
|
24
|
+
height: Math.round(window.innerHeight),
|
|
25
|
+
},
|
|
26
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const loadDeviceInfo = () => getDeviceInfo();
|
|
31
|
+
|
|
32
|
+
// Raw device facts; the backend keeps the SDK version per app build.
|
|
33
|
+
export const getDeviceFacts = async () => {
|
|
34
|
+
const info = await getDeviceInfo();
|
|
35
|
+
const facts: Record<string, unknown> = {
|
|
36
|
+
os: info.platform === 'web' ? 'web' : info.platform,
|
|
37
|
+
osVersion: String(info.osVersion || ''),
|
|
38
|
+
isTablet: false,
|
|
39
|
+
...(info.model ? { model: String(info.model) } : {}),
|
|
40
|
+
};
|
|
41
|
+
if (Capacitor.isNativePlatform()) {
|
|
42
|
+
try {
|
|
43
|
+
// @capacitor/app is optional; its native plugin registers here when installed.
|
|
44
|
+
const app: any = (Capacitor as any).Plugins?.App;
|
|
45
|
+
const appInfo = app?.getInfo ? await app.getInfo() : null;
|
|
46
|
+
if (appInfo?.version) facts['appVersion'] = String(appInfo.version);
|
|
47
|
+
if (appInfo?.build) facts['appBuild'] = String(appInfo.build);
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
return facts;
|
|
51
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { functionStore, spotcheckStore } from '../state/store';
|
|
2
|
+
import { saveData, loadData } from './storage';
|
|
3
|
+
import { sdkListener } from './listener';
|
|
4
|
+
import { captureP0Error, captureP1Error, addBreadcrumb, safeAddBreadcrumb, isReported, registerExecute } from './sentry';
|
|
5
|
+
import { SDK_VERSION } from './version';
|
|
6
|
+
|
|
7
|
+
export const dispatchWrapper = (stateUpdate: any) => {
|
|
8
|
+
try {
|
|
9
|
+
spotcheckStore.dispatch(stateUpdate);
|
|
10
|
+
} catch (error: any) {
|
|
11
|
+
captureP1Error(error, 'GENERAL', { action: 'dispatchWrapper', errorMessage: error?.message });
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type PendingCall = { functionName: string; params?: any };
|
|
16
|
+
|
|
17
|
+
const pendingExecutes: PendingCall[] = [];
|
|
18
|
+
const MAX_PENDING_EXECUTES = 20;
|
|
19
|
+
|
|
20
|
+
const isTrackScreenOrEvent = (functionName: string) =>
|
|
21
|
+
functionName === 'trackScreen' || functionName === 'trackEvent';
|
|
22
|
+
|
|
23
|
+
const flushPendingExecutes = async () => {
|
|
24
|
+
while (pendingExecutes.length > 0) {
|
|
25
|
+
const item = pendingExecutes.shift();
|
|
26
|
+
if (item) await execute(item.functionName, item.params);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Track calls wait until initializeSpotcheckComponent has finished.
|
|
31
|
+
let isInitialized = false;
|
|
32
|
+
|
|
33
|
+
export const markInitialized = async () => {
|
|
34
|
+
isInitialized = true;
|
|
35
|
+
await flushPendingExecutes();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// Reporting failures are not reported again (avoids loops).
|
|
39
|
+
const isSentryFunction = (functionName: string) => functionName.startsWith('sentry.');
|
|
40
|
+
|
|
41
|
+
// Style getters run on every render; they would push useful breadcrumbs out.
|
|
42
|
+
const isBreadcrumbWorthy = (functionName: string) =>
|
|
43
|
+
!isSentryFunction(functionName) && !/\.get\w*Styles$/.test(functionName);
|
|
44
|
+
|
|
45
|
+
// Message type only; never survey content.
|
|
46
|
+
const describeParams = (functionName: string, params?: any) => {
|
|
47
|
+
if (functionName !== 'webviewComponent.handleWebViewMessage') return {};
|
|
48
|
+
try {
|
|
49
|
+
return { messageType: JSON.parse(params?.event?.nativeEvent?.data)?.type };
|
|
50
|
+
} catch {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const lookup = (functionName: string): any => {
|
|
56
|
+
let current: any = functionStore.getState().functionState;
|
|
57
|
+
for (const part of functionName.split('.')) current = current?.[part];
|
|
58
|
+
return current;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// True when the backend sent this function (dotted path), e.g. for schema `$execute`.
|
|
62
|
+
export const hasFunction = (functionName: string): boolean => typeof lookup(functionName) === 'string';
|
|
63
|
+
|
|
64
|
+
// The SDK has no native keyboard mode to change; the overlay follows the keyboard instead.
|
|
65
|
+
const keyboard = {
|
|
66
|
+
pauseDefaultKeyboardBehavior: () => {},
|
|
67
|
+
resumeDefaultKeyboardBehavior: () => {},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const execute = async (functionName: string, params?: any): Promise<any> => {
|
|
71
|
+
let func: any;
|
|
72
|
+
let payload: any;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
if (isTrackScreenOrEvent(functionName) && !isInitialized) {
|
|
76
|
+
pendingExecutes.push({ functionName, params });
|
|
77
|
+
// Cap the queue while init keeps failing; oldest calls go first.
|
|
78
|
+
if (pendingExecutes.length > MAX_PENDING_EXECUTES) pendingExecutes.shift();
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!functionStore.getState().functionState.isLoaded) return null;
|
|
83
|
+
|
|
84
|
+
const functionString = lookup(functionName);
|
|
85
|
+
if (!functionString || typeof functionString !== 'string') {
|
|
86
|
+
console.error(`Function ${functionName} not found`);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
payload = {
|
|
91
|
+
params: { ...params, sdkVersion: SDK_VERSION },
|
|
92
|
+
state: spotcheckStore.getState(),
|
|
93
|
+
dispatchWrapper,
|
|
94
|
+
storage: { saveData, loadData },
|
|
95
|
+
listener: sdkListener,
|
|
96
|
+
sentry: { captureP0Error, captureP1Error, addBreadcrumb: safeAddBreadcrumb },
|
|
97
|
+
keyboard,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Fresh evaluation per call like the other SDKs; indirect eval keeps it in global scope.
|
|
101
|
+
func = (0, eval)(`(${functionString})`);
|
|
102
|
+
if (isBreadcrumbWorthy(functionName)) {
|
|
103
|
+
addBreadcrumb('execute', { functionName, ...describeParams(functionName, params) });
|
|
104
|
+
}
|
|
105
|
+
} catch (error: any) {
|
|
106
|
+
if (isSentryFunction(functionName)) return null;
|
|
107
|
+
captureP1Error(error, 'GENERAL', { action: 'execute:setup', functionName, errorMessage: error?.message });
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
return await func(payload);
|
|
113
|
+
} catch (error: any) {
|
|
114
|
+
if (isSentryFunction(functionName)) return null;
|
|
115
|
+
// Skip when the function already reported this error itself.
|
|
116
|
+
if (!isReported(error)) {
|
|
117
|
+
captureP1Error(error, 'GENERAL', { action: 'execute:runtime', functionName, errorMessage: error?.message });
|
|
118
|
+
}
|
|
119
|
+
console.error(`[execute] ${functionName}:`, error);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
registerExecute(execute);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Subscription } from 'rxjs';
|
|
2
|
+
import type { SsSpotcheckListener } from '../types';
|
|
3
|
+
|
|
4
|
+
// Legacy Ionic events, still supported next to the init `listener`.
|
|
5
|
+
export type SpotCheckEventType = 'surveyCompleted' | 'surveyDismissed' | 'surveyLoadStarted';
|
|
6
|
+
|
|
7
|
+
type EventCallback = (data?: any) => void;
|
|
8
|
+
|
|
9
|
+
const legacyCallbacks = new Map<SpotCheckEventType, Set<EventCallback>>();
|
|
10
|
+
|
|
11
|
+
const emit = (type: SpotCheckEventType, data?: any) => {
|
|
12
|
+
legacyCallbacks.get(type)?.forEach((callback) => {
|
|
13
|
+
try {
|
|
14
|
+
runInApp(() => callback(data));
|
|
15
|
+
} catch (error) {
|
|
16
|
+
console.error(`[SpotCheck] ${type} listener failed`, error);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Returns an RxJS Subscription, as the legacy SDK did.
|
|
22
|
+
export const addSpotCheckListener = (eventType: SpotCheckEventType, callback: EventCallback): Subscription => {
|
|
23
|
+
if (!legacyCallbacks.has(eventType)) legacyCallbacks.set(eventType, new Set());
|
|
24
|
+
legacyCallbacks.get(eventType)!.add(callback);
|
|
25
|
+
return new Subscription(() => {
|
|
26
|
+
legacyCallbacks.get(eventType)?.delete(callback);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
let appListener: SsSpotcheckListener = {};
|
|
31
|
+
|
|
32
|
+
// Set by <SpotCheck>: runs app callbacks inside the Angular zone.
|
|
33
|
+
let runInApp: <T>(fn: () => T) => T = (fn) => fn();
|
|
34
|
+
export const setAppRunner = (runner: <T>(fn: () => T) => T) => {
|
|
35
|
+
runInApp = runner;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const setListener = (listener?: SsSpotcheckListener) => {
|
|
39
|
+
appListener = listener || {};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// What backend functions call: the app's listener first, then the legacy events.
|
|
43
|
+
export const sdkListener: Required<SsSpotcheckListener> = {
|
|
44
|
+
onSurveyLoaded: async (data) => {
|
|
45
|
+
await runInApp(() => appListener.onSurveyLoaded?.(data));
|
|
46
|
+
// 2.0.3 passed surveyDetails.
|
|
47
|
+
emit('surveyLoadStarted', data?.['surveyDetails'] ?? data);
|
|
48
|
+
},
|
|
49
|
+
onSurveyResponse: async (data) => {
|
|
50
|
+
await runInApp(() => appListener.onSurveyResponse?.(data));
|
|
51
|
+
// Legacy Ionic passed the response array.
|
|
52
|
+
emit('surveyCompleted', data?.['response'] ?? data);
|
|
53
|
+
},
|
|
54
|
+
onPartialSubmission: async (data) => {
|
|
55
|
+
await runInApp(() => appListener.onPartialSubmission?.(data));
|
|
56
|
+
},
|
|
57
|
+
onCloseButtonTap: async () => {
|
|
58
|
+
await runInApp(() => appListener.onCloseButtonTap?.());
|
|
59
|
+
emit('surveyDismissed');
|
|
60
|
+
},
|
|
61
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ChangeDetectorRef } from '@angular/core';
|
|
2
|
+
import { captureP0Error } from './sentry';
|
|
3
|
+
|
|
4
|
+
// Render guard, like RN's error boundary: a failed draw hides SpotCheck, the app keeps running,
|
|
5
|
+
// and it is reported once as APP_CRASH.
|
|
6
|
+
let crashed = false;
|
|
7
|
+
const crashListeners = new Set<() => void>();
|
|
8
|
+
|
|
9
|
+
export const hasCrashed = () => crashed;
|
|
10
|
+
|
|
11
|
+
export const onCrash = (listener: () => void) => {
|
|
12
|
+
crashListeners.add(listener);
|
|
13
|
+
return () => {
|
|
14
|
+
crashListeners.delete(listener);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const guardedDetectChanges = (cdr: ChangeDetectorRef, action: string) => {
|
|
19
|
+
if (crashed) return;
|
|
20
|
+
try {
|
|
21
|
+
cdr.detectChanges();
|
|
22
|
+
} catch (error: any) {
|
|
23
|
+
crashed = true;
|
|
24
|
+
captureP0Error(error, 'APP_CRASH', { action, errorMessage: error?.message });
|
|
25
|
+
crashListeners.forEach((listener) => {
|
|
26
|
+
try {
|
|
27
|
+
listener();
|
|
28
|
+
} catch {}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Runs fn once per microtask however often it is requested (many dispatches, one render).
|
|
34
|
+
export const batched = (fn: () => void) => {
|
|
35
|
+
let queued = false;
|
|
36
|
+
return () => {
|
|
37
|
+
if (queued) return;
|
|
38
|
+
queued = true;
|
|
39
|
+
queueMicrotask(() => {
|
|
40
|
+
queued = false;
|
|
41
|
+
fn();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// SDK errors go to SurveySparrow's API (backend sentry.processSentryError), never to Sentry servers.
|
|
2
|
+
import { spotcheckStore } from '../state/store';
|
|
3
|
+
import { SDK_VERSION, FRAMEWORK } from './version';
|
|
4
|
+
|
|
5
|
+
export type ErrorSource =
|
|
6
|
+
| 'SDK_INITIALIZATION'
|
|
7
|
+
| 'SPOTCHECK_INITIALIZATION'
|
|
8
|
+
| 'TRACK_SCREEN'
|
|
9
|
+
| 'TRACK_EVENT'
|
|
10
|
+
| 'WEBVIEW_ERROR'
|
|
11
|
+
| 'CLOSE_SPOTCHECK'
|
|
12
|
+
| 'APP_CRASH'
|
|
13
|
+
| 'GENERAL';
|
|
14
|
+
|
|
15
|
+
// Set by executables.ts at load time (avoids a circular import).
|
|
16
|
+
let executeRef: ((fn: string, params?: any) => Promise<any>) | null = null;
|
|
17
|
+
export const registerExecute = (fn: (fnName: string, params?: any) => Promise<any>) => {
|
|
18
|
+
executeRef = fn;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const MAX_BREADCRUMBS = 30;
|
|
22
|
+
const sdkBreadcrumbs: any[] = [];
|
|
23
|
+
|
|
24
|
+
export const addBreadcrumb = (message: string, data?: Record<string, any>) => {
|
|
25
|
+
sdkBreadcrumbs.push({ category: 'spotcheck', message, data, timestamp: Date.now() / 1000 });
|
|
26
|
+
if (sdkBreadcrumbs.length > MAX_BREADCRUMBS) sdkBreadcrumbs.shift();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const MAX_BREADCRUMB_STRING = 200;
|
|
30
|
+
const capString = (value: string) => value.slice(0, MAX_BREADCRUMB_STRING);
|
|
31
|
+
|
|
32
|
+
// Breadcrumb hook for backend functions; drops bad input and never throws.
|
|
33
|
+
export const safeAddBreadcrumb = (message: unknown, data?: unknown) => {
|
|
34
|
+
try {
|
|
35
|
+
if (typeof message !== 'string') return;
|
|
36
|
+
let clean: Record<string, any> | undefined;
|
|
37
|
+
const proto = data && typeof data === 'object' ? Object.getPrototypeOf(data) : undefined;
|
|
38
|
+
if (proto === Object.prototype || proto === null) {
|
|
39
|
+
clean = {};
|
|
40
|
+
Object.keys(data as object).forEach((key) => {
|
|
41
|
+
const value = (data as any)[key];
|
|
42
|
+
if (typeof value === 'string') clean![key] = capString(value);
|
|
43
|
+
else if (value === null || typeof value === 'number' || typeof value === 'boolean') clean![key] = value;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
addBreadcrumb(capString(message), clean);
|
|
47
|
+
} catch {}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Flags an error as reported so execute() does not report it again.
|
|
51
|
+
const markReported = (error: unknown) => {
|
|
52
|
+
try {
|
|
53
|
+
if (error !== null && typeof error === 'object') {
|
|
54
|
+
Object.defineProperty(error, '__ssReported', { value: true, configurable: true });
|
|
55
|
+
}
|
|
56
|
+
} catch {}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const isReported = (error: unknown) => {
|
|
60
|
+
try {
|
|
61
|
+
return !!(error as any)?.__ssReported;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Routes an event to the backend; never throws.
|
|
68
|
+
const sendToBackend = async (normalizedEvent: any) => {
|
|
69
|
+
try {
|
|
70
|
+
const result = executeRef
|
|
71
|
+
? await executeRef('sentry.processSentryError', {
|
|
72
|
+
event: normalizedEvent,
|
|
73
|
+
sdkType: FRAMEWORK,
|
|
74
|
+
sdkVersion: SDK_VERSION,
|
|
75
|
+
})
|
|
76
|
+
: null;
|
|
77
|
+
if (result) return;
|
|
78
|
+
|
|
79
|
+
const { domainName, targetToken } = spotcheckStore.getState().SpotCheckState?.params || {};
|
|
80
|
+
if (!domainName) return;
|
|
81
|
+
await fetch(`https://${domainName}/api/internal/spotcheck/sdkErrors`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: { 'Content-Type': 'application/json' },
|
|
84
|
+
body: JSON.stringify({
|
|
85
|
+
errorMessage: normalizedEvent.errorMessage,
|
|
86
|
+
sdkType: FRAMEWORK,
|
|
87
|
+
sdkVersion: SDK_VERSION,
|
|
88
|
+
level: normalizedEvent.tags?.error_priority === 'P0' ? 'fatal' : 'error',
|
|
89
|
+
tags: normalizedEvent.tags,
|
|
90
|
+
extra: normalizedEvent.extra,
|
|
91
|
+
breadcrumbs: normalizedEvent.breadcrumbs,
|
|
92
|
+
contexts: { user: { spotcheck_token: targetToken || '', spotcheck_domain_name: domainName } },
|
|
93
|
+
}),
|
|
94
|
+
}).catch(() => {});
|
|
95
|
+
} catch {
|
|
96
|
+
// Reporting must never break the host app.
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const report = (error: unknown, priority: 'P0' | 'P1', source: ErrorSource, extra?: Record<string, any>) => {
|
|
101
|
+
markReported(error);
|
|
102
|
+
const err = error as any;
|
|
103
|
+
sendToBackend({
|
|
104
|
+
errorMessage: err?.message || String(error),
|
|
105
|
+
tags: {
|
|
106
|
+
error_priority: priority,
|
|
107
|
+
severity: priority === 'P0' ? 'CRITICAL' : 'HIGH',
|
|
108
|
+
errorType: source,
|
|
109
|
+
},
|
|
110
|
+
contexts: {},
|
|
111
|
+
extra: extra || {},
|
|
112
|
+
breadcrumbs: [...sdkBreadcrumbs],
|
|
113
|
+
exceptions: [{
|
|
114
|
+
type: err?.name || 'Error',
|
|
115
|
+
value: err?.message || String(error),
|
|
116
|
+
stack: String(err?.stack || '').slice(0, 2000),
|
|
117
|
+
}],
|
|
118
|
+
timestamp: Date.now() / 1000,
|
|
119
|
+
platform: 'javascript',
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const captureP0Error = (error: unknown, source: ErrorSource, extra?: Record<string, any>) =>
|
|
124
|
+
report(error, 'P0', source, extra);
|
|
125
|
+
|
|
126
|
+
export const captureP1Error = (error: unknown, source: ErrorSource, extra?: Record<string, any>) =>
|
|
127
|
+
report(error, 'P1', source, extra);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { captureP1Error } from './sentry';
|
|
2
|
+
|
|
3
|
+
// Same key as the legacy Ionic SDK, so existing installs keep their visitor.
|
|
4
|
+
const UUID_KEY = 'SurveySparrowUUID';
|
|
5
|
+
|
|
6
|
+
const uuidV4 = (): string => {
|
|
7
|
+
const c: any = typeof crypto !== 'undefined' ? crypto : null;
|
|
8
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
9
|
+
const bytes = new Uint8Array(16);
|
|
10
|
+
if (c?.getRandomValues) c.getRandomValues(bytes);
|
|
11
|
+
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
|
|
12
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
13
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
14
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
15
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const generateTraceId = () => `${uuidV4()}-${Date.now()}`;
|
|
19
|
+
|
|
20
|
+
export const saveData = async (value: string) => {
|
|
21
|
+
try {
|
|
22
|
+
localStorage.setItem(UUID_KEY, value);
|
|
23
|
+
} catch (error: any) {
|
|
24
|
+
captureP1Error(error, 'GENERAL', { action: 'saveData', errorMessage: error?.message });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// isuuid: a new trace id; otherwise the stored visitor UUID.
|
|
29
|
+
export const loadData = async (isuuid: boolean = false) => {
|
|
30
|
+
try {
|
|
31
|
+
if (isuuid) return generateTraceId();
|
|
32
|
+
return localStorage.getItem(UUID_KEY) || null;
|
|
33
|
+
} catch (error: any) {
|
|
34
|
+
captureP1Error(error, 'GENERAL', { action: 'loadData', isuuid, errorMessage: error?.message });
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Loads the survey page from its own domain in a separate-origin iframe (as in 2.0.3), so the
|
|
2
|
+
// page can never reach the host app. The SDK and the page talk only through postMessage.
|
|
3
|
+
|
|
4
|
+
export interface SurveyPageCallbacks {
|
|
5
|
+
onMessage: (data: string) => void;
|
|
6
|
+
onError: (error: { nativeEvent: { url: string; description: string }; message: string }) => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Runs a backend injection with a stand-in window and keeps only the messages it dispatches.
|
|
10
|
+
// Scripts that touch the page DOM (CSS, re-measure) cannot cross origins and are dropped, as in 2.0.3.
|
|
11
|
+
const messagesFromScript = (script: string): any[] => {
|
|
12
|
+
const messages: any[] = [];
|
|
13
|
+
const pageWindow = {
|
|
14
|
+
dispatchEvent: (event: any) => {
|
|
15
|
+
if (event?.data != null) messages.push(event.data);
|
|
16
|
+
return true;
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
class PageMessageEvent {
|
|
20
|
+
data: any;
|
|
21
|
+
constructor(public type: string, init?: { data?: any }) {
|
|
22
|
+
this.data = init?.data;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
// The host globals are shadowed so a DOM script fails here instead of touching the app.
|
|
27
|
+
new Function('window', 'MessageEvent', 'document', 'self', 'globalThis', 'parent', 'top', script)(
|
|
28
|
+
pageWindow, PageMessageEvent, undefined, pageWindow, pageWindow, pageWindow, pageWindow,
|
|
29
|
+
);
|
|
30
|
+
} catch {
|
|
31
|
+
// DOM-only script: nothing to send.
|
|
32
|
+
}
|
|
33
|
+
return messages;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Same list as the survey page (rtls).
|
|
37
|
+
const RTL_LANGUAGES = ['ar', 'he', 'fa', 'ps', 'ur'];
|
|
38
|
+
|
|
39
|
+
// The web page sends its fields at the top level; the backend reads the RN/Expo shape { type, data }.
|
|
40
|
+
const toBackendMessage = (message: any): any | null => {
|
|
41
|
+
if (!message || typeof message !== 'object' || typeof message.type !== 'string') return null;
|
|
42
|
+
if (message.type === 'resizeWindow') {
|
|
43
|
+
const height = message.size?.height;
|
|
44
|
+
return typeof height === 'number' ? { type: 'spotCheckData', data: { currentQuestionSize: { height } } } : null;
|
|
45
|
+
}
|
|
46
|
+
if ('data' in message) return message;
|
|
47
|
+
const { type, ...data } = message;
|
|
48
|
+
// The web languageChanged has only the code; add isRtl from the page's own RTL list.
|
|
49
|
+
if (type === 'languageChanged' && typeof data.code === 'string') {
|
|
50
|
+
data.isRtl = RTL_LANGUAGES.includes(data.code.split(/[-_]/)[0].toLowerCase());
|
|
51
|
+
}
|
|
52
|
+
return { type, data };
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export class SurveyPage {
|
|
56
|
+
readonly iframe: HTMLIFrameElement;
|
|
57
|
+
private uri = '';
|
|
58
|
+
private origin = '';
|
|
59
|
+
private loadSeq = 0;
|
|
60
|
+
private readonly onWindowMessage = (event: MessageEvent) => {
|
|
61
|
+
// Only the survey page in this iframe, from the survey domain.
|
|
62
|
+
if (event.source !== this.iframe.contentWindow || event.origin !== this.origin) return;
|
|
63
|
+
const message = toBackendMessage(event.data);
|
|
64
|
+
if (message) this.callbacks.onMessage(JSON.stringify(message));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
constructor(host: HTMLElement, private callbacks: SurveyPageCallbacks) {
|
|
68
|
+
this.iframe = document.createElement('iframe');
|
|
69
|
+
this.iframe.setAttribute('title', 'Survey');
|
|
70
|
+
this.iframe.setAttribute(
|
|
71
|
+
'allow',
|
|
72
|
+
'camera; microphone; geolocation; display-capture; autoplay; clipboard-read; clipboard-write; fullscreen',
|
|
73
|
+
);
|
|
74
|
+
this.iframe.setAttribute('allowfullscreen', '');
|
|
75
|
+
this.iframe.style.cssText = 'display:block;width:100%;height:100%;border:0;background:transparent;';
|
|
76
|
+
host.appendChild(this.iframe);
|
|
77
|
+
window.addEventListener('message', this.onWindowMessage);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
load(uri: string): void {
|
|
81
|
+
if (!uri || uri === this.uri) return;
|
|
82
|
+
this.uri = uri;
|
|
83
|
+
this.origin = new URL(uri).origin;
|
|
84
|
+
const seq = ++this.loadSeq;
|
|
85
|
+
// A cross-origin iframe hides HTTP errors, so check the page first to report a failed load.
|
|
86
|
+
fetch(uri, { credentials: 'omit' })
|
|
87
|
+
.then((response) => {
|
|
88
|
+
if (!response.ok) throw new Error(`Survey page failed with status ${response.status}`);
|
|
89
|
+
if (seq === this.loadSeq) this.iframe.src = uri;
|
|
90
|
+
})
|
|
91
|
+
.catch((error: any) => {
|
|
92
|
+
if (seq !== this.loadSeq) return;
|
|
93
|
+
// A later load() with the same uri retries.
|
|
94
|
+
this.uri = '';
|
|
95
|
+
const description = error?.message || String(error);
|
|
96
|
+
// message: readable text for the Sentry report (the RN event shape has none).
|
|
97
|
+
this.callbacks.onError({ nativeEvent: { url: uri, description }, message: description });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Same contract as the RN WebView ref the backend functions call; messages go by postMessage.
|
|
102
|
+
injectJavaScript = (script: string): boolean => {
|
|
103
|
+
const win = this.iframe.contentWindow;
|
|
104
|
+
if (!win || !this.origin) return false;
|
|
105
|
+
const messages = messagesFromScript(script);
|
|
106
|
+
messages.forEach((data) => {
|
|
107
|
+
win.postMessage(data, this.origin);
|
|
108
|
+
// 2.0.3 sent the theme right after the survey state; the web page needs it to apply survey colours.
|
|
109
|
+
const themeInfo = data?.type === 'RESET_STATE' ? data.state?.config?.generatedCSS : null;
|
|
110
|
+
if (themeInfo) win.postMessage({ type: 'THEME_UPDATE_SPOTCHECK', themeInfo }, this.origin);
|
|
111
|
+
});
|
|
112
|
+
return messages.length > 0;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
destroy(): void {
|
|
116
|
+
this.loadSeq++;
|
|
117
|
+
window.removeEventListener('message', this.onWindowMessage);
|
|
118
|
+
this.iframe.remove();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
export { initializeSpotChecks, trackScreen, trackEvent } from './SpotCheck';
|
|
2
1
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
initializeSpotChecks,
|
|
3
|
+
trackScreen,
|
|
4
|
+
trackEvent,
|
|
5
|
+
closeSpotChecks,
|
|
6
|
+
SDK_VERSION,
|
|
7
|
+
} from './spotcheck';
|
|
8
|
+
export { addSpotCheckListener } from './helpers/listener';
|
|
9
|
+
export type { SpotCheckEventType } from './helpers/listener';
|
|
5
10
|
export type {
|
|
6
11
|
UserDetails,
|
|
7
12
|
Variables,
|
|
8
13
|
CustomProperties,
|
|
14
|
+
TrackScreenOptions,
|
|
9
15
|
TrackScreenProps,
|
|
10
16
|
TrackEventProps,
|
|
11
17
|
InitializeSpotChecksProps,
|
|
18
|
+
SpotcheckProps,
|
|
19
|
+
SsSpotcheckListener,
|
|
12
20
|
} from './types';
|