Skip to content

Commit 927f93d

Browse files
committed
feat(analytics): Enhance event registration tracking and analytics
- Add trackEventRegistration method to AnalyticsService for event registration tracking - Implement fallback mechanism for incrementing company analytics when RPC fails - Update master registrations service to simplify database query and reduce schema complexity - Add error handling for analytics tracking in event registration route - Improve robustness of company analytics data insertion and update process Ensures comprehensive tracking of event registrations and provides more flexible analytics data management.
1 parent 00b2a17 commit 927f93d

File tree

3 files changed

+91
-24
lines changed

3 files changed

+91
-24
lines changed

app/api/events/[slug]/register/route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ export async function POST(
111111
})
112112
.eq('id', event.id);
113113

114+
// Track registration in analytics
115+
try {
116+
const { AnalyticsService } = await import('@/lib/services/analytics-service');
117+
await AnalyticsService.trackEventRegistration(event.id);
118+
} catch (error) {
119+
console.error('Error tracking registration in analytics:', error);
120+
// Don't fail the registration if analytics tracking fails
121+
}
122+
114123
return NextResponse.json({
115124
success: true,
116125
data: registration,

lib/services/analytics-service.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ export class AnalyticsService {
9090
}
9191
}
9292

93+
/**
94+
* Track a registration for an event
95+
*/
96+
static async trackEventRegistration(eventId: number): Promise<void> {
97+
const supabase = await createClient()
98+
99+
const { data: event, error: eventError } = await supabase
100+
.from('events')
101+
.select('id, company_id')
102+
.eq('id', eventId)
103+
.single()
104+
105+
if (eventError || !event) {
106+
throw new Error('Event not found')
107+
}
108+
109+
// Update company analytics if event has a company
110+
if (event.company_id) {
111+
await this.incrementCompanyAnalytics(
112+
event.company_id,
113+
'total_registrations',
114+
1
115+
)
116+
}
117+
}
118+
93119
/**
94120
* Track a view for a hackathon
95121
*/
@@ -165,12 +191,64 @@ export class AnalyticsService {
165191
const supabase = await createClient()
166192
const today = new Date().toISOString().split('T')[0]
167193

168-
await supabase.rpc('increment_company_analytics', {
194+
// Try RPC first, fallback to manual upsert if it fails
195+
const { error: rpcError } = await supabase.rpc('increment_company_analytics', {
169196
p_company_id: companyId,
170197
p_date: today,
171198
p_field: field,
172199
p_increment: increment,
173200
})
201+
202+
if (rpcError) {
203+
// Fallback: Get existing record or create new one
204+
const { data: existing, error: fetchError } = await supabase
205+
.from('company_analytics')
206+
.select('*')
207+
.eq('company_id', companyId)
208+
.eq('date', today)
209+
.single()
210+
211+
if (fetchError && fetchError.code !== 'PGRST116') {
212+
console.error('Error fetching company analytics:', fetchError)
213+
return
214+
}
215+
216+
if (existing) {
217+
// Update existing record
218+
const currentValue = (existing[field] as number) || 0
219+
const { error: updateError } = await supabase
220+
.from('company_analytics')
221+
.update({ [field]: currentValue + increment })
222+
.eq('company_id', companyId)
223+
.eq('date', today)
224+
225+
if (updateError) {
226+
console.error('Error updating company analytics:', updateError)
227+
}
228+
} else {
229+
// Create new record
230+
const { error: insertError } = await supabase
231+
.from('company_analytics')
232+
.insert({
233+
company_id: companyId,
234+
date: today,
235+
events_created: 0,
236+
events_published: 0,
237+
hackathons_created: 0,
238+
hackathons_published: 0,
239+
total_views: 0,
240+
total_clicks: 0,
241+
total_registrations: 0,
242+
total_participants: 0,
243+
revenue_generated: 0,
244+
[field]: increment,
245+
})
246+
247+
if (insertError) {
248+
console.error('Error inserting company analytics:', insertError)
249+
}
250+
}
251+
}
174252
}
175253

176254
/**

lib/services/master-registrations.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class MasterRegistrationsService {
6464

6565
// Register for any activity type with optimized query
6666
async register(request: RegistrationRequest, userId: string): Promise<MasterRegistration> {
67-
// Use a single transaction to insert registration and get user profile data
67+
// Insert registration without profile join to avoid schema cache issues
6868
const { data, error } = await this.supabase
6969
.from('master_registrations')
7070
.insert({
@@ -86,17 +86,7 @@ class MasterRegistrationsService {
8686
experience_level: request.experience_level,
8787
metadata: request.metadata || {}
8888
})
89-
.select(`
90-
*,
91-
profiles!inner(
92-
first_name,
93-
last_name,
94-
email,
95-
phone,
96-
company,
97-
current_position
98-
)
99-
`)
89+
.select('*')
10090
.single();
10191

10292
if (error) {
@@ -110,17 +100,7 @@ class MasterRegistrationsService {
110100
async getUserRegistrations(filters: RegistrationFilters): Promise<MasterRegistration[]> {
111101
let query = this.supabase
112102
.from('master_registrations')
113-
.select(`
114-
*,
115-
profiles!inner(
116-
first_name,
117-
last_name,
118-
email,
119-
phone,
120-
company,
121-
current_position
122-
)
123-
`)
103+
.select('*')
124104
.order('created_at', { ascending: false });
125105

126106
if (filters.user_id) {

0 commit comments

Comments
 (0)