userpath-js 0.0.0 → 0.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 (59) hide show
  1. package/README.md +609 -0
  2. package/dist/index.js +1 -0
  3. package/dist/server/index.js +1 -0
  4. package/dist/types/client/src/base/api.d.ts +12 -0
  5. package/dist/types/client/src/base/api.d.ts.map +1 -0
  6. package/dist/types/client/src/base/events.d.ts +184 -0
  7. package/dist/types/client/src/base/events.d.ts.map +1 -0
  8. package/dist/types/client/src/base/session.d.ts +68 -0
  9. package/dist/types/client/src/base/session.d.ts.map +1 -0
  10. package/dist/types/client/src/index.d.ts +57 -0
  11. package/dist/types/client/src/index.d.ts.map +1 -0
  12. package/dist/types/client/src/pixel.d.ts +2 -0
  13. package/dist/types/client/src/pixel.d.ts.map +1 -0
  14. package/dist/types/client/src/schemas/config.d.ts +32 -0
  15. package/dist/types/client/src/schemas/config.d.ts.map +1 -0
  16. package/dist/types/client/src/schemas/events.d.ts +58 -0
  17. package/dist/types/client/src/schemas/events.d.ts.map +1 -0
  18. package/dist/types/client/src/schemas/identity.d.ts +7 -0
  19. package/dist/types/client/src/schemas/identity.d.ts.map +1 -0
  20. package/dist/types/client/src/server/index.d.ts +42 -0
  21. package/dist/types/client/src/server/index.d.ts.map +1 -0
  22. package/dist/types/core/src/browser/index.d.ts +4 -0
  23. package/dist/types/core/src/browser/index.d.ts.map +1 -0
  24. package/dist/types/core/src/browser/libs/LocalStorage.d.ts +11 -0
  25. package/dist/types/core/src/browser/libs/LocalStorage.d.ts.map +1 -0
  26. package/dist/types/core/src/browser/libs/cookie.d.ts +27 -0
  27. package/dist/types/core/src/browser/libs/cookie.d.ts.map +1 -0
  28. package/dist/types/core/src/browser/libs/memory.d.ts +4 -0
  29. package/dist/types/core/src/browser/libs/memory.d.ts.map +1 -0
  30. package/dist/types/src/base/api.d.ts +12 -0
  31. package/dist/types/src/base/api.d.ts.map +1 -0
  32. package/dist/types/src/base/events.d.ts +184 -0
  33. package/dist/types/src/base/events.d.ts.map +1 -0
  34. package/dist/types/src/base/session.d.ts +64 -0
  35. package/dist/types/src/base/session.d.ts.map +1 -0
  36. package/dist/types/src/index.d.ts +57 -0
  37. package/dist/types/src/index.d.ts.map +1 -0
  38. package/dist/types/src/pixel.d.ts +2 -0
  39. package/dist/types/src/pixel.d.ts.map +1 -0
  40. package/dist/types/src/schemas/config.d.ts +32 -0
  41. package/dist/types/src/schemas/config.d.ts.map +1 -0
  42. package/dist/types/src/schemas/events.d.ts +58 -0
  43. package/dist/types/src/schemas/events.d.ts.map +1 -0
  44. package/dist/types/src/schemas/identity.d.ts +7 -0
  45. package/dist/types/src/schemas/identity.d.ts.map +1 -0
  46. package/dist/types/src/server/index.d.ts +42 -0
  47. package/dist/types/src/server/index.d.ts.map +1 -0
  48. package/package.json +25 -4
  49. package/dist/pixel.js +0 -1
  50. package/examples/usage.ts +0 -83
  51. package/src/clients/api.ts +0 -92
  52. package/src/clients/events.ts +0 -55
  53. package/src/clients/identity.ts +0 -54
  54. package/src/clients/session.ts +0 -47
  55. package/src/index.ts +0 -72
  56. package/src/pixel.ts +0 -28
  57. package/src/schemas/config.ts +0 -4
  58. package/src/schemas/events.ts +0 -31
  59. package/src/schemas/identity.ts +0 -6
@@ -1,92 +0,0 @@
1
- import { memory } from '@userpath/core/browser';
2
-
3
- import { Config } from '../schemas/config';
4
-
5
- /**
6
- * Base API client for UserPath SDK
7
- */
8
- export class ApiClient {
9
- protected baseUrl: string;
10
- protected siteId: string;
11
- protected queue: any[] = [];
12
- protected flushInterval: number | null = null;
13
-
14
- constructor(config: Config) {
15
- this.baseUrl = config.baseUrl || '';
16
- this.siteId = config.siteId;
17
-
18
- // Set up automatic flushing
19
- this.flushInterval = window.setInterval(() => this.flush(), 5000);
20
- }
21
-
22
- /**
23
- * Add an event to the queue and flush if needed
24
- */
25
- public track(event: any): void {
26
- this.queue.push(event);
27
-
28
- if (this.queue.length >= 10) {
29
- this.flush();
30
- }
31
- }
32
-
33
- /**
34
- * Flush queued events to the server
35
- */
36
- protected flush(): void {
37
- if (this.queue.length === 0) return;
38
-
39
- const eventsToSend = [...this.queue];
40
- this.queue = [];
41
-
42
- fetch(`${this.baseUrl}/events`, {
43
- method: 'POST',
44
- headers: {
45
- 'Content-Type': 'application/json',
46
- },
47
- body: JSON.stringify({
48
- siteId: this.siteId,
49
- sessionId: this.getSessionId(),
50
- timestamp: new Date().toISOString(),
51
- userAgent: navigator.userAgent,
52
- screenWidth: window.screen.width,
53
- screenHeight: window.screen.height,
54
- language: navigator.language,
55
- timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
56
- events: eventsToSend,
57
- }),
58
- // Use beacon API for more reliable delivery
59
- keepalive: true,
60
- }).catch((error) => {
61
- // If sending fails, add back to queue for retry
62
- this.queue = [...eventsToSend, ...this.queue].slice(0, 100);
63
- console.error('Error sending analytics data:', error);
64
- });
65
- }
66
-
67
- /**
68
- * Get or generate a session ID
69
- */
70
- protected getSessionId(): string {
71
- let sessionId = memory.getItem<string>('session_id');
72
- if (!sessionId) {
73
- sessionId = this.generateId();
74
- memory.setItem('session_id', sessionId);
75
- }
76
- return sessionId;
77
- }
78
-
79
- /**
80
- * Generate a UUID v4
81
- */
82
- public generateId(): string {
83
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
84
- /[xy]/g,
85
- function (c) {
86
- const r = (Math.random() * 16) | 0;
87
- const v = c === 'x' ? r : (r & 0x3) | 0x8;
88
- return v.toString(16);
89
- }
90
- );
91
- }
92
- }
@@ -1,55 +0,0 @@
1
- import { ApiClient } from './api';
2
-
3
- /**
4
- * Events client for handling different types of tracking events
5
- */
6
- export class EventsClient {
7
- constructor(private api: ApiClient) {}
8
-
9
- /**
10
- * Track a page view event
11
- */
12
- public trackPageView(): void {
13
- const event = {
14
- type: 'pageview',
15
- url: window.location.href,
16
- referrer: document.referrer,
17
- title: document.title,
18
- };
19
-
20
- this.api.track(event);
21
- }
22
-
23
- /**
24
- * Track a custom event
25
- */
26
- public trackCustomEvent(
27
- name: string,
28
- properties: Record<string, any> = {}
29
- ): void {
30
- const event = {
31
- type: 'event',
32
- name,
33
- properties,
34
- };
35
-
36
- this.api.track(event);
37
- }
38
-
39
- /**
40
- * Track a purchase event
41
- */
42
- public trackPurchase(params: {
43
- productId: string;
44
- price: number;
45
- currency: string;
46
- properties?: Record<string, any>;
47
- }): void {
48
- const event = {
49
- type: 'purchase',
50
- ...params,
51
- };
52
-
53
- this.api.track(event);
54
- }
55
- }
@@ -1,54 +0,0 @@
1
- import { UserIdentity } from '../schemas/identity';
2
- import { ApiClient } from './api';
3
-
4
- /**
5
- * Identity client for managing user identification
6
- */
7
- export class IdentityClient {
8
- private anonymousId: string | null = null;
9
- private currentUser: UserIdentity | null = null;
10
-
11
- constructor(private api: ApiClient) {
12
- this.anonymousId = this.getStoredAnonymousId();
13
- }
14
-
15
- /**
16
- * Get the anonymous ID for the current visitor
17
- */
18
- public getAnonymousId(): string {
19
- if (!this.anonymousId) {
20
- this.anonymousId = this.api.generateId();
21
- localStorage.setItem('up_anonymous_id', this.anonymousId);
22
- }
23
- return this.anonymousId;
24
- }
25
-
26
- /**
27
- * Get the current identified user's ID if available
28
- */
29
- public getUserId(): string | null {
30
- return this.currentUser?.userId || null;
31
- }
32
-
33
- /**
34
- * Identify a user and associate them with their anonymous ID
35
- */
36
- public identify(identity: UserIdentity): void {
37
- // Store the current user
38
- this.currentUser = identity;
39
-
40
- // Track the identity mapping
41
- this.api.track({
42
- type: 'identify',
43
- anonymousId: this.getAnonymousId(),
44
- ...identity,
45
- });
46
- }
47
-
48
- /**
49
- * Get the stored anonymous ID if it exists
50
- */
51
- private getStoredAnonymousId(): string | null {
52
- return localStorage.getItem('up_anonymous_id');
53
- }
54
- }
@@ -1,47 +0,0 @@
1
- import { ApiClient } from './api';
2
- import { EventsClient } from './events';
3
-
4
- /**
5
- * Session client for managing user sessions and automatic tracking
6
- */
7
- export class SessionClient {
8
- private events: EventsClient;
9
-
10
- constructor(private api: ApiClient) {
11
- this.events = new EventsClient(api);
12
- this.initialize();
13
- }
14
-
15
- /**
16
- * Initialize session tracking
17
- */
18
- private initialize(): void {
19
- // Track initial pageview
20
- this.events.trackPageView();
21
-
22
- // Set up history change tracking
23
- this.setupHistoryChange();
24
- }
25
-
26
- /**
27
- * Set up tracking for history/navigation changes
28
- */
29
- private setupHistoryChange(): void {
30
- const originalPushState = history.pushState;
31
- const originalReplaceState = history.replaceState;
32
-
33
- history.pushState = (...args) => {
34
- originalPushState.apply(history, args);
35
- this.events.trackPageView();
36
- };
37
-
38
- history.replaceState = (...args) => {
39
- originalReplaceState.apply(history, args);
40
- this.events.trackPageView();
41
- };
42
-
43
- window.addEventListener('popstate', () => {
44
- this.events.trackPageView();
45
- });
46
- }
47
- }
package/src/index.ts DELETED
@@ -1,72 +0,0 @@
1
- import { ApiClient } from './clients/api';
2
- import { EventsClient } from './clients/events';
3
- import { IdentityClient } from './clients/identity';
4
- import { SessionClient } from './clients/session';
5
- import { Config } from './schemas/config';
6
- import { UserIdentity } from './schemas/identity';
7
-
8
- export class UserPath {
9
- private events: EventsClient;
10
- private session: SessionClient;
11
- private identity: IdentityClient;
12
-
13
- constructor(config: Config) {
14
- // Initialize clients
15
- const baseClient = new ApiClient(config);
16
- this.events = new EventsClient(baseClient);
17
- this.session = new SessionClient(baseClient);
18
- this.identity = new IdentityClient(baseClient);
19
- }
20
-
21
- /**
22
- * Get the anonymous ID for the current visitor
23
- */
24
- public getAnonymousId(): string {
25
- return this.identity.getAnonymousId();
26
- }
27
-
28
- /**
29
- * Get the current identified user's ID if available
30
- */
31
- public getUserId(): string | null {
32
- return this.identity.getUserId();
33
- }
34
-
35
- /**
36
- * Identify a user
37
- */
38
- public identify(identity: UserIdentity): void {
39
- this.identity.identify(identity);
40
- }
41
-
42
- /**
43
- * Track a page view event
44
- */
45
- public trackPageView(): void {
46
- this.events.trackPageView();
47
- }
48
-
49
- /**
50
- * Track a custom event
51
- */
52
- public trackEvent(name: string, properties: Record<string, any> = {}): void {
53
- this.events.trackCustomEvent(name, properties);
54
- }
55
-
56
- /**
57
- * Track a purchase event
58
- */
59
- public trackPurchase(params: {
60
- productId: string;
61
- price: number;
62
- currency: string;
63
- properties?: Record<string, any>;
64
- }): void {
65
- this.events.trackPurchase(params);
66
- }
67
- }
68
-
69
- // Export types
70
- export * from './schemas/events';
71
- export * from './schemas/config';
72
- export * from './schemas/identity';
package/src/pixel.ts DELETED
@@ -1,28 +0,0 @@
1
- import { UserPath } from './index';
2
-
3
- const isBrowser = typeof window !== 'undefined';
4
-
5
- document.addEventListener('DOMContentLoaded', () => {
6
- if (!isBrowser) return;
7
- try {
8
- const siteId =
9
- document!.querySelector('[data-up]')!.getAttribute('data-up') || '';
10
- const src = document!.querySelector('[data-up]')!.getAttribute('src') || '';
11
- const url = new URL(src);
12
- const baseUrl = url.origin + '/v1';
13
-
14
- const userpath = new UserPath({
15
- siteId,
16
- baseUrl,
17
- });
18
-
19
- // @ts-ignore
20
- window.userpath = userpath;
21
-
22
- setTimeout(() => {
23
- console.log(siteId, baseUrl, userpath);
24
- }, 1000);
25
- } catch (err) {
26
- console.error(err);
27
- }
28
- });
@@ -1,4 +0,0 @@
1
- export interface Config {
2
- siteId: string;
3
- baseUrl?: string;
4
- }
@@ -1,31 +0,0 @@
1
- export interface PageViewEvent {
2
- type: 'pageview';
3
- url: string;
4
- referrer: string;
5
- title: string;
6
- }
7
-
8
- export interface CustomEvent {
9
- type: 'event';
10
- name: string;
11
- properties: Record<string, any>;
12
- }
13
-
14
- export interface PurchaseEvent {
15
- type: 'purchase';
16
- productId: string;
17
- price: number;
18
- currency: string;
19
- properties?: Record<string, any>;
20
- }
21
-
22
- export interface IdentifyEvent {
23
- type: 'identify';
24
- anonymousId: string;
25
- userId: string;
26
- email?: string;
27
- name?: string;
28
- properties?: Record<string, any>;
29
- }
30
-
31
- export type Event = PageViewEvent | CustomEvent | PurchaseEvent | IdentifyEvent;
@@ -1,6 +0,0 @@
1
- export interface UserIdentity {
2
- userId: string;
3
- email?: string;
4
- name?: string;
5
- properties?: Record<string, any>;
6
- }