File size: 1,754 Bytes
ce8f04a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * Posthog event tracking utility.

 * Wywoływany z kluczowych akcji użytkownika.

 * Safe no-op when VITE_POSTHOG_KEY is unset (posthog.init never called).

 */
import posthog from 'posthog-js';

function ph() {
  try {
    // After init, posthog.__loaded is true; without DSN/key, capture is a no-op
    return posthog;
  } catch {
    return null;
  }
}

export const analytics = {
  /** Identyfikuj użytkownika po zalogowaniu */
  identify(userId: string, props?: Record<string, unknown>) {
    ph()?.identify(userId, props);
  },

  /** Użytkownik kliknął "Nowy Projekt" */
  projectCreated(projectId: string, name: string) {
    ph()?.capture('project_created', { project_id: projectId, project_name: name });
  },

  /** Sekcja wygenerowana przez AI */
  sectionGenerated(projectId: string, sectionKey: string, tierUsed: string) {
    ph()?.capture('section_generated', { project_id: projectId, section_key: sectionKey, tier: tierUsed });
  },

  /** Upload dokumentu PDF */
  documentUploaded(projectId: string, fileName: string) {
    ph()?.capture('document_uploaded', { project_id: projectId, file_name: fileName });
  },

  /** Klik "Kup Pro" — przed redirect do Stripe */
  checkoutStarted(plan: string) {
    ph()?.capture('checkout_started', { plan });
  },

  /** Powrót z Stripe — sukces */
  checkoutCompleted(plan: string) {
    ph()?.capture('checkout_completed', { plan });
  },

  /** Audyt wniosku */
  auditRun(projectId: string, score: number) {
    ph()?.capture('audit_run', { project_id: projectId, compliance_score: score });
  },

  /** Export DOCX */
  exportDocx(projectId: string) {
    ph()?.capture('export_docx', { project_id: projectId });
  },
};