dishub-analytics.sdk 1.1.0

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/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # dishub-analytics.sdk
2
+
3
+ The official tracking SDK for Dishub Analytics. Lightweight, TypeScript-first, and optimized for real-time performance.
4
+
5
+ ## 🚀 Features
6
+
7
+ - **Real-time Tracking**: Sub-second event reporting.
8
+ - **Proactive Disconnect**: Automatic session cleanup using `keepalive` beacons.
9
+ - **Smart Heartbeats**: Monitors tab visibility to ensure accuracy.
10
+ - **Identity Support**: Link sessions to authenticated users.
11
+ - **React Ready**: Includes a built-in `AnalyticsProvider` and `useAnalytics` hook.
12
+
13
+ ## 📦 Installation
14
+
15
+ ```bash
16
+ npm install dishub-analytics.sdk
17
+ ```
18
+
19
+ ## 🛠️ Usage
20
+
21
+ ### Vanilla JavaScript
22
+
23
+ ```javascript
24
+ import { AnalyticsClient } from 'dishub-analytics.sdk';
25
+
26
+ const analytics = new AnalyticsClient({
27
+ apiKey: 'YOUR_API_KEY',
28
+ endpoint: 'https://dishubanalitics-production.up.railway.app/api/v1',
29
+ trackClicks: true,
30
+ trackHeartbeat: true
31
+ });
32
+
33
+ // Initialize session
34
+ await analytics.init();
35
+
36
+ // Track custom events
37
+ analytics.trackEvent('purchase_completed', 'ecommerce', { amount: 99.99 });
38
+
39
+ // Identify user
40
+ analytics.identify('user_123', 'John Doe');
41
+ ```
42
+
43
+ ### React / Next.js
44
+
45
+ Wrap your application with the `AnalyticsProvider`:
46
+
47
+ ```tsx
48
+ import { AnalyticsProvider } from 'dishub-analytics.sdk';
49
+
50
+ function App() {
51
+ return (
52
+ <AnalyticsProvider
53
+ apiKey="YOUR_API_KEY"
54
+ endpoint="https://analytics-api.example.com"
55
+ config={{ trackClicks: true }}
56
+ >
57
+ <MyRoutes />
58
+ </AnalyticsProvider>
59
+ );
60
+ }
61
+ ```
62
+
63
+ Use the hook in your components:
64
+
65
+ ```tsx
66
+ import { useAnalytics } from 'dishub-analytics.sdk';
67
+
68
+ function CheckoutButton() {
69
+ const { trackEvent, identify } = useAnalytics();
70
+
71
+ const handleCheckout = async () => {
72
+ trackEvent('checkout_clicked');
73
+ // ...
74
+ };
75
+
76
+ return <button onClick={handleCheckout}>Checkout</button>;
77
+ }
78
+ ```
79
+
80
+ ## 📄 License
81
+
82
+ MIT © [Dishub](https://dishub.city)
@@ -0,0 +1,30 @@
1
+ import { AnalyticsConfig } from './types';
2
+ export declare class AnalyticsClient {
3
+ private config;
4
+ private visitorId;
5
+ private sessionId;
6
+ private heartbeatInterval;
7
+ constructor(config: AnalyticsConfig);
8
+ getVisitorId(): string | null;
9
+ getSessionId(): string | null;
10
+ init(): Promise<void>;
11
+ identify(userId: string, name?: string): Promise<void>;
12
+ private request;
13
+ private trackSession;
14
+ trackEvent(name: string, category?: string, value?: string, metadata?: Record<string, any>): Promise<void>;
15
+ trackPageView(path?: string, title?: string): Promise<void>;
16
+ private startHeartbeat;
17
+ private setupDisconnectListeners;
18
+ disconnect(): Promise<void>;
19
+ private setupClickTracking;
20
+ trackInteraction(type: string, element: string, x?: number, y?: number, metadata?: Record<string, any>): Promise<void>;
21
+ private setupInteractionTracking;
22
+ private getSelector;
23
+ private getDeviceType;
24
+ private getBrowser;
25
+ private getOS;
26
+ private getSegment;
27
+ private getUtmParam;
28
+ private generateId;
29
+ destroy(): void;
30
+ }
@@ -0,0 +1,72 @@
1
+ import React from 'react';
2
+
3
+ interface AnalyticsConfig {
4
+ apiKey: string;
5
+ endpoint: string;
6
+ trackPageViews?: boolean;
7
+ trackClicks?: boolean;
8
+ trackHeartbeat?: boolean;
9
+ heartbeatInterval?: number;
10
+ excludePaths?: string[];
11
+ segment?: string;
12
+ externalUserId?: string;
13
+ externalUserName?: string;
14
+ utmSource?: string;
15
+ utmMedium?: string;
16
+ utmCampaign?: string;
17
+ }
18
+ interface TrackEventOptions {
19
+ name: string;
20
+ category?: string;
21
+ value?: string;
22
+ metadata?: Record<string, unknown>;
23
+ }
24
+ interface AnalyticsContext {
25
+ trackEvent: (name: string, category?: string, value?: string, metadata?: Record<string, unknown>) => Promise<void>;
26
+ trackInteraction: (type: string, element: string, x?: number, y?: number, metadata?: Record<string, unknown>) => Promise<void>;
27
+ identify: (userId: string, name?: string) => Promise<void>;
28
+ sessionId: string | null;
29
+ visitorId: string | null;
30
+ }
31
+
32
+ declare class AnalyticsClient {
33
+ private config;
34
+ private visitorId;
35
+ private sessionId;
36
+ private heartbeatInterval;
37
+ constructor(config: AnalyticsConfig);
38
+ getVisitorId(): string | null;
39
+ getSessionId(): string | null;
40
+ init(): Promise<void>;
41
+ identify(userId: string, name?: string): Promise<void>;
42
+ private request;
43
+ private trackSession;
44
+ trackEvent(name: string, category?: string, value?: string, metadata?: Record<string, any>): Promise<void>;
45
+ trackPageView(path?: string, title?: string): Promise<void>;
46
+ private startHeartbeat;
47
+ private setupDisconnectListeners;
48
+ disconnect(): Promise<void>;
49
+ private setupClickTracking;
50
+ trackInteraction(type: string, element: string, x?: number, y?: number, metadata?: Record<string, any>): Promise<void>;
51
+ private setupInteractionTracking;
52
+ private getSelector;
53
+ private getDeviceType;
54
+ private getBrowser;
55
+ private getOS;
56
+ private getSegment;
57
+ private getUtmParam;
58
+ private generateId;
59
+ destroy(): void;
60
+ }
61
+
62
+ interface AnalyticsProviderProps {
63
+ children: React.ReactNode;
64
+ apiKey: string;
65
+ endpoint: string;
66
+ config?: Partial<AnalyticsConfig>;
67
+ }
68
+ declare function AnalyticsProvider({ children, apiKey, endpoint, config }: AnalyticsProviderProps): React.JSX.Element;
69
+ declare function useAnalytics(): AnalyticsContext;
70
+
71
+ export { AnalyticsClient, AnalyticsProvider, useAnalytics };
72
+ export type { AnalyticsConfig, AnalyticsContext, AnalyticsProviderProps, TrackEventOptions };