2023-04-26 07:18:16 +00:00
|
|
|
import {
|
2023-05-08 09:03:21 +00:00
|
|
|
getAppViewerPageIdFromPath,
|
2023-04-26 07:18:16 +00:00
|
|
|
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-05-08 09:03:21 +00:00
|
|
|
import PageApi from "api/PageApi";
|
|
|
|
|
import { APP_MODE } from "entities/App";
|
2023-06-05 21:27:40 +00:00
|
|
|
import type { FetchApplicationResponse } from "@appsmith/api/ApplicationApi";
|
2023-05-08 09:03:21 +00:00
|
|
|
import type { AxiosResponse } from "axios";
|
|
|
|
|
import { getFirstTimeUserOnboardingIntroModalVisibility } from "utils/storage";
|
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-05-08 09:03:21 +00:00
|
|
|
static async isTrackableUrl(path: string) {
|
|
|
|
|
if (isViewerPath(path)) {
|
|
|
|
|
if (UsagePulse.isAnonymousUser) {
|
|
|
|
|
/*
|
|
|
|
|
In App view mode for non-logged in user, first we must have to check if the app is public or not.
|
|
|
|
|
If it is private app with non-logged in user, we do not send pulse at this point instead we redirect to the login page.
|
|
|
|
|
And for login page no usage pulse is required.
|
|
|
|
|
*/
|
|
|
|
|
const response: AxiosResponse<FetchApplicationResponse, any> =
|
|
|
|
|
await PageApi.fetchAppAndPages({
|
|
|
|
|
pageId: getAppViewerPageIdFromPath(path),
|
|
|
|
|
mode: APP_MODE.PUBLISHED,
|
|
|
|
|
});
|
|
|
|
|
const { data } = response.data || {};
|
|
|
|
|
if (data?.application && !data.application.isPublic) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else if (isEditorPath(path)) {
|
|
|
|
|
/*
|
|
|
|
|
During onboarding we show the Intro Modal and let user use the app for the first time.
|
|
|
|
|
During this exploration period, we do no send usage pulse.
|
|
|
|
|
*/
|
|
|
|
|
const isFirstTimeOnboarding =
|
|
|
|
|
await getFirstTimeUserOnboardingIntroModalVisibility();
|
|
|
|
|
if (!isFirstTimeOnboarding) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
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
|
|
|
|
|
*/
|
2023-05-08 09:03:21 +00:00
|
|
|
static async watchForTrackableUrl(callback: () => void) {
|
|
|
|
|
UsagePulse.unlistenRouteChange = history.listen(async () => {
|
|
|
|
|
if (await 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-05-08 09:03:21 +00:00
|
|
|
static async track() {
|
|
|
|
|
if (await UsagePulse.isTrackableUrl(window.location.pathname)) {
|
2023-01-10 12:09:33 +00:00
|
|
|
UsagePulse.sendPulse();
|
|
|
|
|
UsagePulse.scheduleNextActivityListeners();
|
|
|
|
|
} else {
|
2023-05-08 09:03:21 +00:00
|
|
|
await 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;
|