PromucFlow_constructor/app/client/src/utils/AnalyticsUtil.tsx

304 lines
8.3 KiB
TypeScript
Raw Normal View History

2020-03-03 07:02:53 +00:00
// Events
import * as log from "loglevel";
2020-05-28 18:10:26 +00:00
import FeatureFlag from "./featureFlags";
import smartlookClient from "smartlook-client";
import { getAppsmithConfigs } from "configs";
import * as Sentry from "@sentry/react";
import { ANONYMOUS_USERNAME, User } from "../constants/userConstants";
import { sha256 } from "js-sha256";
export type EventLocation =
| "LIGHTNING_MENU"
| "API_PANE"
| "QUERY_PANE"
| "QUERY_TEMPLATE";
2019-09-09 09:08:54 +00:00
export type EventName =
2020-03-11 13:59:46 +00:00
| "LOGIN_CLICK"
| "SIGNUP_CLICK"
2019-09-09 09:08:54 +00:00
| "PAGE_VIEW"
| "ADD_COMPONENT"
| "DELETE_COMPONENT"
2020-03-03 07:02:53 +00:00
| "RESIZE_COMPONENT"
| "WIDGET_DRAG"
| "WIDGET_DROP"
| "WIDGET_DELETE"
| "WIDGET_RESIZE_START"
| "WIDGET_RESIZE_END"
| "WIDGET_PROPERTY_UPDATE"
| "WIDGET_TOGGLE_JS_PROP"
| "WIDGET_CARD_DRAG"
| "WIDGET_CARD_DROP"
| "CREATE_PAGE"
| "PAGE_RENAME"
| "PAGE_SWITCH"
2020-03-06 04:59:24 +00:00
| "DELETE_PAGE"
| "SIDEBAR_NAVIGATION"
| "PUBLISH_APP"
| "PREVIEW_APP"
| "EDITOR_OPEN"
| "CREATE_ACTION"
Introducing Google Sheets Plugin (#3517) * cherry pick -make new * revert to enable fix from release * attempt to hook into existing datasource editor * gSheets plugin skeleton from Rest API * Changes for database migration * fix for auth code * separate it out * action page loads! * add to explorer * create action from datasource * Editor JSON WIP * working query form * Editor JSON WIP * import to * fix toast message * redirect from datasource and editor pages * fix onboarding * fix imports and constants * refactor form out * refactor queryForm * Merge branch 'release' into feature/google-sheets * Merge branch 'release' into feature/google-sheets * initial values from settings, editor and form * Check * remove dangling code around lightTheme * Safety net * remove class * try mouseover solve * force click * changes from review * fix action form name on import * Merge branch 'release' into feature/google-sheets * minor cleanup * Merge branch 'release' into feature/google-sheets * WIP * Google sheets changes * Merge conflicts * Merging and fixes, needs refactoring * Check * Merge branch 'release' into feature/google-sheets * Fixed tests * Add cloud services env variable * Clean up saga * Clean up * Refactoring * Deleted svg file * Minor fixes * Modified design to allow behaviour in google sheets methods (#3486) * Modified design to allow behaviour in google sheets methods * Review changes * Removed sysout * Added handling of edge cases with table data * Merge branch 'release' into feature/google-sheets * Fixes * Fixes * Added validations * Improved tests * Removed extraneous injected bean * Review changes * Fixed bug with method * Changes to Google sheets plugin's request and response structures (#3692) * Method changes * Removed logging * Renaming options * Reverting pom version * Modified type of collection variables, fixed errors * Converted row offset field to one that supports dynamic bindings * Review changes * List SAAS plugin type actions under lightning menu apis (#3820) * list saas plugin type actions under lightning menu apis * combine saas plugin type actions in the other sub menu of lightning menu Co-authored-by: Hetu Nandu <hetunandu@gmail.com> * Fix merge issues * Prettified query editor and a few fixes w/ ux * Test fixes * Reformatting request * code for REST added (#3876) Co-authored-by: hetunandu <hetu@appsmith.com> * Renamed body to row object * Renamed placeholder for range * Renamed range heading * Modifications to handle range semantics * Use spreadsheet Url instead of id * Ordering of methods * Removed logging * Add tests for Dynamic text controls * Add tests for url helpers * Fix coverage config * Nevermind * Interface changes * There is no body here * Yay to hints * Delete row field is separately handled as row index * placeholder support (#4001) * Fixed tests, typos and creating new sheets with random rows * Switched to using 'rowIndex' throughout * binding path added for query input field (#4016) * - Fixed QA bugs (#4032) - Split delete sheet into two - Removed dynamic query input types from hidden keys * Proper exceptions * Removed extra logging * Throw exception if update method does not match any of the columns * Same for bulk update * Zero-indexed delete row * I'm a space bound rocket ship * Logic to register installations with cs (#4062) * Logic to register installations with cs * Clean up * Casting to string * Checking to see if this makes the test pass * Added an extra null check Co-authored-by: Piyush <piyush@codeitout.com> Co-authored-by: hetunandu <hetu@appsmith.com> Co-authored-by: Hetu Nandu <hetunandu@gmail.com> Co-authored-by: Apeksha Bhosale <7846888+ApekshaBhosale@users.noreply.github.com>
2021-04-22 03:30:09 +00:00
| "SAVE_SAAS"
| "DELETE_SAAS"
| "RUN_SAAS_API"
2020-03-06 04:59:24 +00:00
| "SAVE_API"
2020-03-11 13:59:46 +00:00
| "SAVE_API_CLICK"
2020-03-06 04:59:24 +00:00
| "RUN_API"
2020-03-11 13:59:46 +00:00
| "RUN_API_CLICK"
2020-03-06 04:59:24 +00:00
| "DELETE_API"
2020-03-11 13:59:46 +00:00
| "DELETE_API_CLICK"
| "IMPORT_API"
| "EXPAND_API"
| "IMPORT_API_CLICK"
| "MOVE_API_CLICK"
| "ADD_API_PAGE"
2020-03-06 04:59:24 +00:00
| "DUPLICATE_API"
| "DUPLICATE_API_CLICK"
| "RUN_QUERY"
| "RUN_QUERY_CLICK"
| "DELETE_QUERY"
| "SAVE_QUERY"
2020-03-06 04:59:24 +00:00
| "MOVE_API"
| "3P_PROVIDER_CLICK"
2020-03-06 04:59:24 +00:00
| "API_SELECT"
| "CREATE_API_CLICK"
| "AUTO_COMPELTE_SHOW"
| "AUTO_COMPLETE_SELECT"
| "CREATE_APP_CLICK"
| "CREATE_APP"
| "CREATE_DATA_SOURCE_CLICK"
2020-03-11 13:59:46 +00:00
| "SAVE_DATA_SOURCE"
| "SAVE_DATA_SOURCE_CLICK"
| "TEST_DATA_SOURCE_SUCCESS"
| "TEST_DATA_SOURCE_CLICK"
| "CREATE_QUERY_CLICK"
2020-03-11 13:59:46 +00:00
| "NAVIGATE"
| "PAGE_LOAD"
2020-03-18 14:31:30 +00:00
| "NAVIGATE_EDITOR"
| "PROPERTY_PANE_OPEN"
| "PROPERTY_PANE_CLOSE"
| "PROPERTY_PANE_OPEN_CLICK"
| "PROPERTY_PANE_CLOSE_CLICK"
| "WIDGET_DELETE_UNDO"
| "WIDGET_COPY_VIA_SHORTCUT"
| "WIDGET_COPY"
| "WIDGET_CUT_VIA_SHORTCUT"
| "WIDGET_PASTE"
| "WIDGET_DELETE_VIA_SHORTCUT"
| "OPEN_HELP"
| "INVITE_USER"
| "ROUTE_CHANGE"
| "PROPERTY_PANE_CLOSE_CLICK"
2020-10-07 11:19:56 +00:00
| "APPLICATIONS_PAGE_LOAD"
2020-12-30 07:31:20 +00:00
| "EXECUTE_ACTION"
| "WELCOME_TOUR_CLICK"
2020-12-30 07:31:20 +00:00
| "ONBOARDING_WELCOME"
| "ONBOARDING_START_BUILDING"
| "ONBOARDING_INTRODUCTION"
2021-01-08 06:06:09 +00:00
| "ONBOARDING_ADD_QUERY"
2020-12-30 07:31:20 +00:00
| "ONBOARDING_RUN_QUERY"
| "ONBOARDING_ADD_WIDGET_CLICK"
| "ONBOARDING_ADD_WIDGET_TABLE"
| "ONBOARDING_ADD_WIDGET_INPUT"
| "ONBOARDING_ONSUBMIT_SUCCESS"
| "ONBOARDING_BINDING_HINT"
| "ONBOARDING_CHEAT"
2020-12-30 07:31:20 +00:00
| "ONBOARDING_SUCCESSFUL_BINDING"
| "ONBOARDING_DEPLOY"
| "ONBOARDING_SKIP_NOW"
| "ONBOARDING_NEXT_MISSION"
| "ONBOARDING_GO_HOME"
| "END_ONBOARDING"
2021-03-08 08:24:12 +00:00
| "ONBOARDING_COMPLETE"
| "OPEN_OMNIBAR"
| "CLOSE_OMNIBAR"
| "NAVIGATE_TO_ENTITY_FROM_OMNIBAR"
| "PAGE_SAVE"
2021-04-23 13:50:55 +00:00
| "CORRECT_BAD_BINDING"
| "OPEN_DEBUGGER"
| "DEBUGGER_TAB_SWITCH"
| "DEBUGGER_ENTITY_NAVIGATION";
2020-03-03 07:02:53 +00:00
function getApplicationId(location: Location) {
const pathSplit = location.pathname.split("/");
const applicationsIndex = pathSplit.findIndex(
2020-12-24 04:32:25 +00:00
(path) => path === "applications",
);
const appId = pathSplit[applicationsIndex + 1];
return appId;
}
2019-08-30 11:23:42 +00:00
class AnalyticsUtil {
2020-11-06 06:27:36 +00:00
static cachedAnonymoustId: string;
static cachedUserId: string;
static user?: User = undefined;
static initializeSmartLook(id: string) {
smartlookClient.init(id);
2019-09-09 09:08:54 +00:00
}
2019-08-30 11:23:42 +00:00
static initializeSegment(key: string) {
2019-09-09 09:08:54 +00:00
(function init(window: any) {
const analytics = (window.analytics = window.analytics || []);
if (!analytics.initialize) {
if (analytics.invoked) {
window.console &&
console.error &&
console.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;
n.src =
"https://cdn.segment.com/analytics.js/v1/" +
t +
"/analytics.min.js";
const a: any = document.getElementsByTagName("script")[0];
a.parentNode.insertBefore(n, a);
analytics._loadOptions = e;
};
analytics.SNIPPET_VERSION = "4.1.0";
analytics.load(key);
2019-09-09 09:08:54 +00:00
analytics.page();
}
})(window);
}
2019-08-30 11:23:42 +00:00
2020-10-07 11:19:56 +00:00
static logEvent(eventName: EventName, eventData: any = {}) {
2019-09-09 09:08:54 +00:00
const windowDoc: any = window;
2020-03-06 04:59:24 +00:00
let finalEventData = eventData;
const userData = AnalyticsUtil.user;
const appId = getApplicationId(windowDoc.location);
2020-03-06 04:59:24 +00:00
if (userData) {
const { segment } = getAppsmithConfigs();
2020-03-11 13:59:46 +00:00
const app = (userData.applications || []).find(
(app: any) => app.id === appId,
);
let user: any = {};
2020-11-05 16:51:22 +00:00
if (segment.enabled && segment.apiKey) {
user = {
userId: userData.username,
email: userData.email,
currentOrgId: userData.currentOrganizationId,
appId: appId,
appName: app ? app.name : undefined,
2020-11-23 13:17:12 +00:00
source: "cloud",
};
} else {
const userId = userData.username;
if (userId !== AnalyticsUtil.cachedUserId) {
AnalyticsUtil.cachedAnonymoustId = sha256(userId);
AnalyticsUtil.cachedUserId = userId;
}
user = {
userId: AnalyticsUtil.cachedAnonymoustId,
source: "ce",
};
}
2020-03-06 04:59:24 +00:00
finalEventData = {
...eventData,
userData: user.userId === ANONYMOUS_USERNAME ? undefined : user,
2020-03-06 04:59:24 +00:00
};
}
2020-12-30 07:31:20 +00:00
2020-03-03 07:02:53 +00:00
if (windowDoc.analytics) {
2020-11-05 16:51:22 +00:00
log.debug("Event fired", eventName, finalEventData);
2020-03-06 04:59:24 +00:00
windowDoc.analytics.track(eventName, finalEventData);
2020-03-03 07:02:53 +00:00
}
2019-09-09 09:08:54 +00:00
}
2019-08-30 11:23:42 +00:00
static identifyUser(userData: User) {
2020-11-05 16:51:22 +00:00
const { segment, smartLook } = getAppsmithConfigs();
2019-09-09 09:08:54 +00:00
const windowDoc: any = window;
2020-11-06 06:27:36 +00:00
const userId = userData.username;
2020-05-28 18:10:26 +00:00
FeatureFlag.identify(userData);
2020-03-03 07:02:53 +00:00
if (windowDoc.analytics) {
// This flag is only set on Appsmith Cloud. In this case, we get more detailed analytics of the user
2020-11-05 06:11:29 +00:00
if (segment.apiKey) {
const userProperties = {
email: userData.email,
name: userData.name,
userId: userId,
2020-11-23 13:17:12 +00:00
source: "cloud",
};
AnalyticsUtil.user = userData;
2020-11-05 16:51:22 +00:00
log.debug("Identify User " + userId);
windowDoc.analytics.identify(userId, userProperties);
2020-11-05 16:51:22 +00:00
} else if (segment.ceKey) {
// This is a self-hosted instance. Only send data if the analytics are NOT disabled by the user
// This is done by setting environment variable APPSMITH_DISABLE_TELEMETRY in the docker.env file
2020-11-06 06:27:36 +00:00
if (userId !== AnalyticsUtil.cachedUserId) {
AnalyticsUtil.cachedAnonymoustId = sha256(userId);
AnalyticsUtil.cachedUserId = userId;
}
2020-11-23 13:17:12 +00:00
const userProperties = {
2020-11-24 15:40:50 +00:00
userId: AnalyticsUtil.cachedUserId,
2020-11-23 13:17:12 +00:00
source: "ce",
};
2020-11-06 06:27:36 +00:00
log.debug(
"Identify Anonymous User " + AnalyticsUtil.cachedAnonymoustId,
);
2020-11-23 13:17:12 +00:00
windowDoc.analytics.identify(
AnalyticsUtil.cachedAnonymoustId,
userProperties,
);
}
2020-03-03 07:02:53 +00:00
}
Sentry.configureScope(function(scope) {
scope.setUser({
id: userId,
username: userData.username,
email: userData.email,
});
});
2020-11-05 02:31:01 +00:00
if (smartLook.enabled) {
smartlookClient.identify(userId, { email: userData.email });
2020-08-21 08:13:59 +00:00
}
2019-09-09 09:08:54 +00:00
}
static reset() {
const windowDoc: any = window;
if (windowDoc.Intercom) {
windowDoc.Intercom("shutdown");
}
2020-04-15 14:19:39 +00:00
windowDoc.analytics && windowDoc.analytics.reset();
windowDoc.mixpanel && windowDoc.mixpanel.reset();
}
2019-08-30 11:23:42 +00:00
}
2019-09-09 09:08:54 +00:00
export default AnalyticsUtil;