-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
index.ts
231 lines (205 loc) · 7.05 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import _Vue from "vue";
import {
ITelemetrySettings,
IDataObject,
} from 'n8n-workflow';
import { ILogLevel, INodeCreateElement, IRootState } from "@/Interface";
import { Route } from "vue-router";
import { Store } from "vuex";
declare module 'vue/types/vue' {
interface Vue {
$telemetry: Telemetry;
}
}
export function TelemetryPlugin(vue: typeof _Vue): void {
const telemetry = new Telemetry();
Object.defineProperty(vue, '$telemetry', {
get() { return telemetry; },
});
Object.defineProperty(vue.prototype, '$telemetry', {
get() { return telemetry; },
});
}
interface IUserNodesPanelSessionData {
nodeFilter: string;
resultsNodes: string[];
filterMode: string;
}
interface IUserNodesPanelSession {
sessionId: string;
data: IUserNodesPanelSessionData;
}
class Telemetry {
private pageEventQueue: Array<{route: Route}>;
private previousPath: string;
private store: Store<IRootState> | null;
private get telemetry() {
// @ts-ignore
return window.rudderanalytics;
}
private userNodesPanelSession: IUserNodesPanelSession = {
sessionId: '',
data: {
nodeFilter: '',
resultsNodes: [],
filterMode: 'Regular',
},
};
constructor() {
this.pageEventQueue = [];
this.previousPath = '';
this.store = null;
}
init(options: ITelemetrySettings, { instanceId, logLevel, userId, store }: { instanceId: string, logLevel?: ILogLevel, userId?: string, store: Store<IRootState> }) {
if (options.enabled && !this.telemetry) {
if(!options.config) {
return;
}
this.store = store;
const logging = logLevel === 'debug' ? { logLevel: 'DEBUG'} : {};
this.loadTelemetryLibrary(options.config.key, options.config.url, { integrations: { All: false }, loadIntegration: false, ...logging});
this.identify(instanceId, userId);
this.flushPageEvents();
}
}
identify(instanceId: string, userId?: string) {
const traits = { instance_id: instanceId };
if (userId) {
this.telemetry.identify(`${instanceId}#${userId}`, traits);
}
else {
this.telemetry.reset();
this.telemetry.identify(undefined, traits);
}
}
track(event: string, properties?: IDataObject) {
if (this.telemetry) {
this.telemetry.track(event, properties);
}
}
page(route: Route) {
if (this.telemetry) {
if (route.path === this.previousPath) { // avoid duplicate requests query is changed for example on search page
return;
}
this.previousPath = route.path;
const pageName = route.name;
let properties: {[key: string]: string} = {};
if (this.store && route.meta && route.meta.telemetry && typeof route.meta.telemetry.getProperties === 'function') {
properties = route.meta.telemetry.getProperties(route, this.store);
}
const category = (route.meta && route.meta.telemetry && route.meta.telemetry.pageCategory) || 'Editor';
this.telemetry.page(category, pageName, properties);
}
else {
this.pageEventQueue.push({
route,
});
}
}
flushPageEvents() {
const queue = this.pageEventQueue;
this.pageEventQueue = [];
queue.forEach(({route}) => {
this.page(route);
});
}
trackNodesPanel(event: string, properties: IDataObject = {}) {
if (this.telemetry) {
properties.nodes_panel_session_id = this.userNodesPanelSession.sessionId;
switch (event) {
case 'nodeView.createNodeActiveChanged':
if (properties.createNodeActive !== false) {
this.resetNodesPanelSession();
properties.nodes_panel_session_id = this.userNodesPanelSession.sessionId;
this.telemetry.track('User opened nodes panel', properties);
}
break;
case 'nodeCreateList.selectedTypeChanged':
this.userNodesPanelSession.data.filterMode = properties.new_filter as string;
this.telemetry.track('User changed nodes panel filter', properties);
break;
case 'nodeCreateList.destroyed':
if(this.userNodesPanelSession.data.nodeFilter.length > 0 && this.userNodesPanelSession.data.nodeFilter !== '') {
this.telemetry.track('User entered nodes panel search term', this.generateNodesPanelEvent());
}
break;
case 'nodeCreateList.nodeFilterChanged':
if((properties.newValue as string).length === 0 && this.userNodesPanelSession.data.nodeFilter.length > 0) {
this.telemetry.track('User entered nodes panel search term', this.generateNodesPanelEvent());
}
if((properties.newValue as string).length > (properties.oldValue as string || '').length) {
this.userNodesPanelSession.data.nodeFilter = properties.newValue as string;
this.userNodesPanelSession.data.resultsNodes = ((properties.filteredNodes || []) as INodeCreateElement[]).map((node: INodeCreateElement) => node.key);
}
break;
case 'nodeCreateList.onCategoryExpanded':
properties.is_subcategory = false;
this.telemetry.track('User viewed node category', properties);
break;
case 'nodeCreateList.onSubcategorySelected':
const selectedProperties = (properties.selected as IDataObject).properties as IDataObject;
if(selectedProperties && selectedProperties.subcategory) {
properties.category_name = selectedProperties.subcategory;
}
properties.is_subcategory = true;
delete properties.selected;
this.telemetry.track('User viewed node category', properties);
break;
case 'nodeView.addNodeButton':
this.telemetry.track('User added node to workflow canvas', properties);
break;
case 'nodeView.addSticky':
this.telemetry.track('User inserted workflow note', properties);
break;
default:
break;
}
}
}
private resetNodesPanelSession() {
this.userNodesPanelSession.sessionId = `nodes_panel_session_${(new Date()).valueOf()}`;
this.userNodesPanelSession.data = {
nodeFilter: '',
resultsNodes: [],
filterMode: 'All',
};
}
private generateNodesPanelEvent() {
return {
search_string: this.userNodesPanelSession.data.nodeFilter,
results_count: this.userNodesPanelSession.data.resultsNodes.length,
filter_mode: this.userNodesPanelSession.data.filterMode,
nodes_panel_session_id: this.userNodesPanelSession.sessionId,
};
}
private loadTelemetryLibrary(key: string, url: string, options: IDataObject) {
// @ts-ignore
window.rudderanalytics = window.rudderanalytics || [];
this.telemetry.methods = ["load", "page", "track", "identify", "alias", "group", "ready", "reset", "getAnonymousId", "setAnonymousId"];
this.telemetry.factory = (t: any) => { // tslint:disable-line:no-any
return (...args: any[]) => { // tslint:disable-line:no-any
const r = Array.prototype.slice.call(args);
r.unshift(t);
this.telemetry.push(r);
return this.telemetry;
};
};
for (let t = 0; t < this.telemetry.methods.length; t++) {
const r = this.telemetry.methods[t];
this.telemetry[r] = this.telemetry.factory(r);
}
this.telemetry.loadJS = () => {
const r = document.createElement("script");
r.type = "text/javascript";
r.async = !0;
r.src = "https://cdn.rudderlabs.com/v1/rudder-analytics.min.js";
const a = document.getElementsByTagName("script")[0];
if(a && a.parentNode) {
a.parentNode.insertBefore(r, a);
}
};
this.telemetry.loadJS();
this.telemetry.load(key, url, options);
}
}