PromucFlow_constructor/app/client/src/sagas/AnalyticsSaga.ts
Valera Melnikov c42e0317de
fix: change appsmith alias (#35349)
In order to unify package names, we decided to use `@appsmith` prefix as
a marker to indicate that packages belong to our codebase and that these
packages are developed internally. So that we can use this prefix, we
need to rename the alias of the same name. But since `@appsmith` is
currently being used as an alias for `ee` folder, we have to rename the
alias as the first step.

Related discussion
https://theappsmith.slack.com/archives/CPG2ZTXEY/p1722516279126329

EE PR — https://github.com/appsmithorg/appsmith-ee/pull/4801

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10267368821>
> Commit: 2b00af2d257e4d4304db0a80072afef7513de6be
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10267368821&attempt=2"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Tue, 06 Aug 2024 14:24:22 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No
2024-08-06 17:52:22 +03:00

159 lines
4.5 KiB
TypeScript

import {
ReduxActionTypes,
type ReduxActionType,
} from "ee/constants/ReduxActionConstants";
import type { Action } from "entities/Action";
import AnalyticsUtil from "ee/utils/AnalyticsUtil";
import {
RequestPayloadAnalyticsPath,
cleanValuesInObjectForHashing,
generateHashFromString,
} from "./helper";
import get from "lodash/get";
import log from "loglevel";
import { all, put, select, takeEvery } from "redux-saga/effects";
import { getIdeCanvasSideBySideHoverState } from "selectors/ideSelectors";
import { EditorViewMode } from "ee/entities/IDE/constants";
import {
recordAnalyticsForSideBySideNavigation,
recordAnalyticsForSideBySideWidgetHover,
resetAnalyticsForSideBySideHover,
} from "actions/ideActions";
import type { routeChanged } from "actions/focusHistoryActions";
import { NavigationMethod } from "utils/history";
import { getIDEViewMode } from "selectors/ideSelectors";
import {
JS_COLLECTION_EDITOR_PATH,
QUERIES_EDITOR_BASE_PATH,
WIDGETS_EDITOR_BASE_PATH,
} from "constants/routes";
import type { focusWidget } from "actions/widgetActions";
import { getCanvasWidgets } from "ee/selectors/entitiesSelector";
import { identifyEntityFromPath } from "navigation/FocusEntity";
import { getCurrentEntityInfo, isInSideBySideEditor } from "pages/Editor/utils";
export function* sendAnalyticsEventSaga(
type: ReduxActionType,
payload: unknown,
) {
try {
switch (type) {
case ReduxActionTypes.UPDATE_ACTION_INIT:
const { action, pageName } = payload as {
action: Action;
pageName: string;
};
const cleanActionConfiguration = cleanValuesInObjectForHashing(
action.actionConfiguration,
);
const actionConfigurationHash: string = yield generateHashFromString(
JSON.stringify(cleanActionConfiguration),
);
const originalActionId = get(
action,
`${RequestPayloadAnalyticsPath}.originalActionId`,
action.id,
);
AnalyticsUtil.logEvent("SAVE_ACTION", {
actionName: action.name,
pageName: pageName,
originalActionId: originalActionId,
actionId: action.id,
hash: actionConfigurationHash,
actionType: action.pluginType,
actionPlugin: action.pluginId,
});
}
} catch (e) {
log.error("Failed to send analytics event");
}
}
function* sendSideBySideWidgetHoverAnalyticsEventSaga() {
const {
navigated,
widgetTypes,
}: ReturnType<typeof getIdeCanvasSideBySideHoverState> = yield select(
getIdeCanvasSideBySideHoverState,
);
const payload = {
navigated,
widgetHover: widgetTypes.length > 0,
widgetTypes: Array.from(new Set(widgetTypes)),
};
yield put(resetAnalyticsForSideBySideHover());
AnalyticsUtil.logEvent("CANVAS_HOVER", payload);
}
function* routeChangeInSideBySideModeSaga({
payload,
}: ReturnType<typeof routeChanged>) {
const viewMode: ReturnType<typeof getIDEViewMode> =
yield select(getIDEViewMode);
const {
location: { pathname: pathName, state },
prevLocation: { pathname: prevPathName },
} = payload;
const invokedBy = state?.invokedBy;
if (
invokedBy === NavigationMethod.CanvasClick &&
viewMode === EditorViewMode.SplitScreen &&
pathName.includes(WIDGETS_EDITOR_BASE_PATH) &&
(prevPathName.includes(JS_COLLECTION_EDITOR_PATH) ||
prevPathName.includes(QUERIES_EDITOR_BASE_PATH))
) {
yield put(recordAnalyticsForSideBySideNavigation());
yield sendSideBySideWidgetHoverAnalyticsEventSaga();
}
}
function* focusWidgetInSideBySideModeSaga({
payload,
}: ReturnType<typeof focusWidget>) {
const { widgetId } = payload;
if (widgetId) {
const viewMode: ReturnType<typeof getIDEViewMode> =
yield select(getIDEViewMode);
const { appState, entity } = identifyEntityFromPath(
window.location.pathname,
);
const { segment } = getCurrentEntityInfo(entity);
if (isInSideBySideEditor({ appState, segment, viewMode })) {
const widgets: ReturnType<typeof getCanvasWidgets> =
yield select(getCanvasWidgets);
const widget = widgets[widgetId];
if (widget) {
yield put(recordAnalyticsForSideBySideWidgetHover(widget.type));
}
}
}
}
export default function* root() {
yield all([
takeEvery(
ReduxActionTypes.SEND_ANALYTICS_FOR_SIDE_BY_SIDE_HOVER,
sendSideBySideWidgetHoverAnalyticsEventSaga,
),
takeEvery(ReduxActionTypes.ROUTE_CHANGED, routeChangeInSideBySideModeSaga),
takeEvery(ReduxActionTypes.FOCUS_WIDGET, focusWidgetInSideBySideModeSaga),
]);
}