@umituz/web-traffic 1.0.8 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-traffic",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Web analytics tracking library. Event tracking, pageviews, sessions, device info, and UTM parameter support.",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -74,23 +74,23 @@ export class TrackingCommandService {
74
74
  const sessionIdVo = new SessionId(sessionId);
75
75
  const pageviewId = EventId.generate();
76
76
 
77
+ // Get session first to get siteId
78
+ let session = await this.sessionRepo.findById(sessionIdVo);
79
+ if (!session) {
80
+ throw new Error('Session not found');
81
+ }
82
+
77
83
  const utmParameters = utmParams ? new UTMParameters(utmParams) : null;
78
84
 
79
85
  const pageview = new Pageview({
80
86
  id: pageviewId,
81
87
  sessionId: sessionIdVo,
82
- siteId: new SessionId(data.sessionId).getSiteId(),
88
+ siteId: session.siteId,
83
89
  path,
84
90
  referrer,
85
91
  utmParameters,
86
92
  });
87
93
 
88
- // Get or create session
89
- let session = await this.sessionRepo.findById(sessionIdVo);
90
- if (!session) {
91
- throw new Error('Session not found');
92
- }
93
-
94
94
  session.addPageview(pageview);
95
95
  await this.pageviewRepo.save(pageview);
96
96
  await this.sessionRepo.save(session);
@@ -103,7 +103,8 @@ class WebTrafficService {
103
103
  if (!this.sessionRepo) return;
104
104
 
105
105
  const deviceId = this.getOrCreateDeviceId();
106
- const existingSession = await this.sessionRepo.findActive(deviceId);
106
+ const SESSION_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
107
+ const existingSession = await this.sessionRepo.findActive(deviceId, SESSION_TIMEOUT_MS);
107
108
 
108
109
  if (existingSession) {
109
110
  this.currentSession = existingSession;
@@ -3,8 +3,7 @@
3
3
  * @description React context provider for web-traffic tracking
4
4
  */
5
5
 
6
- import type { createContext } from 'react';
7
- import { createContext, useContext, useEffect, useMemo, type ReactNode } from 'react';
6
+ import React, { createContext, useContext, useEffect, useMemo, type ReactNode } from 'react';
8
7
  import { webTrafficService, type WebTrafficConfig } from '../infrastructure/tracking/web-traffic.service';
9
8
  import type { WebTrafficContextValue, TrackingCommandResult } from './hooks';
10
9
 
@@ -33,8 +32,9 @@ export function WebTrafficProvider({ children, config }: WebTrafficProviderProps
33
32
  return webTrafficService.trackPageView(path);
34
33
  },
35
34
  isInitialized: webTrafficService.isInitialized(),
35
+ config,
36
36
  }),
37
- []
37
+ [config]
38
38
  );
39
39
 
40
40
  return <TrackingContext.Provider value={value}>{children}</TrackingContext.Provider>;
@@ -16,10 +16,13 @@ export interface TrackingCommandResult {
16
16
  error?: string;
17
17
  }
18
18
 
19
+ import type { WebTrafficConfig } from '../infrastructure/tracking/web-traffic.service';
20
+
19
21
  export interface WebTrafficContextValue {
20
22
  readonly trackEvent: (name: string, properties?: Record<string, unknown>) => Promise<TrackingCommandResult>;
21
23
  readonly trackPageView: (path?: string) => Promise<TrackingCommandResult>;
22
24
  readonly isInitialized: boolean;
25
+ readonly config: WebTrafficConfig;
23
26
  }
24
27
 
25
28
  export function useWebTraffic(): WebTrafficContextValue {
@@ -35,6 +38,7 @@ export function useWebTraffic(): WebTrafficContextValue {
35
38
  return webTrafficService.trackPageView(path);
36
39
  }, []),
37
40
  isInitialized: webTrafficService.isInitialized(),
41
+ config: { apiKey: '' }, // Fallback - context should always be used
38
42
  };
39
43
  }
40
44
 
@@ -45,6 +49,7 @@ export function useAnalytics(query: AnalyticsQuery) {
45
49
  const [data, setData] = useState<AnalyticsData | null>(null);
46
50
  const [loading, setLoading] = useState(true);
47
51
  const [error, setError] = useState<Error | null>(null);
52
+ const { config } = useWebTraffic();
48
53
 
49
54
  useEffect(() => {
50
55
  let cancelled = false;
@@ -53,10 +58,9 @@ export function useAnalytics(query: AnalyticsQuery) {
53
58
  setLoading(true);
54
59
  setError(null);
55
60
 
56
- // Note: You'll need to initialize this with proper config
57
61
  const repo = new HTTPAnalyticsRepository({
58
- apiUrl: 'https://analytics.umituz.com',
59
- apiKey: 'your-api-key', // This should come from config
62
+ apiUrl: config.apiUrl ?? 'https://analytics.umituz.com',
63
+ apiKey: config.apiKey,
60
64
  });
61
65
 
62
66
  const result = await repo.getAnalytics(query);
@@ -72,7 +76,7 @@ export function useAnalytics(query: AnalyticsQuery) {
72
76
  return () => {
73
77
  cancelled = true;
74
78
  };
75
- }, [query]);
79
+ }, [query, config]);
76
80
 
77
81
  return { data, loading, error };
78
82
  }