๐ Built using [Crystal](https://github.com/stravu/crystal) Co-Authored-By: Crystal <crystal@stravu.com>
8.5 KiB
Analytics UI Events Integration Guide
This document describes how to integrate UI interaction event tracking into Crystal's frontend components.
Overview
The analytics system has been implemented with:
- Backend IPC handlers in
/main/src/ipc/analytics.ts - AnalyticsManager service enhancements to support string arrays
- Frontend service utility in
/frontend/src/services/analyticsService.ts - Preload script exposure for secure IPC communication
Backend Implementation
IPC Handlers
- File:
/main/src/ipc/analytics.ts - Registered: In
/main/src/ipc/index.ts - Service Integration: AnalyticsManager added to AppServices in
/main/src/ipc/types.ts
IPC Endpoints
analytics:track-ui-event- Track UI interaction eventsanalytics:categorize-result-count- Helper to categorize search result countsanalytics:hash-session-id- Helper to hash session IDs for privacy
Frontend Integration Points
Using the AnalyticsService
Import the service:
import { AnalyticsService } from '@/services/analyticsService';
Event 1: view_switched
Track when users switch between tabs (Output, Diff, Logs, Editor, etc.)
Where to implement:
- Component:
/frontend/src/components/SessionView.tsxor/frontend/src/hooks/useSessionView.ts - Trigger: When user clicks on a tab to switch views
- Implementation example:
const handleViewSwitch = async (fromView: string, toView: string, sessionId?: string) => {
// Hash the session ID for privacy
const sessionIdHash = sessionId
? await AnalyticsService.hashSessionId(sessionId)
: undefined;
await AnalyticsService.trackViewSwitched({
from_view: fromView,
to_view: toView,
session_id_hash: sessionIdHash,
});
};
View names to use:
output- Main output viewdiff- Git diff viewlogs- Logs/terminal vieweditor- Editor viewdashboard- Dashboard view
Event 2: help_dialog_opened
Track when users open the help dialog
Where to implement:
- Component:
/frontend/src/components/Help.tsx(or wherever the help dialog trigger is) - Trigger: When the help button/icon is clicked
- Implementation example:
const handleHelpOpen = async (location: string) => {
await AnalyticsService.trackHelpDialogOpened({
from_location: location,
});
// Then open the help dialog
setShowHelp(true);
};
Location values to use:
sidebar- From sidebar help buttontoolbar- From toolbar help buttonsettings- From settings pagekeyboard_shortcut- From keyboard shortcut
Event 3: settings_opened
Track when users open settings
Where to implement:
- Component:
/frontend/src/components/Settings.tsx(or wherever settings trigger is) - Trigger: When settings dialog/page is opened
- Implementation example:
const handleSettingsOpen = async (location: string) => {
await AnalyticsService.trackSettingsOpened({
from_location: location,
});
// Then open settings
setShowSettings(true);
};
Location values to use:
sidebar- From sidebar settings buttonmenu- From menu barkeyboard_shortcut- From keyboard shortcut
Event 4: settings_saved
Track when users save settings changes
Where to implement:
- Component:
/frontend/src/components/Settings.tsx - Trigger: When user clicks "Save" or settings are persisted
- Implementation example:
const handleSettingsSave = async (category: string, changedKeys: string[]) => {
await AnalyticsService.trackSettingsSaved({
category: category,
setting_keys: changedKeys,
});
// Then save the settings
await window.electronAPI.config.update(updatedSettings);
};
Category values to use:
general- General settingsnotifications- Notification settingsproject- Project-specific settingsappearance- UI/appearance settingsadvanced- Advanced settings
Setting keys examples:
verbose_loggingapi_keynotification_enablednotification_soundcustom_promptmain_branch
Event 5: sidebar_toggled
Track when users show/hide the sidebar
Where to implement:
- Component: Main layout component (likely in
/frontend/src/App.tsxor similar) - Trigger: When sidebar visibility changes
- Implementation example:
const handleSidebarToggle = async (isVisible: boolean) => {
await AnalyticsService.trackSidebarToggled({
is_visible: isVisible,
});
// Then toggle sidebar
setSidebarVisible(isVisible);
};
Event 6: search_used
Track when users use search functionality
Where to implement:
- Component: Search components (likely in
/frontend/src/components/DraggableProjectTreeView.tsxor search bar component) - Trigger: When search is executed (on Enter or search button click)
- Implementation example:
const handleSearch = async (searchTerm: string, searchType: string, resultCount: number) => {
// Categorize the result count for privacy (don't track exact numbers)
const resultCategory = await AnalyticsService.categorizeResultCount(resultCount);
await AnalyticsService.trackSearchUsed({
search_type: searchType,
result_count_category: resultCategory,
});
// Note: Do NOT track the actual search term for privacy
};
Search type values to use:
sessions- Searching sessionsprompts- Searching promptsfiles- Searching filesprojects- Searching projects
Result count categories (automatically categorized by backend):
0-0- No results1-5- Few results6-10- Some results11-25- Many results26-50- Lots of results51-100- Very many results101+- Tons of results
Privacy Considerations
Do NOT Track:
- โ Actual search terms
- โ File names or paths
- โ Prompt content
- โ User identifiable information
- โ Exact counts (use categories)
- โ Raw session IDs (always hash them)
DO Track:
- โ Event types (which button was clicked)
- โ Categories and types
- โ Hashed/anonymized identifiers
- โ Boolean states (visible/hidden, enabled/disabled)
- โ Categorized counts (0-5, 6-10, etc.)
Testing
To test analytics integration:
- Enable analytics in settings
- Enable verbose logging to see analytics events in the console
- Perform actions that should trigger events
- Check logs for messages like:
[Analytics] Tracked event: view_switched { from_view: 'output', to_view: 'diff', ... }
Type Safety
The AnalyticsService class provides type-safe methods for all events. TypeScript will enforce:
- Correct event names
- Required properties for each event type
- Proper value types for each property
If you try to pass invalid data, you'll get a TypeScript error at compile time.
Error Handling
All analytics methods:
- Are non-blocking (use
async/awaitbut don't wait for responses) - Fail silently (errors are logged but don't break functionality)
- Check if analytics is enabled before tracking
- Handle cases where the electronAPI is not available
Example: Complete Integration
Here's a complete example of integrating view switching in a component:
import React, { useState } from 'react';
import { AnalyticsService } from '@/services/analyticsService';
export function SessionView({ sessionId }: { sessionId: string }) {
const [currentView, setCurrentView] = useState<'output' | 'diff' | 'logs' | 'editor'>('output');
const switchView = async (newView: typeof currentView) => {
// Track the view switch
const sessionIdHash = await AnalyticsService.hashSessionId(sessionId);
await AnalyticsService.trackViewSwitched({
from_view: currentView,
to_view: newView,
session_id_hash: sessionIdHash,
});
// Update the view
setCurrentView(newView);
};
return (
<div>
<div className="tabs">
<button onClick={() => switchView('output')}>Output</button>
<button onClick={() => switchView('diff')}>Diff</button>
<button onClick={() => switchView('logs')}>Logs</button>
<button onClick={() => switchView('editor')}>Editor</button>
</div>
{/* View content here */}
</div>
);
}
Summary
All backend infrastructure is complete. Frontend developers should:
- Import
AnalyticsServicefrom/frontend/src/services/analyticsService.ts - Call the appropriate tracking method when user actions occur
- Follow the privacy guidelines (no PII, use hashing/categorization)
- Test with verbose logging enabled to verify events are tracked
The tracking is opt-in (users can disable analytics in settings) and respects user privacy by design.