Skip to content

Commit 365e421

Browse files
author
Archith
committed
Fix behavior tracking initialization timing
- Move behavior tracking init to after ACE editor is ready - Use custom events to bridge ACE editor events to tracking module - Track paste events from ACE editor properly - Track typing patterns from ACE editor change events - Add debug logging throughout The issue was that behavior tracking was initialized before the editor existed. Now it initializes after the editor is created and hooks into ACE editor events.
1 parent e296329 commit 365e421

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

scripts/app.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@
8181
// Remove the security check message - just proceed
8282
}
8383

84-
// Initialize behavior tracking for candidates only
85-
if (window.initBehaviorTracking) {
86-
console.log('Initializing behavior tracking for candidate:', name);
87-
window.initBehaviorTracking(sessionCode, name, 'candidate');
88-
} else {
89-
console.warn('Behavior tracking module not loaded');
90-
}
84+
// Store behavior tracking info to initialize after editor loads
85+
window.pendingBehaviorTracking = {
86+
sessionCode: sessionCode,
87+
name: name,
88+
type: 'candidate'
89+
};
9190

9291
Auth.joinAsCandidate(name);
9392
window.location.hash = sessionCode;

scripts/behavior-tracking.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,20 @@
102102
function trackPasteEvents() {
103103
console.log('Setting up paste tracking...');
104104

105-
// Track paste in CodeMirror
106-
if (window.codeMirror) {
107-
console.log('CodeMirror found, attaching paste listener');
108-
window.codeMirror.on('paste', function(cm, event) {
109-
console.log('CodeMirror paste event detected');
110-
if (!isTracking) return;
111-
handlePasteEvent(event);
112-
});
113-
} else {
114-
console.log('CodeMirror not found, will track global paste only');
115-
}
105+
// Listen for ACE editor paste events (sent from firepad.js)
106+
document.addEventListener('editorPaste', function(e) {
107+
console.log('Editor paste event received:', e.detail);
108+
if (!isTracking) return;
109+
110+
const fakeEvent = {
111+
clipboardData: {
112+
getData: function() { return e.detail.text; }
113+
}
114+
};
115+
handlePasteEvent(fakeEvent);
116+
});
116117

117-
// Also track global paste
118+
// Also track global paste (for paste outside editor)
118119
document.addEventListener('paste', function(e) {
119120
console.log('Global paste event detected, tracking:', isTracking);
120121
if (!isTracking) return;
@@ -198,24 +199,29 @@
198199

199200
// 3. Typing Pattern Analysis
200201
function trackTypingPatterns() {
201-
if (!window.codeMirror) return;
202+
console.log('Setting up typing pattern tracking...');
202203

203204
let keystrokeBuffer = [];
204205
let lastEventTime = Date.now();
205206

206-
window.codeMirror.on('change', function(cm, change) {
207-
if (!isTracking || change.origin === 'setValue') return;
207+
// Listen for ACE editor change events (sent from firepad.js)
208+
document.addEventListener('editorChange', function(e) {
209+
if (!isTracking) return;
210+
211+
const change = e.detail;
212+
if (change.action === 'setValue') return; // Ignore programmatic changes
208213

209214
const now = Date.now();
210215
const timeDiff = now - lastEventTime;
211216
lastEventTime = now;
212217

213218
// Track keystroke timing
214-
if (change.origin === 'input' || change.origin === '+input') {
219+
if (change.action === 'insert' || change.action === 'insertText') {
215220
typingMetrics.totalKeystrokes++;
216221

217222
// Calculate WPM from recent keystrokes
218-
keystrokeBuffer.push({ time: now, chars: change.text.join('').length });
223+
const textLength = change.text ? change.text.length : 0;
224+
keystrokeBuffer.push({ time: now, chars: textLength });
219225

220226
// Keep only last 5 seconds of keystrokes
221227
keystrokeBuffer = keystrokeBuffer.filter(k => now - k.time < 5000);
@@ -240,9 +246,10 @@
240246
}
241247

242248
// Detect large insertions (potential paste via typing)
243-
if (change.text.join('').length > 50 && timeDiff < 100) {
249+
const insertedText = change.text || '';
250+
if (insertedText.length > 50 && timeDiff < 100) {
244251
notifyInterviewer('instant_code_appearance', {
245-
size: change.text.join('').length,
252+
size: insertedText.length,
246253
timeGap: timeDiff,
247254
severity: 'high'
248255
});

scripts/firepad.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@
155155
editor.on('changeSelection', updateCursorPosition);
156156

157157
console.log('Editor initialized - ReadOnly:', editor.getReadOnly());
158+
159+
// Initialize behavior tracking if pending (for candidates)
160+
if (window.pendingBehaviorTracking && window.initBehaviorTracking) {
161+
const { sessionCode, name, type } = window.pendingBehaviorTracking;
162+
console.log('Initializing delayed behavior tracking for:', name, type);
163+
164+
// Set up ACE editor paste tracking
165+
editor.on('paste', function(e) {
166+
console.log('ACE Editor paste event detected');
167+
const pasteEvent = new CustomEvent('editorPaste', {
168+
detail: { text: e.text || '' }
169+
});
170+
document.dispatchEvent(pasteEvent);
171+
});
172+
173+
// Track editor changes for typing patterns
174+
editor.on('change', function(e) {
175+
const changeEvent = new CustomEvent('editorChange', {
176+
detail: {
177+
action: e.action,
178+
lines: e.lines,
179+
text: e.lines ? e.lines.join('\n') : ''
180+
}
181+
});
182+
document.dispatchEvent(changeEvent);
183+
});
184+
185+
// Initialize behavior tracking
186+
window.initBehaviorTracking(sessionCode, name, type);
187+
188+
// Clear pending flag
189+
window.pendingBehaviorTracking = null;
190+
}
158191
}
159192

160193
// Initialize Firebase and Firepad

0 commit comments

Comments
 (0)