clarity-analytics-sdk 1.0.6 → 1.0.7

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +26 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-analytics-sdk",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "A lightweight JavaScript SDK for tracking events and analytics with Clarity Analytics",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -20,11 +20,9 @@
20
20
  apiUrl: 'https://apps.helo.ai/api/v1/sdk/clarity/events/publish-event',
21
21
  fetchUserIdUrl: 'https://apps.helo.ai/api/v1/sdk/clarity/projects?id=',
22
22
  projectId: config.projectId,
23
- projectName: config.projectName || '',
24
- apiKey: config.apiKey || null,
25
- userId: null, // Will be fetched from backend
26
- systemUserId: null, // Will be fetched from backend API
27
- sessionId: config.sessionId || this._generateSessionId(),
23
+ projectName: null,
24
+ userId: null, // customer: should come from app
25
+ systemUserId: null, // Will be fetched from backend API(user who logged in clarity)
28
26
  debug: config.debug || false,
29
27
  retryAttempts: config.retryAttempts || 3,
30
28
  timeout: config.timeout || 10000,
@@ -50,15 +48,20 @@
50
48
  async _initialize (config) {
51
49
  try {
52
50
  // Fetch user ID from backend
53
- const { userId, ipaddress } = await this._fetchUserId(this.config.projectId)
54
-
55
- if (!userId) {
51
+ const { systemUserId, ipaddress, projectName } = await this._fetchUserId(this.config.projectId)
52
+ if (!systemUserId) {
56
53
  throw new Error('Failed to fetch user ID from backend')
57
54
  }
58
55
 
59
- this.config.userId = userId
56
+ if (!projectName) {
57
+ throw new Error('Failed to fetch project name from backend111')
58
+ }
59
+
60
60
  this.config.ipaddress = ipaddress
61
- this.config.systemUserId = userId || '' // Set to empty string if not provided
61
+ this.config.systemUserId = systemUserId // Set to empty string if not provided
62
+ this.config.projectName = projectName
63
+ this.config.customerId = this._generateId()
64
+ this.config.sessionId = this._generateId()
62
65
  this._isInitialized = true
63
66
 
64
67
  // Auto-collect page view if enabled
@@ -111,21 +114,23 @@
111
114
  // Extract userId from response (supports multiple response structures)
112
115
  let userId = null
113
116
  let ipaddress = null
114
-
117
+ let projectName = null
115
118
  // Check if response has a 'data' wrapper
116
119
  if (responseData?.data) {
117
120
  userId = responseData?.data?.userId
118
121
  ipaddress = responseData?.data?.ipaddress
122
+ projectName = responseData?.data?.projectName
119
123
  } else {
120
124
  userId = responseData?.userId
121
125
  ipaddress = responseData?.ipaddress
126
+ projectName = responseData?.projectName
122
127
  }
123
128
 
124
129
  if (!userId) {
125
130
  throw new Error('User ID not found in API response')
126
131
  }
127
132
 
128
- return { userId, ipaddress }
133
+ return { systemUserId: userId, ipaddress, projectName }
129
134
  } catch (error) {
130
135
  clearTimeout(timeoutId)
131
136
  throw error
@@ -138,8 +143,7 @@
138
143
  * @param {Object} properties - Event properties
139
144
  * @param {Object} options - Additional options
140
145
  */
141
- async track (eventName, properties = {}, options = {}) {
142
- // Wait for SDK to be initialized
146
+ async track (eventName, properties = {}, options = {}) { // Wait for SDK to be initialized
143
147
  if (!this._isInitialized) {
144
148
  try {
145
149
  await this._initializationPromise
@@ -163,7 +167,7 @@
163
167
  }
164
168
 
165
169
  const eventNameLength = eventName.trim().length
166
- if (eventNameLength < 5 || eventNameLength > 50) {
170
+ if (eventNameLength < 4 || eventNameLength > 50) {
167
171
  throw new Error('Event name must be between 5 and 50 characters')
168
172
  }
169
173
 
@@ -200,11 +204,11 @@
200
204
  eventName,
201
205
  eventKey: options.eventKey || this._sanitizeEventName(eventName),
202
206
  properties,
203
- sessionId: properties.sessionId || options.sessionId,
207
+ sessionId: properties.sessionId || options.sessionId || this.config.sessionId,
204
208
  context: options.context || {},
205
209
  traits: options.traits || {},
206
- ipaddress: options.ipaddress || this.config.ipaddress,
207
- customId: properties.userId || options.userId
210
+ ipaddress: properties.ipaddress || options.ipaddress || this.config.ipaddress,
211
+ customerId: properties.userId || properties.customerId || options.userId || options.customerId || this.config.customerId,
208
212
  })
209
213
  return this._sendEvent(eventData)
210
214
  }
@@ -217,11 +221,10 @@
217
221
  const timestamp = Date.now()
218
222
  const eventId = this._generateUUID()
219
223
  const requestId = this._generateRequestId()
220
- const userId = data.customId || this.config.userId
224
+ const userId = data.customerId // actual customer id from app
221
225
 
222
226
  // system_user_id comes from API response only, default to empty string if not available
223
227
  const systemUserId = this.config.systemUserId || ''
224
- const finalUserId = userId
225
228
 
226
229
  // Build flat event payload (matching staging.js structure)
227
230
  return {
@@ -229,16 +232,16 @@
229
232
  event_id: eventId,
230
233
  request_id: requestId,
231
234
  event_name: data.eventName || 'Unnamed Event',
232
- timestamp,
235
+ timestamp: timestamp,
233
236
 
234
237
  // User and session
235
- user_id: finalUserId,
238
+ user_id: userId,
236
239
  session_id: data.sessionId || this.config.sessionId,
237
240
  system_user_id: systemUserId,
238
241
 
239
242
  // Project information
240
243
  project_id: this.config.projectId,
241
- project_name: this.config.projectName || '',
244
+ project_name: this.config.projectName,
242
245
 
243
246
  // Device information (flattened)
244
247
  device: this._deviceInfo.device.type,
@@ -314,7 +317,6 @@
314
317
  try {
315
318
  // Validate mandatory fields before sending
316
319
  this._validateMandatoryFields(eventData)
317
-
318
320
  const headers = {
319
321
  'Content-Type': 'application/json'
320
322
  }
@@ -534,14 +536,6 @@
534
536
  }
535
537
  }
536
538
 
537
- /**
538
- * Generate session ID
539
- * @private
540
- */
541
- _generateSessionId () {
542
- return 'sess_' + this._generateId()
543
- }
544
-
545
539
  /**
546
540
  * Generate UUID v4
547
541
  * @private