@flisk/analyze-tracking 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -61
- package/package.json +16 -3
- package/schema.json +4 -0
- package/src/analyze/analyzeGoFile.js +285 -89
- package/src/analyze/analyzeJsFile.js +20 -5
- package/src/analyze/analyzePythonFile.js +1 -0
- package/src/analyze/analyzeRubyFile.js +20 -10
- package/src/analyze/analyzeTsFile.js +138 -15
- package/src/analyze/helpers.js +462 -23
- package/src/analyze/index.js +0 -1
- package/src/analyze/pythonTrackingAnalyzer.py +149 -47
- package/.github/workflows/npm-publish.yml +0 -33
- package/.github/workflows/pr-check.yml +0 -17
- package/jest.config.js +0 -7
- package/tests/detectSource.test.js +0 -20
- package/tests/extractProperties.test.js +0 -109
- package/tests/findWrappingFunction.test.js +0 -30
|
@@ -33,10 +33,20 @@ function analyzeJsFile(filePath, customFunction) {
|
|
|
33
33
|
if (source === 'googleanalytics' && node.arguments.length >= 3) {
|
|
34
34
|
eventName = node.arguments[1]?.value || null;
|
|
35
35
|
propertiesNode = node.arguments[2];
|
|
36
|
-
} else if (source === 'snowplow' && node.arguments.length
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
} else if (source === 'snowplow' && node.arguments.length > 0) {
|
|
37
|
+
// Snowplow pattern: tracker.track(buildStructEvent({...}))
|
|
38
|
+
const firstArg = node.arguments[0];
|
|
39
|
+
if (firstArg.type === 'CallExpression' && firstArg.arguments.length > 0) {
|
|
40
|
+
const structEventArg = firstArg.arguments[0];
|
|
41
|
+
if (structEventArg.type === 'ObjectExpression') {
|
|
42
|
+
const actionProperty = structEventArg.properties.find(prop => prop.key.name === 'action');
|
|
43
|
+
eventName = actionProperty ? actionProperty.value.value : null;
|
|
44
|
+
propertiesNode = structEventArg;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} else if (source === 'mparticle' && node.arguments.length >= 3) {
|
|
48
|
+
eventName = node.arguments[0]?.value || null;
|
|
49
|
+
propertiesNode = node.arguments[2];
|
|
40
50
|
} else if (node.arguments.length >= 2) {
|
|
41
51
|
eventName = node.arguments[0]?.value || null;
|
|
42
52
|
propertiesNode = node.arguments[1];
|
|
@@ -46,7 +56,12 @@ function analyzeJsFile(filePath, customFunction) {
|
|
|
46
56
|
const functionName = findWrappingFunctionJs(node, ancestors);
|
|
47
57
|
|
|
48
58
|
if (eventName && propertiesNode && propertiesNode.type === 'ObjectExpression') {
|
|
49
|
-
|
|
59
|
+
let properties = extractJsProperties(propertiesNode);
|
|
60
|
+
|
|
61
|
+
// For Snowplow, remove 'action' from properties since it's used as the event name
|
|
62
|
+
if (source === 'snowplow' && properties.action) {
|
|
63
|
+
delete properties.action;
|
|
64
|
+
}
|
|
50
65
|
|
|
51
66
|
events.push({
|
|
52
67
|
eventName,
|
|
@@ -25,6 +25,7 @@ async function analyzePythonFile(filePath, customFunction) {
|
|
|
25
25
|
py.globals.set('code', code);
|
|
26
26
|
py.globals.set('filepath', filePath);
|
|
27
27
|
py.globals.set('custom_function', customFunction || null);
|
|
28
|
+
py.globals.set('__name__', null);
|
|
28
29
|
|
|
29
30
|
// Run the Python analyzer
|
|
30
31
|
py.runPython(analyzerCode);
|
|
@@ -46,12 +46,16 @@ class TrackingVisitor {
|
|
|
46
46
|
const objectName = node.receiver.name;
|
|
47
47
|
const methodName = node.name;
|
|
48
48
|
|
|
49
|
-
// Segment
|
|
50
|
-
|
|
49
|
+
// Segment and Rudderstack (both use similar format)
|
|
50
|
+
// Analytics.track (Segment) or analytics.track (Rudderstack)
|
|
51
|
+
if ((objectName === 'Analytics' || objectName === 'analytics') && methodName === 'track') {
|
|
52
|
+
// Try to determine if it's Rudderstack based on context
|
|
53
|
+
// For now, we'll treat lowercase 'analytics' as Rudderstack
|
|
54
|
+
return objectName === 'analytics' ? 'rudderstack' : 'segment';
|
|
55
|
+
}
|
|
51
56
|
|
|
52
57
|
// Mixpanel (Ruby SDK uses Mixpanel::Tracker instance)
|
|
53
|
-
if (methodName === 'track' &&
|
|
54
|
-
node.receiver.name === 'tracker') return 'mixpanel';
|
|
58
|
+
if (methodName === 'track' && objectName === 'tracker') return 'mixpanel';
|
|
55
59
|
|
|
56
60
|
// PostHog
|
|
57
61
|
if (objectName === 'posthog' && methodName === 'capture') return 'posthog';
|
|
@@ -67,7 +71,8 @@ class TrackingVisitor {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
extractEventName(node, source) {
|
|
70
|
-
if (source === 'segment') {
|
|
74
|
+
if (source === 'segment' || source === 'rudderstack') {
|
|
75
|
+
// Both Segment and Rudderstack use the same format
|
|
71
76
|
const params = node.arguments_.arguments_[0].elements;
|
|
72
77
|
const eventProperty = params.find(param => param?.key?.unescaped?.value === 'event');
|
|
73
78
|
return eventProperty?.value?.unescaped?.value || null;
|
|
@@ -111,7 +116,8 @@ class TrackingVisitor {
|
|
|
111
116
|
async extractProperties(node, source) {
|
|
112
117
|
const { HashNode, ArrayNode } = await import('@ruby/prism');
|
|
113
118
|
|
|
114
|
-
if (source === 'segment') {
|
|
119
|
+
if (source === 'segment' || source === 'rudderstack') {
|
|
120
|
+
// Both Segment and Rudderstack use the same format
|
|
115
121
|
const params = node.arguments_.arguments_[0].elements;
|
|
116
122
|
const properties = {};
|
|
117
123
|
|
|
@@ -158,10 +164,10 @@ class TrackingVisitor {
|
|
|
158
164
|
const args = node.arguments_.arguments_;
|
|
159
165
|
const properties = {};
|
|
160
166
|
|
|
161
|
-
// Add distinct_id as property
|
|
162
|
-
if (args && args.length > 0
|
|
167
|
+
// Add distinct_id as property (even if it's a variable)
|
|
168
|
+
if (args && args.length > 0) {
|
|
163
169
|
properties.distinct_id = {
|
|
164
|
-
type:
|
|
170
|
+
type: await this.getValueType(args[0])
|
|
165
171
|
};
|
|
166
172
|
}
|
|
167
173
|
|
|
@@ -300,7 +306,7 @@ class TrackingVisitor {
|
|
|
300
306
|
}
|
|
301
307
|
|
|
302
308
|
async visit(node) {
|
|
303
|
-
const { CallNode, ProgramNode, StatementsNode, DefNode, IfNode, BlockNode, ArgumentsNode, HashNode, AssocNode, ClassNode } = await import('@ruby/prism');
|
|
309
|
+
const { CallNode, ProgramNode, StatementsNode, DefNode, IfNode, BlockNode, ArgumentsNode, HashNode, AssocNode, ClassNode, ModuleNode } = await import('@ruby/prism');
|
|
304
310
|
if (!node) return;
|
|
305
311
|
|
|
306
312
|
this.ancestors.push(node);
|
|
@@ -344,6 +350,10 @@ class TrackingVisitor {
|
|
|
344
350
|
if (node.body) {
|
|
345
351
|
await this.visit(node.body);
|
|
346
352
|
}
|
|
353
|
+
} else if (node instanceof ModuleNode) {
|
|
354
|
+
if (node.body) {
|
|
355
|
+
await this.visit(node.body);
|
|
356
|
+
}
|
|
347
357
|
} else if (node instanceof DefNode) {
|
|
348
358
|
if (node.body) {
|
|
349
359
|
await this.visit(node.body);
|
|
@@ -1,5 +1,65 @@
|
|
|
1
1
|
const ts = require('typescript');
|
|
2
|
-
const { detectSourceTs, findWrappingFunctionTs, extractTsProperties } = require('./helpers');
|
|
2
|
+
const { detectSourceTs, findWrappingFunctionTs, extractTsProperties, resolveIdentifierToInitializer, extractInterfaceProperties } = require('./helpers');
|
|
3
|
+
|
|
4
|
+
function resolveUnresolvedTypes(properties, checker, sourceFile) {
|
|
5
|
+
const resolved = {};
|
|
6
|
+
|
|
7
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
8
|
+
if (value && typeof value === 'object') {
|
|
9
|
+
if (value.__unresolved) {
|
|
10
|
+
// Try to find and resolve the type
|
|
11
|
+
const typeName = value.__unresolved;
|
|
12
|
+
delete value.__unresolved;
|
|
13
|
+
|
|
14
|
+
// This is a simplified approach - in practice, you'd need to find the actual type declaration
|
|
15
|
+
// For now, we'll keep the object type but remove the unresolved marker
|
|
16
|
+
resolved[key] = value;
|
|
17
|
+
} else if (value.type === 'array' && value.items && value.items.__unresolved) {
|
|
18
|
+
// Handle unresolved array element types
|
|
19
|
+
const itemTypeName = value.items.__unresolved;
|
|
20
|
+
delete value.items.__unresolved;
|
|
21
|
+
resolved[key] = value;
|
|
22
|
+
} else if (value.type === 'array' && value.items && typeof value.items.type === 'string' && value.items.type.includes(' ')) {
|
|
23
|
+
// Handle types like "readonly Product" - extract the actual type name
|
|
24
|
+
const typeString = value.items.type;
|
|
25
|
+
const actualType = typeString.replace(/^readonly\s+/, '').trim();
|
|
26
|
+
|
|
27
|
+
// If it looks like a custom type, mark it as object
|
|
28
|
+
if (actualType[0] === actualType[0].toUpperCase() && !actualType.includes('<')) {
|
|
29
|
+
resolved[key] = {
|
|
30
|
+
...value,
|
|
31
|
+
items: {
|
|
32
|
+
type: 'object'
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
} else {
|
|
36
|
+
resolved[key] = value;
|
|
37
|
+
}
|
|
38
|
+
} else if (value.type === 'object' && value.properties) {
|
|
39
|
+
// Recursively resolve nested properties
|
|
40
|
+
resolved[key] = {
|
|
41
|
+
...value,
|
|
42
|
+
properties: resolveUnresolvedTypes(value.properties, checker, sourceFile)
|
|
43
|
+
};
|
|
44
|
+
} else if (value.type === 'array' && value.items && value.items.properties) {
|
|
45
|
+
// Recursively resolve array item properties
|
|
46
|
+
resolved[key] = {
|
|
47
|
+
...value,
|
|
48
|
+
items: {
|
|
49
|
+
...value.items,
|
|
50
|
+
properties: value.items.properties ? resolveUnresolvedTypes(value.items.properties, checker, sourceFile) : undefined
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
} else {
|
|
54
|
+
resolved[key] = value;
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
resolved[key] = value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return resolved;
|
|
62
|
+
}
|
|
3
63
|
|
|
4
64
|
function analyzeTsFile(filePath, program, customFunction) {
|
|
5
65
|
let events = [];
|
|
@@ -24,10 +84,50 @@ function analyzeTsFile(filePath, program, customFunction) {
|
|
|
24
84
|
if (source === 'googleanalytics' && node.arguments.length >= 3) {
|
|
25
85
|
eventName = node.arguments[1]?.text || null;
|
|
26
86
|
propertiesNode = node.arguments[2];
|
|
27
|
-
} else if (source === 'snowplow' && node.arguments.length
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
87
|
+
} else if (source === 'snowplow' && node.arguments.length > 0) {
|
|
88
|
+
// Snowplow pattern: tracker.track(buildStructEvent({...})) or tracker.track(payload)
|
|
89
|
+
const firstArg = node.arguments[0];
|
|
90
|
+
|
|
91
|
+
// Check if it's a direct buildStructEvent call
|
|
92
|
+
if (ts.isCallExpression(firstArg) &&
|
|
93
|
+
ts.isIdentifier(firstArg.expression) &&
|
|
94
|
+
firstArg.expression.escapedText === 'buildStructEvent' &&
|
|
95
|
+
firstArg.arguments.length > 0) {
|
|
96
|
+
const structEventArg = firstArg.arguments[0];
|
|
97
|
+
if (ts.isObjectLiteralExpression(structEventArg)) {
|
|
98
|
+
// Find the action property for event name
|
|
99
|
+
const actionProp = structEventArg.properties.find(
|
|
100
|
+
prop => prop.name && prop.name.escapedText === 'action'
|
|
101
|
+
);
|
|
102
|
+
if (actionProp && actionProp.initializer && ts.isStringLiteral(actionProp.initializer)) {
|
|
103
|
+
eventName = actionProp.initializer.text;
|
|
104
|
+
propertiesNode = structEventArg;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Check if it's a variable reference (e.g., const payload = buildStructEvent({...}))
|
|
109
|
+
else if (ts.isIdentifier(firstArg)) {
|
|
110
|
+
const resolvedNode = resolveIdentifierToInitializer(checker, firstArg, sourceFile);
|
|
111
|
+
if (resolvedNode && ts.isCallExpression(resolvedNode) &&
|
|
112
|
+
ts.isIdentifier(resolvedNode.expression) &&
|
|
113
|
+
resolvedNode.expression.escapedText === 'buildStructEvent' &&
|
|
114
|
+
resolvedNode.arguments.length > 0) {
|
|
115
|
+
const structEventArg = resolvedNode.arguments[0];
|
|
116
|
+
if (ts.isObjectLiteralExpression(structEventArg)) {
|
|
117
|
+
const actionProp = structEventArg.properties.find(
|
|
118
|
+
prop => prop.name && prop.name.escapedText === 'action'
|
|
119
|
+
);
|
|
120
|
+
if (actionProp && actionProp.initializer && ts.isStringLiteral(actionProp.initializer)) {
|
|
121
|
+
eventName = actionProp.initializer.text;
|
|
122
|
+
propertiesNode = structEventArg;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} else if (source === 'mparticle' && node.arguments.length >= 3) {
|
|
128
|
+
// mParticle: first param is event name, second is event type (ignored), third is properties
|
|
129
|
+
eventName = node.arguments[0]?.text || null;
|
|
130
|
+
propertiesNode = node.arguments[2];
|
|
31
131
|
} else if (node.arguments.length >= 2) {
|
|
32
132
|
eventName = node.arguments[0]?.text || null;
|
|
33
133
|
propertiesNode = node.arguments[1];
|
|
@@ -36,17 +136,40 @@ function analyzeTsFile(filePath, program, customFunction) {
|
|
|
36
136
|
const line = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
37
137
|
const functionName = findWrappingFunctionTs(node);
|
|
38
138
|
|
|
39
|
-
if (eventName && propertiesNode
|
|
139
|
+
if (eventName && propertiesNode) {
|
|
40
140
|
try {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
properties,
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
141
|
+
let properties = null;
|
|
142
|
+
|
|
143
|
+
// Check if properties is an object literal
|
|
144
|
+
if (ts.isObjectLiteralExpression(propertiesNode)) {
|
|
145
|
+
properties = extractTsProperties(checker, propertiesNode);
|
|
146
|
+
}
|
|
147
|
+
// Check if properties is an identifier (variable reference)
|
|
148
|
+
else if (ts.isIdentifier(propertiesNode)) {
|
|
149
|
+
const resolvedNode = resolveIdentifierToInitializer(checker, propertiesNode, sourceFile);
|
|
150
|
+
if (resolvedNode && ts.isObjectLiteralExpression(resolvedNode)) {
|
|
151
|
+
properties = extractTsProperties(checker, resolvedNode);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (properties) {
|
|
156
|
+
// For Snowplow, remove 'action' from properties since it's used as the event name
|
|
157
|
+
if (source === 'snowplow' && properties.action) {
|
|
158
|
+
delete properties.action;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Clean up any unresolved type markers
|
|
162
|
+
const cleanedProperties = resolveUnresolvedTypes(properties, checker, sourceFile);
|
|
163
|
+
|
|
164
|
+
events.push({
|
|
165
|
+
eventName,
|
|
166
|
+
source,
|
|
167
|
+
properties: cleanedProperties,
|
|
168
|
+
filePath,
|
|
169
|
+
line,
|
|
170
|
+
functionName
|
|
171
|
+
});
|
|
172
|
+
}
|
|
50
173
|
} catch (propertyError) {
|
|
51
174
|
console.error(`Error extracting properties in ${filePath} at line ${line}`);
|
|
52
175
|
}
|