@dashgram/javascript 1.0.0 → 1.0.2

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.
Files changed (47) hide show
  1. package/README.md +117 -174
  2. package/dist/core/config.d.ts +1 -0
  3. package/dist/core/config.d.ts.map +1 -1
  4. package/dist/core/config.js +5 -4
  5. package/dist/core/context.d.ts +4 -8
  6. package/dist/core/context.d.ts.map +1 -1
  7. package/dist/core/context.js +6 -17
  8. package/dist/core/event-queue.d.ts +4 -4
  9. package/dist/core/event-queue.d.ts.map +1 -1
  10. package/dist/dashgram.min.js +2 -0
  11. package/dist/dashgram.min.js.map +1 -0
  12. package/dist/errors.d.ts +0 -3
  13. package/dist/errors.d.ts.map +1 -1
  14. package/dist/errors.js +0 -10
  15. package/dist/index.d.ts +3 -6
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +20 -32
  18. package/dist/trackers/base-tracker.d.ts +3 -3
  19. package/dist/trackers/base-tracker.d.ts.map +1 -1
  20. package/dist/trackers/base-tracker.js +1 -4
  21. package/dist/trackers/core-tracker.d.ts +3 -3
  22. package/dist/trackers/core-tracker.d.ts.map +1 -1
  23. package/dist/trackers/core-tracker.js +19 -21
  24. package/dist/trackers/deep-tracker.d.ts +6 -0
  25. package/dist/trackers/deep-tracker.d.ts.map +1 -1
  26. package/dist/trackers/deep-tracker.js +562 -12
  27. package/dist/trackers/interaction-tracker.d.ts +7 -3
  28. package/dist/trackers/interaction-tracker.d.ts.map +1 -1
  29. package/dist/trackers/interaction-tracker.js +142 -37
  30. package/dist/transport/batch-processor.d.ts +4 -4
  31. package/dist/transport/batch-processor.d.ts.map +1 -1
  32. package/dist/transport/batch-processor.js +8 -8
  33. package/dist/transport/transport.d.ts +4 -3
  34. package/dist/transport/transport.d.ts.map +1 -1
  35. package/dist/transport/transport.js +17 -24
  36. package/dist/types/index.d.ts +100 -25
  37. package/dist/types/index.d.ts.map +1 -1
  38. package/dist/utils/device.d.ts +3 -7
  39. package/dist/utils/device.d.ts.map +1 -1
  40. package/dist/utils/device.js +19 -53
  41. package/dist/utils/telegram.d.ts +4 -6
  42. package/dist/utils/telegram.d.ts.map +1 -1
  43. package/dist/utils/telegram.js +45 -29
  44. package/package.json +9 -3
  45. package/dist/core/session.d.ts +0 -13
  46. package/dist/core/session.d.ts.map +0 -1
  47. package/dist/core/session.js +0 -63
@@ -1,72 +1,38 @@
1
- import { getTelegramPlatform, getTelegramVersion, getTelegramLanguage, getTelegramTheme, } from './telegram';
2
- export function getDeviceContext() {
3
- if (typeof window === 'undefined') {
1
+ import { getTelegramPlatform, getTelegramTheme } from "./telegram";
2
+ export function getTelemetry() {
3
+ if (typeof window === "undefined") {
4
4
  return {
5
- platform: 'unknown',
6
- app_version: 'unknown',
7
- language: 'unknown',
8
- screen_width: 0,
9
- screen_height: 0,
10
- viewport_width: 0,
11
- viewport_height: 0,
12
- user_agent: '',
13
- timezone: 'UTC',
5
+ platform: "unknown"
14
6
  };
15
7
  }
16
- const telegramLang = getTelegramLanguage();
17
- const browserLang = navigator.language || 'en';
18
8
  return {
19
9
  platform: getTelegramPlatform(),
20
- app_version: getTelegramVersion(),
21
- language: telegramLang || browserLang,
22
- screen_width: window.screen.width,
23
- screen_height: window.screen.height,
24
- viewport_width: window.innerWidth,
25
- viewport_height: window.innerHeight,
26
10
  user_agent: navigator.userAgent,
27
- timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC',
28
- telegram_version: getTelegramVersion(),
29
- theme: getTelegramTheme(),
11
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || undefined,
12
+ theme: getTelegramTheme()
30
13
  };
31
14
  }
32
- export function getViewportSize() {
33
- if (typeof window === 'undefined') {
34
- return { width: 0, height: 0 };
35
- }
36
- return {
37
- width: window.innerWidth,
38
- height: window.innerHeight,
39
- };
40
- }
41
- export function getScrollDepth() {
42
- if (typeof window === 'undefined' || typeof document === 'undefined') {
43
- return 0;
44
- }
45
- const windowHeight = window.innerHeight;
46
- const documentHeight = document.documentElement.scrollHeight;
47
- const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
48
- if (documentHeight <= windowHeight) {
49
- return 100;
50
- }
51
- const maxScroll = documentHeight - windowHeight;
52
- const scrollPercentage = (scrollTop / maxScroll) * 100;
53
- return Math.min(Math.round(scrollPercentage), 100);
54
- }
55
15
  export function getCurrentUrl() {
56
- if (typeof window === 'undefined') {
57
- return '';
16
+ if (typeof window === "undefined") {
17
+ return "";
58
18
  }
59
19
  return window.location.href;
60
20
  }
61
21
  export function getCurrentPath() {
62
- if (typeof window === 'undefined') {
63
- return '';
22
+ if (typeof window === "undefined") {
23
+ return "";
64
24
  }
65
25
  return window.location.pathname;
66
26
  }
67
27
  export function getPageTitle() {
68
- if (typeof document === 'undefined') {
69
- return '';
28
+ if (typeof document === "undefined") {
29
+ return "";
30
+ }
31
+ return document.title || "";
32
+ }
33
+ export function getCurrentOrigin() {
34
+ if (typeof window === "undefined") {
35
+ return "";
70
36
  }
71
- return document.title || '';
37
+ return window.location.origin;
72
38
  }
@@ -1,10 +1,8 @@
1
- import type { TelegramWebApp } from '../types';
1
+ import type { TelegramWebApp } from "../types";
2
2
  export declare function getTelegramWebApp(): TelegramWebApp | null;
3
3
  export declare function isTelegramMiniApp(): boolean;
4
- export declare function getTelegramUserId(): string | null;
4
+ export declare function getTelegramInitData(): string;
5
5
  export declare function getTelegramPlatform(): string;
6
- export declare function getTelegramVersion(): string;
7
- export declare function getTelegramLanguage(): string | null;
8
- export declare function getTelegramTheme(): string;
9
- export declare function subscribeToTelegramEvent(event: string, callback: () => void): () => void;
6
+ export declare function getTelegramTheme(): string | undefined;
7
+ export declare function subscribeToTelegramEvent(event: string, callback: (eventData?: unknown) => void): () => void;
10
8
  //# sourceMappingURL=telegram.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"telegram.d.ts","sourceRoot":"","sources":["../../src/utils/telegram.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,UAAU,CAAC;AAK/D,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,IAAI,CAOzD;AAKD,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAKD,wBAAgB,iBAAiB,IAAI,MAAM,GAAG,IAAI,CAIjD;AAKD,wBAAgB,mBAAmB,IAAI,MAAM,CAG5C;AAKD,wBAAgB,kBAAkB,IAAI,MAAM,CAG3C;AAKD,wBAAgB,mBAAmB,IAAI,MAAM,GAAG,IAAI,CAGnD;AAKD,wBAAgB,gBAAgB,IAAI,MAAM,CAmBzC;AAKD,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,IAAI,GACnB,MAAM,IAAI,CAgBZ"}
1
+ {"version":3,"file":"telegram.d.ts","sourceRoot":"","sources":["../../src/utils/telegram.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAkB,MAAM,UAAU,CAAA;AAK9D,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,IAAI,CAOzD;AAKD,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAKD,wBAAgB,mBAAmB,IAAI,MAAM,CAG5C;AAKD,wBAAgB,mBAAmB,IAAI,MAAM,CAG5C;AAKD,wBAAgB,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAwBrD;AAKD,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,SAAS,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAmC3G"}
@@ -1,5 +1,5 @@
1
1
  export function getTelegramWebApp() {
2
- if (typeof window === 'undefined') {
2
+ if (typeof window === "undefined") {
3
3
  return null;
4
4
  }
5
5
  const win = window;
@@ -8,48 +8,64 @@ export function getTelegramWebApp() {
8
8
  export function isTelegramMiniApp() {
9
9
  return getTelegramWebApp() !== null;
10
10
  }
11
- export function getTelegramUserId() {
11
+ export function getTelegramInitData() {
12
12
  const webApp = getTelegramWebApp();
13
- const userId = webApp?.initDataUnsafe?.user?.id;
14
- return userId ? String(userId) : null;
13
+ return webApp?.initData || "";
15
14
  }
16
15
  export function getTelegramPlatform() {
17
16
  const webApp = getTelegramWebApp();
18
- return webApp?.platform || 'unknown';
19
- }
20
- export function getTelegramVersion() {
21
- const webApp = getTelegramWebApp();
22
- return webApp?.version || 'unknown';
23
- }
24
- export function getTelegramLanguage() {
25
- const webApp = getTelegramWebApp();
26
- return webApp?.initDataUnsafe?.user?.language_code || null;
17
+ return webApp?.platform || "unknown";
27
18
  }
28
19
  export function getTelegramTheme() {
29
20
  const webApp = getTelegramWebApp();
30
- if (!webApp?.themeParams) {
31
- return 'unknown';
21
+ if (webApp?.colorScheme) {
22
+ return webApp.colorScheme;
32
23
  }
33
- const bgColor = webApp.themeParams.bg_color || '';
34
- if (bgColor) {
35
- const rgb = parseInt(bgColor.slice(1), 16);
36
- const r = (rgb >> 16) & 0xff;
37
- const g = (rgb >> 8) & 0xff;
38
- const b = (rgb >> 0) & 0xff;
39
- const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
40
- return luma < 128 ? 'dark' : 'light';
24
+ if (webApp?.themeParams?.bg_color) {
25
+ const bgColor = webApp.themeParams.bg_color;
26
+ try {
27
+ const rgb = parseInt(bgColor.slice(1), 16);
28
+ const r = (rgb >> 16) & 0xff;
29
+ const g = (rgb >> 8) & 0xff;
30
+ const b = (rgb >> 0) & 0xff;
31
+ const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
32
+ return luma < 128 ? "dark" : "light";
33
+ }
34
+ catch {
35
+ return undefined;
36
+ }
41
37
  }
42
- return 'unknown';
38
+ return undefined;
43
39
  }
44
40
  export function subscribeToTelegramEvent(event, callback) {
45
41
  const webApp = getTelegramWebApp();
46
- if (webApp && webApp.onEvent) {
47
- webApp.onEvent(event, callback);
42
+ if (!webApp || !webApp.onEvent) {
43
+ return () => { };
44
+ }
45
+ try {
46
+ const wrappedCallback = (_eventType, eventData) => {
47
+ try {
48
+ callback(eventData);
49
+ }
50
+ catch (error) {
51
+ if (typeof window !== "undefined" &&
52
+ window.__DASHGRAM_DEBUG__) {
53
+ console.warn(`[Dashgram] Error in Telegram event callback for ${event}:`, error);
54
+ }
55
+ }
56
+ };
57
+ webApp.onEvent(event, wrappedCallback);
48
58
  return () => {
49
- if (webApp.offEvent) {
50
- webApp.offEvent(event, callback);
59
+ try {
60
+ if (webApp.offEvent) {
61
+ webApp.offEvent(event, wrappedCallback);
62
+ }
63
+ }
64
+ catch {
51
65
  }
52
66
  };
53
67
  }
54
- return () => { };
68
+ catch {
69
+ return () => { };
70
+ }
55
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dashgram/javascript",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Dashgram Analytics SDK for Telegram Mini Apps",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -22,7 +22,9 @@
22
22
  "scripts": {
23
23
  "build": "tsc",
24
24
  "dev": "tsc --watch",
25
- "prepublishOnly": "npm run build"
25
+ "build:umd": "rollup -c",
26
+ "build:all": "npm run build && npm run build:umd",
27
+ "prepublishOnly": "npm run build:all"
26
28
  },
27
29
  "keywords": [
28
30
  "dashgram",
@@ -38,13 +40,17 @@
38
40
  "license": "MIT",
39
41
  "repository": {
40
42
  "type": "git",
41
- "url": "https://github.com/dashgram/dashgram-javascript.git"
43
+ "url": "git+https://github.com/dashgram/dashgram-javascript.git"
42
44
  },
43
45
  "bugs": {
44
46
  "url": "https://github.com/dashgram/dashgram-javascript/issues"
45
47
  },
46
48
  "homepage": "https://github.com/dashgram/dashgram-javascript#readme",
47
49
  "devDependencies": {
50
+ "@rollup/plugin-typescript": "^12.3.0",
51
+ "rollup": "^2.79.2",
52
+ "rollup-plugin-terser": "^7.0.2",
53
+ "tslib": "^2.8.1",
48
54
  "typescript": "^5.3.3"
49
55
  }
50
56
  }
@@ -1,13 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,63 +0,0 @@
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
- }