@datalyr/api 1.2.2 → 1.2.3

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 CHANGED
@@ -112,6 +112,22 @@ await datalyr.page('user_123', 'Pricing', {
112
112
  });
113
113
  ```
114
114
 
115
+ ### alias()
116
+
117
+ ```javascript
118
+ await datalyr.alias(newUserId: string, previousId?: string);
119
+ ```
120
+
121
+ Link a new user ID to a previous one (e.g., after account merge). If `previousId` is omitted, the current anonymous ID is used. Internally sends a `$alias` event.
122
+
123
+ ```javascript
124
+ // Link new ID to the current anonymous user
125
+ await datalyr.alias('new_user_456');
126
+
127
+ // Or specify the previous ID explicitly
128
+ await datalyr.alias('new_user_456', 'old_user_123');
129
+ ```
130
+
115
131
  ### group()
116
132
 
117
133
  ```javascript
package/dist/index.d.mts CHANGED
@@ -11,6 +11,7 @@ interface DatalyrConfig {
11
11
  interface TrackEvent {
12
12
  userId?: string;
13
13
  anonymousId?: string;
14
+ eventId?: string;
14
15
  event: string;
15
16
  properties?: Record<string, any>;
16
17
  context?: Record<string, any>;
@@ -40,6 +41,7 @@ declare class Datalyr {
40
41
  track(options: TrackOptions): Promise<void>;
41
42
  track(userId: string | null, event: string, properties?: any): Promise<void>;
42
43
  identify(userId: string, traits?: any): Promise<void>;
44
+ alias(newUserId: string, previousId?: string): Promise<void>;
43
45
  page(userId: string, name?: string, properties?: any): Promise<void>;
44
46
  group(userId: string, groupId: string, traits?: any): Promise<void>;
45
47
  private enqueue;
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ interface DatalyrConfig {
11
11
  interface TrackEvent {
12
12
  userId?: string;
13
13
  anonymousId?: string;
14
+ eventId?: string;
14
15
  event: string;
15
16
  properties?: Record<string, any>;
16
17
  context?: Record<string, any>;
@@ -40,6 +41,7 @@ declare class Datalyr {
40
41
  track(options: TrackOptions): Promise<void>;
41
42
  track(userId: string | null, event: string, properties?: any): Promise<void>;
42
43
  identify(userId: string, traits?: any): Promise<void>;
44
+ alias(newUserId: string, previousId?: string): Promise<void>;
43
45
  page(userId: string, name?: string, properties?: any): Promise<void>;
44
46
  group(userId: string, groupId: string, traits?: any): Promise<void>;
45
47
  private enqueue;
package/dist/index.js CHANGED
@@ -24,6 +24,13 @@ __export(index_exports, {
24
24
  default: () => index_default
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
+ function generateEventId() {
28
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
29
+ return crypto.randomUUID();
30
+ }
31
+ return "evt_" + Math.random().toString(36).substring(2) + Date.now().toString(36);
32
+ }
33
+ var SDK_VERSION = "1.2.3";
27
34
  var Datalyr = class {
28
35
  // Persistent anonymous ID for identity resolution
29
36
  constructor(config) {
@@ -96,13 +103,13 @@ var Datalyr = class {
96
103
  userId: userId || void 0,
97
104
  anonymousId,
98
105
  // Always include for identity resolution
106
+ eventId: generateEventId(),
99
107
  event: eventName,
100
108
  properties: enrichedProperties,
101
109
  context: {
102
110
  library: "@datalyr/api",
103
- version: "1.2.1",
111
+ version: SDK_VERSION,
104
112
  source: "api"
105
- // Explicitly set source for server-side API
106
113
  },
107
114
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
108
115
  };
@@ -113,7 +120,17 @@ var Datalyr = class {
113
120
  throw new Error("userId is required for identify");
114
121
  }
115
122
  return this.track(userId, "$identify", {
116
- $set: traits,
123
+ ...traits,
124
+ anonymous_id: this.getOrCreateAnonymousId()
125
+ });
126
+ }
127
+ async alias(newUserId, previousId) {
128
+ if (!newUserId) {
129
+ throw new Error("newUserId is required for alias");
130
+ }
131
+ return this.track(newUserId, "$alias", {
132
+ new_user_id: newUserId,
133
+ previous_id: previousId || this.getOrCreateAnonymousId(),
117
134
  anonymous_id: this.getOrCreateAnonymousId()
118
135
  });
119
136
  }
@@ -124,7 +141,7 @@ var Datalyr = class {
124
141
  if (!groupId) {
125
142
  throw new Error("groupId is required for group");
126
143
  }
127
- return this.track(userId, "$group", { groupId, traits });
144
+ return this.track(userId, "$group", { groupId, ...traits });
128
145
  }
129
146
  enqueue(event) {
130
147
  if (this.queue.length >= this.maxQueueSize) {
@@ -183,7 +200,8 @@ var Datalyr = class {
183
200
  method: "POST",
184
201
  headers: {
185
202
  "X-API-Key": this.apiKey,
186
- "Content-Type": "application/json"
203
+ "Content-Type": "application/json",
204
+ "User-Agent": `@datalyr/api/${SDK_VERSION}`
187
205
  },
188
206
  body: JSON.stringify(event),
189
207
  signal: controller.signal
package/dist/index.mjs CHANGED
@@ -1,4 +1,11 @@
1
1
  // src/index.ts
2
+ function generateEventId() {
3
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
4
+ return crypto.randomUUID();
5
+ }
6
+ return "evt_" + Math.random().toString(36).substring(2) + Date.now().toString(36);
7
+ }
8
+ var SDK_VERSION = "1.2.3";
2
9
  var Datalyr = class {
3
10
  // Persistent anonymous ID for identity resolution
4
11
  constructor(config) {
@@ -71,13 +78,13 @@ var Datalyr = class {
71
78
  userId: userId || void 0,
72
79
  anonymousId,
73
80
  // Always include for identity resolution
81
+ eventId: generateEventId(),
74
82
  event: eventName,
75
83
  properties: enrichedProperties,
76
84
  context: {
77
85
  library: "@datalyr/api",
78
- version: "1.2.1",
86
+ version: SDK_VERSION,
79
87
  source: "api"
80
- // Explicitly set source for server-side API
81
88
  },
82
89
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
83
90
  };
@@ -88,7 +95,17 @@ var Datalyr = class {
88
95
  throw new Error("userId is required for identify");
89
96
  }
90
97
  return this.track(userId, "$identify", {
91
- $set: traits,
98
+ ...traits,
99
+ anonymous_id: this.getOrCreateAnonymousId()
100
+ });
101
+ }
102
+ async alias(newUserId, previousId) {
103
+ if (!newUserId) {
104
+ throw new Error("newUserId is required for alias");
105
+ }
106
+ return this.track(newUserId, "$alias", {
107
+ new_user_id: newUserId,
108
+ previous_id: previousId || this.getOrCreateAnonymousId(),
92
109
  anonymous_id: this.getOrCreateAnonymousId()
93
110
  });
94
111
  }
@@ -99,7 +116,7 @@ var Datalyr = class {
99
116
  if (!groupId) {
100
117
  throw new Error("groupId is required for group");
101
118
  }
102
- return this.track(userId, "$group", { groupId, traits });
119
+ return this.track(userId, "$group", { groupId, ...traits });
103
120
  }
104
121
  enqueue(event) {
105
122
  if (this.queue.length >= this.maxQueueSize) {
@@ -158,7 +175,8 @@ var Datalyr = class {
158
175
  method: "POST",
159
176
  headers: {
160
177
  "X-API-Key": this.apiKey,
161
- "Content-Type": "application/json"
178
+ "Content-Type": "application/json",
179
+ "User-Agent": `@datalyr/api/${SDK_VERSION}`
162
180
  },
163
181
  body: JSON.stringify(event),
164
182
  signal: controller.signal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datalyr/api",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Datalyr API SDK for server-side tracking",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",