PromucFlow_constructor/app/client/src/usagePulse/utils.ts
Ankita Kinger 6244e28fed
chore: Update analytics to pass the correct source information in identify user call (#32591)
## Description

Updating analytics to pass the correct source information

Fixes [#32266](https://github.com/appsmithorg/appsmith/issues/32266)

## Automation

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

### 🔍 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/8750877755>
> Commit: 6fedefebd3867aee79877b7ed105c90888005cfd
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=8750877755&attempt=1"
target="_blank">Click here!</a>

<!-- end of auto-generated comment: Cypress test results  -->
2024-04-19 15:06:50 +05:30

72 lines
1.9 KiB
TypeScript

import { isEditorPath } from "@appsmith/pages/Editor/Explorer/helpers";
import { APP_MODE } from "entities/App";
import { isNil } from "lodash";
import nanoid from "nanoid";
import { getAppMode } from "@appsmith/selectors/entitiesSelector";
import store from "store";
import AnalyticsUtil from "@appsmith/utils/AnalyticsUtil";
import { FALLBACK_KEY } from "@appsmith/constants/UsagePulse";
//TODO (Dipyaman): We should return a promise that will get resolved only on success or rejected after the retries
export const fetchWithRetry = (config: {
url: any;
payload: Record<string, unknown>;
retries: number;
retryTimeout: number;
}) => {
fetch(config.url, {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(config.payload),
keepalive: true,
})
.then((res) => {
if (!res.ok) throw new Error();
})
.catch(() => {
if (config.retries > 0) {
setTimeout(fetchWithRetry, config.retryTimeout, {
url: config.url,
payload: config.payload,
retries: config.retries - 1,
retryTimeout: config.retryTimeout,
});
}
});
};
export const getUsagePulsePayload = (
isTelemetryEnabled: boolean,
isAnonymousUser: boolean,
) => {
let mode = getAppMode(store.getState());
if (isNil(mode)) {
mode = isEditorPath(window.location.pathname)
? APP_MODE.EDIT
: APP_MODE.PUBLISHED;
}
const data: Record<string, unknown> = {
viewMode: mode === APP_MODE.PUBLISHED,
};
if (isAnonymousUser) {
if (isTelemetryEnabled) {
data["anonymousUserId"] = AnalyticsUtil.getAnonymousId();
} else {
let fallback = localStorage.getItem(FALLBACK_KEY);
if (!fallback) {
fallback = nanoid() as string;
localStorage.setItem(FALLBACK_KEY, fallback);
}
data["anonymousUserId"] = fallback;
}
}
return data;
};