@dashgram/javascript 1.0.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/LICENSE +26 -0
- package/README.md +257 -0
- package/dist/core/config.d.ts +13 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +50 -0
- package/dist/core/context.d.ts +12 -0
- package/dist/core/context.d.ts.map +1 -0
- package/dist/core/context.js +23 -0
- package/dist/core/event-queue.d.ts +13 -0
- package/dist/core/event-queue.d.ts.map +1 -0
- package/dist/core/event-queue.js +29 -0
- package/dist/core/session.d.ts +13 -0
- package/dist/core/session.d.ts.map +1 -0
- package/dist/core/session.js +63 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +53 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +148 -0
- package/dist/trackers/base-tracker.d.ts +17 -0
- package/dist/trackers/base-tracker.d.ts.map +1 -0
- package/dist/trackers/base-tracker.js +41 -0
- package/dist/trackers/core-tracker.d.ts +14 -0
- package/dist/trackers/core-tracker.d.ts.map +1 -0
- package/dist/trackers/core-tracker.js +64 -0
- package/dist/trackers/deep-tracker.d.ts +19 -0
- package/dist/trackers/deep-tracker.d.ts.map +1 -0
- package/dist/trackers/deep-tracker.js +241 -0
- package/dist/trackers/interaction-tracker.d.ts +17 -0
- package/dist/trackers/interaction-tracker.d.ts.map +1 -0
- package/dist/trackers/interaction-tracker.js +145 -0
- package/dist/transport/batch-processor.d.ts +19 -0
- package/dist/transport/batch-processor.d.ts.map +1 -0
- package/dist/transport/batch-processor.js +75 -0
- package/dist/transport/transport.d.ts +16 -0
- package/dist/transport/transport.d.ts.map +1 -0
- package/dist/transport/transport.js +140 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/device.d.ts +11 -0
- package/dist/utils/device.d.ts.map +1 -0
- package/dist/utils/device.js +72 -0
- package/dist/utils/helpers.d.ts +9 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +74 -0
- package/dist/utils/telegram.d.ts +10 -0
- package/dist/utils/telegram.d.ts.map +1 -0
- package/dist/utils/telegram.js +55 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dashgram
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# Dashgram JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Analytics SDK for Telegram Mini Apps.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @dashgram/javascript
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import DashgramMini from "@dashgram/javascript"
|
|
15
|
+
|
|
16
|
+
// Initialize SDK
|
|
17
|
+
DashgramMini.init({
|
|
18
|
+
projectId: "your-project-id",
|
|
19
|
+
apiKey: "your-api-key",
|
|
20
|
+
trackLevel: 2, // 1, 2, or 3
|
|
21
|
+
debug: true // for development
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// Track custom events
|
|
25
|
+
DashgramMini.track("button_clicked", {
|
|
26
|
+
button_name: "subscribe",
|
|
27
|
+
screen: "home"
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// Identify user
|
|
31
|
+
DashgramMini.identify("user-123", {
|
|
32
|
+
plan: "premium",
|
|
33
|
+
email: "user@example.com"
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Track Levels
|
|
38
|
+
|
|
39
|
+
### Level 1 — Core (minimal, default)
|
|
40
|
+
|
|
41
|
+
Automatically tracks:
|
|
42
|
+
|
|
43
|
+
- `app_open` — App opened
|
|
44
|
+
- `app_close` — App closed
|
|
45
|
+
- `session_start` — Session started
|
|
46
|
+
- `session_end` — Session ended
|
|
47
|
+
- Device/platform information
|
|
48
|
+
- Telegram user ID (if available)
|
|
49
|
+
|
|
50
|
+
### Level 2 — Interaction
|
|
51
|
+
|
|
52
|
+
Includes Level 1 + automatically tracks:
|
|
53
|
+
|
|
54
|
+
- `screen_view` — Screen views (URL/history changes)
|
|
55
|
+
- `button_click` — Button clicks
|
|
56
|
+
- `link_click` — Link clicks
|
|
57
|
+
- `form_submit` — Form submissions
|
|
58
|
+
- `input_focus` — Input field focus
|
|
59
|
+
- `js_error` — JavaScript errors
|
|
60
|
+
- `unhandled_rejection` — Unhandled Promise rejections
|
|
61
|
+
|
|
62
|
+
### Level 3 — Deep / Product
|
|
63
|
+
|
|
64
|
+
Includes Level 1 + 2 + automatically tracks:
|
|
65
|
+
|
|
66
|
+
- `scroll_depth` — Scroll depth (25%, 50%, 75%, 100%)
|
|
67
|
+
- `element_visible` — Element visibility (IntersectionObserver)
|
|
68
|
+
- `rage_click` — Rage clicks (5+ clicks in 2 seconds)
|
|
69
|
+
- `long_task` — Long tasks (Performance API, >50ms)
|
|
70
|
+
- `web_vital_lcp` — Largest Contentful Paint
|
|
71
|
+
- `web_vital_fid` — First Input Delay
|
|
72
|
+
- `web_vital_cls` — Cumulative Layout Shift
|
|
73
|
+
- Telegram WebApp events:
|
|
74
|
+
- `telegram_theme_changed`
|
|
75
|
+
- `telegram_viewport_changed`
|
|
76
|
+
- `telegram_back_button_clicked`
|
|
77
|
+
- `telegram_main_button_clicked`
|
|
78
|
+
|
|
79
|
+
## API
|
|
80
|
+
|
|
81
|
+
### `DashgramMini.init(config)`
|
|
82
|
+
|
|
83
|
+
Initializes the SDK.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
interface DashgramConfig {
|
|
87
|
+
projectId: string // Project ID from Dashgram dashboard
|
|
88
|
+
apiKey: string // API key for authentication
|
|
89
|
+
trackLevel?: 1 | 2 | 3 // Track level (default: 1)
|
|
90
|
+
apiUrl?: string // API endpoint URL (optional)
|
|
91
|
+
batchSize?: number // Batch size (default: 10)
|
|
92
|
+
flushInterval?: number // Flush interval in ms (default: 5000)
|
|
93
|
+
debug?: boolean // Debug mode (default: false)
|
|
94
|
+
disabled?: boolean // Disable tracking (default: false)
|
|
95
|
+
onError?: (error: DashgramError) => void // Optional error handler
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `DashgramMini.track(event, properties?)`
|
|
100
|
+
|
|
101
|
+
Tracks a custom event.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
DashgramMini.track("purchase_completed", {
|
|
105
|
+
product_id: "123",
|
|
106
|
+
amount: 99.99,
|
|
107
|
+
currency: "USD"
|
|
108
|
+
})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `DashgramMini.identify(userId, traits?)`
|
|
112
|
+
|
|
113
|
+
Identifies a user.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
DashgramMini.identify("user-123", {
|
|
117
|
+
email: "user@example.com",
|
|
118
|
+
plan: "premium",
|
|
119
|
+
signup_date: "2024-01-01"
|
|
120
|
+
})
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### `DashgramMini.setTrackLevel(level)`
|
|
124
|
+
|
|
125
|
+
Changes the track level.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
DashgramMini.setTrackLevel(3) // Enable all auto-events
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### `DashgramMini.flush()`
|
|
132
|
+
|
|
133
|
+
Forces sending all pending events to the server.
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
await DashgramMini.flush()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### `DashgramMini.reset()`
|
|
140
|
+
|
|
141
|
+
Resets the session and user information.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
DashgramMini.reset()
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `DashgramMini.shutdown()`
|
|
148
|
+
|
|
149
|
+
Stops the SDK and sends all remaining events.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
DashgramMini.shutdown()
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Element Visibility Tracking
|
|
156
|
+
|
|
157
|
+
For Level 3, you can mark elements for visibility tracking:
|
|
158
|
+
|
|
159
|
+
```html
|
|
160
|
+
<div data-track-visible="hero-banner">
|
|
161
|
+
<!-- Content -->
|
|
162
|
+
</div>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
When the element becomes visible (>50% of area), an `element_visible` event will be sent.
|
|
166
|
+
|
|
167
|
+
## Event Format
|
|
168
|
+
|
|
169
|
+
Every event is sent in the following format:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
{
|
|
173
|
+
"event": "event_name",
|
|
174
|
+
"properties": { /* custom properties */ },
|
|
175
|
+
"timestamp": "2024-01-01T12:00:00.000Z",
|
|
176
|
+
"source": "auto" | "manual",
|
|
177
|
+
"level": 1 | 2 | 3,
|
|
178
|
+
"session_id": "uuid",
|
|
179
|
+
"user_id": "telegram_user_id | null",
|
|
180
|
+
"context": {
|
|
181
|
+
"platform": "...",
|
|
182
|
+
"app_version": "...",
|
|
183
|
+
"language": "...",
|
|
184
|
+
"screen_width": 1920,
|
|
185
|
+
"screen_height": 1080,
|
|
186
|
+
"viewport_width": 1200,
|
|
187
|
+
"viewport_height": 800,
|
|
188
|
+
"user_agent": "...",
|
|
189
|
+
"timezone": "Europe/Moscow",
|
|
190
|
+
"telegram_version": "...",
|
|
191
|
+
"theme": "dark"
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## TypeScript
|
|
197
|
+
|
|
198
|
+
The SDK is written in TypeScript and includes all types:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import DashgramMini, {
|
|
202
|
+
DashgramConfig,
|
|
203
|
+
EventProperties,
|
|
204
|
+
UserTraits,
|
|
205
|
+
DashgramError,
|
|
206
|
+
InvalidCredentialsError,
|
|
207
|
+
DashgramAPIError,
|
|
208
|
+
NetworkError,
|
|
209
|
+
DashgramConfigurationError
|
|
210
|
+
} from "@dashgram/javascript"
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Error Handling
|
|
214
|
+
|
|
215
|
+
The SDK provides typed error classes for better error handling:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import {
|
|
219
|
+
DashgramError,
|
|
220
|
+
InvalidCredentialsError,
|
|
221
|
+
DashgramAPIError,
|
|
222
|
+
NetworkError,
|
|
223
|
+
DashgramConfigurationError
|
|
224
|
+
} from "@dashgram/javascript"
|
|
225
|
+
|
|
226
|
+
// Optional error handler callback
|
|
227
|
+
DashgramMini.init({
|
|
228
|
+
projectId: "xxx",
|
|
229
|
+
apiKey: "yyy",
|
|
230
|
+
onError: error => {
|
|
231
|
+
if (error instanceof InvalidCredentialsError) {
|
|
232
|
+
console.error("Invalid credentials!")
|
|
233
|
+
} else if (error instanceof NetworkError) {
|
|
234
|
+
console.error("Network issue:", error.originalError)
|
|
235
|
+
} else if (error instanceof DashgramConfigurationError) {
|
|
236
|
+
console.error("Configuration error:", error.message)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Features
|
|
243
|
+
|
|
244
|
+
- ✅ Tree-shakeable
|
|
245
|
+
- ✅ No heavy dependencies
|
|
246
|
+
- ✅ Batching and automatic sending
|
|
247
|
+
- ✅ sendBeacon support for reliable page unload tracking
|
|
248
|
+
- ✅ Automatic session management (30-minute timeout)
|
|
249
|
+
- ✅ Graceful degradation (works even without Telegram WebApp)
|
|
250
|
+
- ✅ Throttling and debouncing for performance optimization
|
|
251
|
+
- ✅ Offline mode support
|
|
252
|
+
- ✅ Typed error classes
|
|
253
|
+
- ✅ Optional error handler callback
|
|
254
|
+
|
|
255
|
+
## License
|
|
256
|
+
|
|
257
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DashgramConfig, TrackLevel } from "../types";
|
|
2
|
+
export declare class Config {
|
|
3
|
+
private config;
|
|
4
|
+
constructor(userConfig: DashgramConfig);
|
|
5
|
+
private validate;
|
|
6
|
+
get<K extends keyof Required<Omit<DashgramConfig, "onError">>>(key: K): Required<Omit<DashgramConfig, "onError">>[K];
|
|
7
|
+
getOnError(): DashgramConfig["onError"];
|
|
8
|
+
setTrackLevel(level: TrackLevel): void;
|
|
9
|
+
getTrackLevel(): TrackLevel;
|
|
10
|
+
isDebug(): boolean;
|
|
11
|
+
isDisabled(): boolean;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAkB1D,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAA6E;gBAE/E,UAAU,EAAE,cAAc;IAYtC,OAAO,CAAC,QAAQ;IAiBhB,GAAG,CAAC,CAAC,SAAS,MAAM,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAOpH,UAAU,IAAI,cAAc,CAAC,SAAS,CAAC;IAOvC,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAUtC,aAAa,IAAI,UAAU;IAO3B,OAAO,IAAI,OAAO;IAOlB,UAAU,IAAI,OAAO;CAGtB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DashgramConfigurationError } from "../errors";
|
|
2
|
+
const DEFAULT_CONFIG = {
|
|
3
|
+
trackLevel: 1,
|
|
4
|
+
apiUrl: "https://api.dashgram.com/v1/events",
|
|
5
|
+
batchSize: 10,
|
|
6
|
+
flushInterval: 5000,
|
|
7
|
+
debug: false,
|
|
8
|
+
disabled: false
|
|
9
|
+
};
|
|
10
|
+
export class Config {
|
|
11
|
+
constructor(userConfig) {
|
|
12
|
+
this.config = {
|
|
13
|
+
...DEFAULT_CONFIG,
|
|
14
|
+
...userConfig
|
|
15
|
+
};
|
|
16
|
+
this.validate();
|
|
17
|
+
}
|
|
18
|
+
validate() {
|
|
19
|
+
if (!this.config.projectId) {
|
|
20
|
+
throw new DashgramConfigurationError("projectId is required");
|
|
21
|
+
}
|
|
22
|
+
if (!this.config.apiKey) {
|
|
23
|
+
throw new DashgramConfigurationError("apiKey is required");
|
|
24
|
+
}
|
|
25
|
+
if (![1, 2, 3].includes(this.config.trackLevel)) {
|
|
26
|
+
throw new DashgramConfigurationError("trackLevel must be 1, 2, or 3");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
get(key) {
|
|
30
|
+
return this.config[key];
|
|
31
|
+
}
|
|
32
|
+
getOnError() {
|
|
33
|
+
return this.config.onError;
|
|
34
|
+
}
|
|
35
|
+
setTrackLevel(level) {
|
|
36
|
+
if (![1, 2, 3].includes(level)) {
|
|
37
|
+
throw new DashgramConfigurationError("trackLevel must be 1, 2, or 3");
|
|
38
|
+
}
|
|
39
|
+
this.config.trackLevel = level;
|
|
40
|
+
}
|
|
41
|
+
getTrackLevel() {
|
|
42
|
+
return this.config.trackLevel;
|
|
43
|
+
}
|
|
44
|
+
isDebug() {
|
|
45
|
+
return this.config.debug;
|
|
46
|
+
}
|
|
47
|
+
isDisabled() {
|
|
48
|
+
return this.config.disabled;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { EventContext } from '../types';
|
|
2
|
+
export declare class Context {
|
|
3
|
+
private context;
|
|
4
|
+
private userId;
|
|
5
|
+
constructor();
|
|
6
|
+
getContext(): EventContext;
|
|
7
|
+
updateContext(): void;
|
|
8
|
+
getUserId(): string | null;
|
|
9
|
+
setUserId(userId: string): void;
|
|
10
|
+
clearUserId(): void;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/core/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAO7C,qBAAa,OAAO;IAClB,OAAO,CAAC,OAAO,CAAe;IAC9B,OAAO,CAAC,MAAM,CAAgB;;IAU9B,UAAU,IAAI,YAAY;IAO1B,aAAa,IAAI,IAAI;IAOrB,SAAS,IAAI,MAAM,GAAG,IAAI;IAO1B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAO/B,WAAW,IAAI,IAAI;CAGpB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getDeviceContext } from '../utils/device';
|
|
2
|
+
import { getTelegramUserId } from '../utils/telegram';
|
|
3
|
+
export class Context {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.context = getDeviceContext();
|
|
6
|
+
this.userId = getTelegramUserId();
|
|
7
|
+
}
|
|
8
|
+
getContext() {
|
|
9
|
+
return { ...this.context };
|
|
10
|
+
}
|
|
11
|
+
updateContext() {
|
|
12
|
+
this.context = getDeviceContext();
|
|
13
|
+
}
|
|
14
|
+
getUserId() {
|
|
15
|
+
return this.userId;
|
|
16
|
+
}
|
|
17
|
+
setUserId(userId) {
|
|
18
|
+
this.userId = userId;
|
|
19
|
+
}
|
|
20
|
+
clearUserId() {
|
|
21
|
+
this.userId = null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DashgramEvent } from '../types';
|
|
2
|
+
export declare class EventQueue {
|
|
3
|
+
private queue;
|
|
4
|
+
private maxSize;
|
|
5
|
+
constructor(maxSize?: number);
|
|
6
|
+
enqueue(event: DashgramEvent): void;
|
|
7
|
+
flush(): DashgramEvent[];
|
|
8
|
+
peek(): DashgramEvent[];
|
|
9
|
+
size(): number;
|
|
10
|
+
isEmpty(): boolean;
|
|
11
|
+
clear(): void;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=event-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-queue.d.ts","sourceRoot":"","sources":["../../src/core/event-queue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAK9C,qBAAa,UAAU;IACrB,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,GAAE,MAAY;IAOjC,OAAO,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI;IAYnC,KAAK,IAAI,aAAa,EAAE;IASxB,IAAI,IAAI,aAAa,EAAE;IAOvB,IAAI,IAAI,MAAM;IAOd,OAAO,IAAI,OAAO;IAOlB,KAAK,IAAI,IAAI;CAGd"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class EventQueue {
|
|
2
|
+
constructor(maxSize = 100) {
|
|
3
|
+
this.queue = [];
|
|
4
|
+
this.maxSize = maxSize;
|
|
5
|
+
}
|
|
6
|
+
enqueue(event) {
|
|
7
|
+
this.queue.push(event);
|
|
8
|
+
if (this.queue.length > this.maxSize) {
|
|
9
|
+
this.queue.shift();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
flush() {
|
|
13
|
+
const events = [...this.queue];
|
|
14
|
+
this.queue = [];
|
|
15
|
+
return events;
|
|
16
|
+
}
|
|
17
|
+
peek() {
|
|
18
|
+
return [...this.queue];
|
|
19
|
+
}
|
|
20
|
+
size() {
|
|
21
|
+
return this.queue.length;
|
|
22
|
+
}
|
|
23
|
+
isEmpty() {
|
|
24
|
+
return this.queue.length === 0;
|
|
25
|
+
}
|
|
26
|
+
clear() {
|
|
27
|
+
this.queue = [];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class Session {
|
|
2
|
+
private sessionId;
|
|
3
|
+
private lastActivity;
|
|
4
|
+
constructor();
|
|
5
|
+
private loadOrCreateSession;
|
|
6
|
+
private createNewSession;
|
|
7
|
+
updateLastActivity(): void;
|
|
8
|
+
getSessionId(): string;
|
|
9
|
+
reset(): void;
|
|
10
|
+
isExpired(): boolean;
|
|
11
|
+
renewIfExpired(): void;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/core/session.ts"],"names":[],"mappings":"AASA,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAS;;IAW7B,OAAO,CAAC,mBAAmB;IA6B3B,OAAO,CAAC,gBAAgB;IAgBxB,kBAAkB,IAAI,IAAI;IAa1B,YAAY,IAAI,MAAM;IAOtB,KAAK,IAAI,IAAI;IAQb,SAAS,IAAI,OAAO;IAOpB,cAAc,IAAI,IAAI;CAKvB"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { generateUUID } from '../utils/helpers';
|
|
2
|
+
const SESSION_KEY = 'dashgram_session_id';
|
|
3
|
+
const SESSION_TIMEOUT = 30 * 60 * 1000;
|
|
4
|
+
const LAST_ACTIVITY_KEY = 'dashgram_last_activity';
|
|
5
|
+
export class Session {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.sessionId = this.loadOrCreateSession();
|
|
8
|
+
this.lastActivity = Date.now();
|
|
9
|
+
this.updateLastActivity();
|
|
10
|
+
}
|
|
11
|
+
loadOrCreateSession() {
|
|
12
|
+
if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
|
|
13
|
+
return generateUUID();
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const storedSessionId = localStorage.getItem(SESSION_KEY);
|
|
17
|
+
const storedLastActivity = localStorage.getItem(LAST_ACTIVITY_KEY);
|
|
18
|
+
if (storedSessionId && storedLastActivity) {
|
|
19
|
+
const lastActivity = parseInt(storedLastActivity, 10);
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
if (now - lastActivity < SESSION_TIMEOUT) {
|
|
22
|
+
return storedSessionId;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
}
|
|
28
|
+
return this.createNewSession();
|
|
29
|
+
}
|
|
30
|
+
createNewSession() {
|
|
31
|
+
const newSessionId = generateUUID();
|
|
32
|
+
try {
|
|
33
|
+
localStorage.setItem(SESSION_KEY, newSessionId);
|
|
34
|
+
localStorage.setItem(LAST_ACTIVITY_KEY, Date.now().toString());
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
}
|
|
38
|
+
return newSessionId;
|
|
39
|
+
}
|
|
40
|
+
updateLastActivity() {
|
|
41
|
+
this.lastActivity = Date.now();
|
|
42
|
+
try {
|
|
43
|
+
localStorage.setItem(LAST_ACTIVITY_KEY, this.lastActivity.toString());
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
getSessionId() {
|
|
49
|
+
return this.sessionId;
|
|
50
|
+
}
|
|
51
|
+
reset() {
|
|
52
|
+
this.sessionId = this.createNewSession();
|
|
53
|
+
this.lastActivity = Date.now();
|
|
54
|
+
}
|
|
55
|
+
isExpired() {
|
|
56
|
+
return Date.now() - this.lastActivity > SESSION_TIMEOUT;
|
|
57
|
+
}
|
|
58
|
+
renewIfExpired() {
|
|
59
|
+
if (this.isExpired()) {
|
|
60
|
+
this.reset();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare class DashgramError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class InvalidCredentialsError extends DashgramError {
|
|
5
|
+
constructor(message?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class DashgramAPIError extends DashgramError {
|
|
8
|
+
statusCode: number;
|
|
9
|
+
details: string;
|
|
10
|
+
constructor(statusCode: number, details: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class NetworkError extends DashgramError {
|
|
13
|
+
originalError: Error;
|
|
14
|
+
constructor(originalError: Error);
|
|
15
|
+
}
|
|
16
|
+
export declare class DashgramConfigurationError extends DashgramError {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAUA,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAS5B;AAKD,qBAAa,uBAAwB,SAAQ,aAAa;gBAC5C,OAAO,SAAgC;CAQpD;AAKD,qBAAa,gBAAiB,SAAQ,aAAa;IAC9B,UAAU,EAAE,MAAM;IAAS,OAAO,EAAE,MAAM;gBAA1C,UAAU,EAAE,MAAM,EAAS,OAAO,EAAE,MAAM;CAQ9D;AAKD,qBAAa,YAAa,SAAQ,aAAa;IAC1B,aAAa,EAAE,KAAK;gBAApB,aAAa,EAAE,KAAK;CAQxC;AAKD,qBAAa,0BAA2B,SAAQ,aAAa;gBAC/C,OAAO,EAAE,MAAM;CAQ5B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class DashgramError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "DashgramError";
|
|
5
|
+
const ErrorWithCapture = Error;
|
|
6
|
+
if (typeof ErrorWithCapture.captureStackTrace === "function") {
|
|
7
|
+
ErrorWithCapture.captureStackTrace(this, DashgramError);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class InvalidCredentialsError extends DashgramError {
|
|
12
|
+
constructor(message = "Invalid projectId or apiKey") {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "InvalidCredentialsError";
|
|
15
|
+
const ErrorWithCapture = Error;
|
|
16
|
+
if (typeof ErrorWithCapture.captureStackTrace === "function") {
|
|
17
|
+
ErrorWithCapture.captureStackTrace(this, InvalidCredentialsError);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class DashgramAPIError extends DashgramError {
|
|
22
|
+
constructor(statusCode, details) {
|
|
23
|
+
super(`Dashgram API error (${statusCode}): ${details}`);
|
|
24
|
+
this.statusCode = statusCode;
|
|
25
|
+
this.details = details;
|
|
26
|
+
this.name = "DashgramAPIError";
|
|
27
|
+
const ErrorWithCapture = Error;
|
|
28
|
+
if (typeof ErrorWithCapture.captureStackTrace === "function") {
|
|
29
|
+
ErrorWithCapture.captureStackTrace(this, DashgramAPIError);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class NetworkError extends DashgramError {
|
|
34
|
+
constructor(originalError) {
|
|
35
|
+
super(`Network error: ${originalError.message}`);
|
|
36
|
+
this.originalError = originalError;
|
|
37
|
+
this.name = "NetworkError";
|
|
38
|
+
const ErrorWithCapture = Error;
|
|
39
|
+
if (typeof ErrorWithCapture.captureStackTrace === "function") {
|
|
40
|
+
ErrorWithCapture.captureStackTrace(this, NetworkError);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class DashgramConfigurationError extends DashgramError {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "DashgramConfigurationError";
|
|
48
|
+
const ErrorWithCapture = Error;
|
|
49
|
+
if (typeof ErrorWithCapture.captureStackTrace === "function") {
|
|
50
|
+
ErrorWithCapture.captureStackTrace(this, DashgramConfigurationError);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DashgramConfig, DashgramEvent, EventProperties, UserTraits, TrackLevel } from "./types";
|
|
2
|
+
declare class DashgramSDK {
|
|
3
|
+
private config;
|
|
4
|
+
private context;
|
|
5
|
+
private session;
|
|
6
|
+
private transport;
|
|
7
|
+
private batchProcessor;
|
|
8
|
+
private trackers;
|
|
9
|
+
private isInitialized;
|
|
10
|
+
init(userConfig: DashgramConfig): void;
|
|
11
|
+
private setupTrackers;
|
|
12
|
+
track(event: string, properties?: EventProperties): void;
|
|
13
|
+
private trackAuto;
|
|
14
|
+
private buildEvent;
|
|
15
|
+
identify(userId: string, traits?: UserTraits): void;
|
|
16
|
+
setTrackLevel(level: TrackLevel): void;
|
|
17
|
+
flush(): Promise<void>;
|
|
18
|
+
reset(): void;
|
|
19
|
+
shutdown(): void;
|
|
20
|
+
private ensureInitialized;
|
|
21
|
+
private log;
|
|
22
|
+
}
|
|
23
|
+
declare const DashgramMini: DashgramSDK;
|
|
24
|
+
export type { DashgramConfig, DashgramEvent, EventProperties, UserTraits, TrackLevel };
|
|
25
|
+
export { DashgramError, InvalidCredentialsError, DashgramAPIError, NetworkError, DashgramConfigurationError } from "./errors";
|
|
26
|
+
export { DashgramMini };
|
|
27
|
+
export default DashgramMini;
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAerG,cAAM,WAAW;IACf,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,OAAO,CAAuB;IACtC,OAAO,CAAC,OAAO,CAAuB;IACtC,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,cAAc,CAA8B;IACpD,OAAO,CAAC,QAAQ,CAA4D;IAC5E,OAAO,CAAC,aAAa,CAAQ;IAK7B,IAAI,CAAC,UAAU,EAAE,cAAc,GAAG,IAAI;IAwCtC,OAAO,CAAC,aAAa;IA0BrB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,GAAE,eAAoB,GAAG,IAAI;IAY5D,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,UAAU;IAsBlB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,UAAe,GAAG,IAAI;IAgBvD,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAkBhC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAW5B,KAAK,IAAI,IAAI;IAYb,QAAQ,IAAI,IAAI;IAqBhB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,GAAG;CAKZ;AAGD,QAAA,MAAM,YAAY,aAAoB,CAAA;AAGtC,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,CAAA;AAGtF,OAAO,EACL,aAAa,EACb,uBAAuB,EACvB,gBAAgB,EAChB,YAAY,EACZ,0BAA0B,EAC3B,MAAM,UAAU,CAAA;AAGjB,OAAO,EAAE,YAAY,EAAE,CAAA;AAGvB,eAAe,YAAY,CAAA"}
|