PromucFlow_constructor/app/client/src/utils/AnalyticsUtil.tsx
Hetu Nandu 31bca0b123
feat: Email Verification (#25854)
## Description

Adds a setting for Admin Users to enable Email verification of users who
are signing up using "Form login" When enabled, it will send a
verification email to a user who is signing up on a tenant and only when
they verify (by clicking on the link in email) they will be allowed to
proceed to the rest of the sign up process.

Corresponding EE PR for the email template:
https://github.com/appsmithorg/appsmith-ee/pull/2153

#### PR fixes following issue(s)
Fixes #21387
Fixes #25552

#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Testing
>
#### How Has This Been Tested?
- [x] Manual
- [x] Jest
- [x] Cypress
We have mocked server apis to respond with different states and tested
the ui on that change

#### Test Plan
https://github.com/appsmithorg/TestSmith/issues/2459
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [x] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: nilansh <nilansh@appsmith.com>
2023-08-26 09:52:23 +05:30

261 lines
8.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Events
import * as log from "loglevel";
import smartlookClient from "smartlook-client";
import { getAppsmithConfigs } from "@appsmith/configs";
import * as Sentry from "@sentry/react";
import type { User } from "constants/userConstants";
import { ANONYMOUS_USERNAME } from "constants/userConstants";
import { sha256 } from "js-sha256";
import type { EventName } from "@appsmith/utils/analyticsUtilTypes";
declare global {
interface Window {
// Zipy is added via script tags in index.html
zipy: {
identify: (uid: string, userInfo: Record<string, string>) => void;
anonymize: () => void;
};
}
}
function getApplicationId(location: Location) {
const pathSplit = location.pathname.split("/");
const applicationsIndex = pathSplit.findIndex(
(path) => path === "applications",
);
const appId = pathSplit[applicationsIndex + 1];
return appId;
}
class AnalyticsUtil {
static cachedAnonymoustId: string;
static cachedUserId: string;
static user?: User = undefined;
static blockTrackEvent: boolean | undefined;
static instanceId?: string = "";
static initializeSmartLook(id: string) {
smartlookClient.init(id);
}
static initializeSegment(key: string) {
const initPromise = new Promise<boolean>((resolve) => {
(function init(window: any) {
const analytics = (window.analytics = window.analytics || []);
if (!analytics.initialize) {
if (analytics.invoked) {
log.error("Segment snippet included twice.");
} else {
analytics.invoked = !0;
analytics.methods = [
"trackSubmit",
"trackClick",
"trackLink",
"trackForm",
"pageview",
"identify",
"reset",
"group",
"track",
"ready",
"alias",
"debug",
"page",
"once",
"off",
"on",
];
analytics.factory = function (t: any) {
return function () {
const e = Array.prototype.slice.call(arguments); //eslint-disable-line prefer-rest-params
e.unshift(t);
analytics.push(e);
return analytics;
};
};
}
for (let t: any = 0; t < analytics.methods.length; t++) {
const e = analytics.methods[t];
analytics[e] = analytics.factory(e);
}
analytics.load = function (t: any, e: any) {
const n = document.createElement("script");
n.type = "text/javascript";
n.async = !0;
// Ref: https://www.notion.so/appsmith/530051a2083040b5bcec15a46121aea3
n.src = "https://a.appsmith.com/reroute/" + t + "/main.js";
const a: any = document.getElementsByTagName("script")[0];
a.parentNode.insertBefore(n, a);
analytics._loadOptions = e;
};
analytics.ready(() => {
resolve(true);
});
setTimeout(() => {
resolve(false);
}, 2000);
analytics.SNIPPET_VERSION = "4.1.0";
// Ref: https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#batching
analytics.load(key, {
integrations: {
"Segment.io": {
deliveryStrategy: {
strategy: "batching", // The delivery strategy used for sending events to Segment
config: {
size: 100, // The batch size is the threshold that forces all batched events to be sent once its reached.
timeout: 1000, // The number of milliseconds that forces all events queued for batching to be sent, regardless of the batch size, once its reached
},
},
},
},
});
if (!AnalyticsUtil.blockTrackEvent) {
analytics.page();
}
}
})(window);
});
return initPromise;
}
static logEvent(eventName: EventName, eventData: any = {}) {
if (AnalyticsUtil.blockTrackEvent) {
return;
}
const windowDoc: any = window;
let finalEventData = eventData;
const userData = AnalyticsUtil.user;
const instanceId = AnalyticsUtil.instanceId;
const appId = getApplicationId(windowDoc.location);
const { appVersion, segment } = getAppsmithConfigs();
if (userData) {
let user: any = {};
if (segment.apiKey) {
user = {
userId: userData.username,
email: userData.email,
appId: appId,
source: "cloud",
};
} else {
const userId = userData.username;
if (userId !== AnalyticsUtil.cachedUserId) {
AnalyticsUtil.cachedAnonymoustId = sha256(userId);
AnalyticsUtil.cachedUserId = userId;
}
user = {
userId: AnalyticsUtil.cachedAnonymoustId,
source: "ce",
};
}
finalEventData = {
...eventData,
userData: user.userId === ANONYMOUS_USERNAME ? undefined : user,
};
}
finalEventData = { ...finalEventData, instanceId, version: appVersion.id };
if (windowDoc.analytics) {
log.debug("Event fired", eventName, finalEventData);
windowDoc.analytics.track(eventName, finalEventData);
} else {
log.debug("Event fired locally", eventName, finalEventData);
}
}
static identifyUser(userData: User) {
const { segment, sentry, smartLook } = getAppsmithConfigs();
const windowDoc: any = window;
const userId = userData.username;
if (windowDoc.analytics) {
// This flag is only set on Appsmith Cloud. In this case, we get more detailed analytics of the user
if (segment.apiKey) {
const userProperties = {
email: userData.email,
name: userData.name,
userId: userId,
source: "cloud",
emailVerified: userData.emailVerified,
};
AnalyticsUtil.user = userData;
log.debug("Identify User " + userId);
windowDoc.analytics.identify(userId, userProperties);
} else if (segment.ceKey) {
// This is a self-hosted instance. Only send data if the analytics are NOT disabled by the user
if (userId !== AnalyticsUtil.cachedUserId) {
AnalyticsUtil.cachedAnonymoustId = sha256(userId);
AnalyticsUtil.cachedUserId = userId;
}
const userProperties = {
userId: AnalyticsUtil.cachedAnonymoustId,
source: "ce",
};
log.debug(
"Identify Anonymous User " + AnalyticsUtil.cachedAnonymoustId,
);
windowDoc.analytics.identify(
AnalyticsUtil.cachedAnonymoustId,
userProperties,
);
}
}
if (sentry.enabled) {
Sentry.configureScope(function (scope) {
scope.setUser({
id: userId,
username: userData.username,
email: userData.email,
});
});
}
if (smartLook.enabled) {
smartlookClient.identify(userId, { email: userData.email });
}
// If zipy was included, identify this user on the platform
if (window.zipy && userId) {
window.zipy.identify(userId, {
email: userData.email,
username: userData.username,
});
}
AnalyticsUtil.blockTrackEvent = false;
}
static initInstanceId(instanceId: string) {
AnalyticsUtil.instanceId = instanceId;
}
static getAnonymousId() {
const windowDoc: any = window;
const { segment } = getAppsmithConfigs();
if (windowDoc.analytics && windowDoc.analytics.user) {
return windowDoc.analytics.user().anonymousId();
} else if (segment.enabled) {
return localStorage.getItem("ajs_anonymous_id")?.replaceAll('"', "");
}
}
static reset() {
const windowDoc: any = window;
if (windowDoc.Intercom) {
windowDoc.Intercom("shutdown");
}
windowDoc.analytics && windowDoc.analytics.reset();
windowDoc.mixpanel && windowDoc.mixpanel.reset();
window.zipy && window.zipy.anonymize();
}
static removeAnalytics() {
AnalyticsUtil.blockTrackEvent = false;
(window as any).analytics = undefined;
}
}
export default AnalyticsUtil;