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

171 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-03-03 07:02:53 +00:00
// Events
2019-09-09 09:08:54 +00:00
export type EventName =
| "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_API"
| "SAVE_API"
| "RUN_API"
| "DELETE_API"
| "DUPLICATE_API"
| "MOVE_API"
| "API_SELECT"
| "CREATE_API_CLICK"
| "AUTO_COMPELTE_SHOW"
| "AUTO_COMPLETE_SELECT"
| "CREATE_APP_CLICK"
| "CREATE_APP"
| "CREATE_DATA_SOURCE_CLICK"
| "SAVE_DATA_SOURCE";
2020-03-03 07:02:53 +00:00
2019-09-09 09:08:54 +00:00
export type Gender = "MALE" | "FEMALE";
2019-08-30 11:23:42 +00:00
export interface User {
2019-09-09 09:08:54 +00:00
userId: string;
name: string;
email: string;
gender: Gender;
2019-08-30 11:23:42 +00:00
}
class AnalyticsUtil {
2020-03-06 04:59:24 +00:00
static user: any = {};
static appData: {
appId: string;
appName: string;
} = {
appId: "",
appName: "",
};
2019-09-09 09:08:54 +00:00
static initializeHotjar(id: string, sv: string) {
(function init(h: any, o: any, t: any, j: any, a?: any, r?: any) {
h.hj =
h.hj ||
function() {
(h.hj.q = h.hj.q || []).push(arguments); //eslint-disable-line prefer-rest-params
};
h._hjSettings = { hjid: id, hjsv: sv };
a = o.getElementsByTagName("head")[0];
r = o.createElement("script");
r.async = 1;
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
a.appendChild(r);
})(window, document, "//static.hotjar.com/c/hotjar-", ".js?sv=");
}
2019-08-30 11:23:42 +00:00
2019-09-09 09:08:54 +00:00
static initializeSegment() {
(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("O7rsLdWq7fhJI9rYsj1eatGAjuULTmfP");
analytics.page();
}
})(window);
}
2019-08-30 11:23:42 +00:00
2019-09-09 09:08:54 +00:00
static logEvent(eventName: EventName, eventData: any) {
const windowDoc: any = window;
2020-03-06 04:59:24 +00:00
let finalEventData = eventData;
const userData = AnalyticsUtil.user;
const appData = AnalyticsUtil.appData;
if (userData) {
finalEventData = {
...finalEventData,
userData: {
email: userData.email,
currentOrgId: userData.currentOrganizationId,
...appData,
},
};
}
2020-03-03 07:02:53 +00:00
if (windowDoc.analytics) {
2020-03-06 04:59:24 +00:00
windowDoc.analytics.track(eventName, finalEventData);
2020-03-03 07:02:53 +00:00
} else {
2020-03-06 04:59:24 +00:00
console.log("Event fired", 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
2019-09-09 09:08:54 +00:00
static identifyUser(userId: string, userData: User) {
const windowDoc: any = window;
2020-03-06 04:59:24 +00:00
AnalyticsUtil.user = userData;
2020-03-03 07:02:53 +00:00
if (windowDoc.analytics) {
windowDoc.analytics.identify(userId, userData);
}
2019-09-09 09:08:54 +00:00
}
2020-03-06 04:59:24 +00:00
static setAppData(appId: string, appName: string) {
AnalyticsUtil.appData = {
appId: appId,
appName: appName,
};
}
2019-08-30 11:23:42 +00:00
}
2019-09-09 09:08:54 +00:00
export default AnalyticsUtil;