2023-04-26 07:18:16 +00:00
|
|
|
import {
|
|
|
|
|
isEditorPath,
|
|
|
|
|
isViewerPath,
|
|
|
|
|
} from "@appsmith/pages/Editor/Explorer/helpers";
|
2023-01-10 12:09:33 +00:00
|
|
|
import history from "utils/history";
|
2023-04-06 12:21:58 +00:00
|
|
|
import { fetchWithRetry, getUsagePulsePayload } from "./utils";
|
|
|
|
|
import {
|
|
|
|
|
PULSE_API_ENDPOINT,
|
|
|
|
|
PULSE_API_MAX_RETRY_COUNT,
|
|
|
|
|
PULSE_API_RETRY_TIMEOUT,
|
|
|
|
|
PULSE_INTERVAL,
|
|
|
|
|
USER_ACTIVITY_LISTENER_EVENTS,
|
|
|
|
|
} from "@appsmith/constants/UsagePulse";
|
2023-01-10 12:09:33 +00:00
|
|
|
|
|
|
|
|
class UsagePulse {
|
|
|
|
|
static userAnonymousId: string | undefined;
|
2023-01-13 11:05:59 +00:00
|
|
|
static Timer: ReturnType<typeof setTimeout>;
|
2023-01-10 12:09:33 +00:00
|
|
|
static unlistenRouteChange: () => void;
|
2023-04-06 12:21:58 +00:00
|
|
|
static isTelemetryEnabled: boolean;
|
|
|
|
|
static isAnonymousUser: boolean;
|
2023-01-10 12:09:33 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function to check if the given URL is trakable or not.
|
|
|
|
|
* app builder and viewer urls are trackable
|
|
|
|
|
*/
|
2023-04-06 12:21:58 +00:00
|
|
|
static isTrackableUrl(path: string) {
|
|
|
|
|
return isEditorPath(path) || isViewerPath(path);
|
2023-01-10 12:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sendPulse() {
|
2023-04-06 12:21:58 +00:00
|
|
|
const payload = getUsagePulsePayload(
|
|
|
|
|
UsagePulse.isTelemetryEnabled,
|
|
|
|
|
UsagePulse.isAnonymousUser,
|
|
|
|
|
);
|
2023-03-09 10:03:13 +00:00
|
|
|
|
2023-04-06 12:21:58 +00:00
|
|
|
const fetchWithRetryConfig = {
|
|
|
|
|
url: PULSE_API_ENDPOINT,
|
|
|
|
|
payload,
|
|
|
|
|
retries: PULSE_API_MAX_RETRY_COUNT,
|
|
|
|
|
retryTimeout: PULSE_API_RETRY_TIMEOUT,
|
2023-01-10 12:09:33 +00:00
|
|
|
};
|
|
|
|
|
|
2023-04-06 12:21:58 +00:00
|
|
|
fetchWithRetry(fetchWithRetryConfig);
|
2023-01-10 12:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static registerActivityListener() {
|
|
|
|
|
USER_ACTIVITY_LISTENER_EVENTS.forEach((event) => {
|
2023-04-06 12:21:58 +00:00
|
|
|
window.document.body.addEventListener(event, UsagePulse.track);
|
2023-01-10 12:09:33 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static deregisterActivityListener() {
|
|
|
|
|
USER_ACTIVITY_LISTENER_EVENTS.forEach((event) => {
|
2023-04-06 12:21:58 +00:00
|
|
|
window.document.body.removeEventListener(event, UsagePulse.track);
|
2023-01-10 12:09:33 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function to register a history change event and trigger
|
|
|
|
|
* a callback and unlisten when the user goes to a trackable URL
|
|
|
|
|
*/
|
|
|
|
|
static watchForTrackableUrl(callback: () => void) {
|
|
|
|
|
UsagePulse.unlistenRouteChange = history.listen(() => {
|
2023-04-06 12:21:58 +00:00
|
|
|
if (UsagePulse.isTrackableUrl(window.location.pathname)) {
|
2023-01-10 12:09:33 +00:00
|
|
|
UsagePulse.unlistenRouteChange();
|
|
|
|
|
setTimeout(callback, 0);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
UsagePulse.deregisterActivityListener();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function that suspends active tracking listeners
|
|
|
|
|
* and schedules when next listeners should be registered.
|
|
|
|
|
*/
|
|
|
|
|
static scheduleNextActivityListeners() {
|
|
|
|
|
UsagePulse.deregisterActivityListener();
|
|
|
|
|
|
|
|
|
|
UsagePulse.Timer = setTimeout(
|
|
|
|
|
UsagePulse.registerActivityListener,
|
2023-04-06 12:21:58 +00:00
|
|
|
PULSE_INTERVAL,
|
2023-01-10 12:09:33 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Point of entry for the user tracking
|
2023-04-06 12:21:58 +00:00
|
|
|
*/
|
|
|
|
|
static startTrackingActivity(
|
|
|
|
|
isTelemetryEnabled: boolean,
|
|
|
|
|
isAnonymousUser: boolean,
|
|
|
|
|
) {
|
|
|
|
|
UsagePulse.isTelemetryEnabled = isTelemetryEnabled;
|
|
|
|
|
UsagePulse.isAnonymousUser = isAnonymousUser;
|
|
|
|
|
UsagePulse.track();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2023-01-10 12:09:33 +00:00
|
|
|
* triggers a pulse and schedules the pulse , if user is on a trackable url, otherwise
|
|
|
|
|
* registers listeners to wait for the user to go to a trackable url
|
|
|
|
|
*/
|
2023-04-06 12:21:58 +00:00
|
|
|
static track() {
|
|
|
|
|
if (UsagePulse.isTrackableUrl(window.location.pathname)) {
|
2023-01-10 12:09:33 +00:00
|
|
|
UsagePulse.sendPulse();
|
|
|
|
|
UsagePulse.scheduleNextActivityListeners();
|
|
|
|
|
} else {
|
2023-04-06 12:21:58 +00:00
|
|
|
UsagePulse.watchForTrackableUrl(UsagePulse.track);
|
2023-01-10 12:09:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Function to cleanup states and listeners
|
|
|
|
|
*/
|
|
|
|
|
static stopTrackingActivity() {
|
|
|
|
|
UsagePulse.userAnonymousId = undefined;
|
|
|
|
|
clearTimeout(UsagePulse.Timer);
|
|
|
|
|
UsagePulse.unlistenRouteChange && UsagePulse.unlistenRouteChange();
|
|
|
|
|
UsagePulse.deregisterActivityListener();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default UsagePulse;
|