clarity-analytics-sdk 1.0.5 → 1.0.6
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/package.json +1 -1
- package/src/index.js +41 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -138,7 +138,8 @@
|
|
|
138
138
|
* @param {Object} properties - Event properties
|
|
139
139
|
* @param {Object} options - Additional options
|
|
140
140
|
*/
|
|
141
|
-
async track (eventName, properties = {}, options = {}) {
|
|
141
|
+
async track (eventName, properties = {}, options = {}) {
|
|
142
|
+
// Wait for SDK to be initialized
|
|
142
143
|
if (!this._isInitialized) {
|
|
143
144
|
try {
|
|
144
145
|
await this._initializationPromise
|
|
@@ -156,6 +157,44 @@
|
|
|
156
157
|
throw new Error('Event name is required')
|
|
157
158
|
}
|
|
158
159
|
|
|
160
|
+
// Validate event name length
|
|
161
|
+
if (typeof eventName !== 'string') {
|
|
162
|
+
throw new Error('Event name must be a string')
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const eventNameLength = eventName.trim().length
|
|
166
|
+
if (eventNameLength < 5 || eventNameLength > 50) {
|
|
167
|
+
throw new Error('Event name must be between 5 and 50 characters')
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Validate properties is an object
|
|
171
|
+
if (properties !== null && (typeof properties !== 'object' || Array.isArray(properties))) {
|
|
172
|
+
throw new Error('Properties must be an object')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Validate properties size (max 16KB)
|
|
176
|
+
if (properties && typeof properties === 'object') {
|
|
177
|
+
const propertiesJson = JSON.stringify(properties)
|
|
178
|
+
let propertiesSize
|
|
179
|
+
|
|
180
|
+
// Calculate size in bytes (works in both browser and Node.js)
|
|
181
|
+
if (typeof Blob !== 'undefined') {
|
|
182
|
+
propertiesSize = new Blob([propertiesJson]).size
|
|
183
|
+
} else if (typeof TextEncoder !== 'undefined') {
|
|
184
|
+
propertiesSize = new TextEncoder().encode(propertiesJson).length
|
|
185
|
+
} else if (typeof Buffer !== 'undefined') {
|
|
186
|
+
propertiesSize = Buffer.byteLength(propertiesJson, 'utf8')
|
|
187
|
+
} else {
|
|
188
|
+
// Fallback: approximate size using string length (UTF-8)
|
|
189
|
+
propertiesSize = propertiesJson.length
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const maxSize = 16 * 1024 // 16KB in bytes
|
|
193
|
+
if (propertiesSize > maxSize) {
|
|
194
|
+
throw new Error(`Properties object size (${propertiesSize} bytes) exceeds maximum allowed size of 16KB`)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
159
198
|
const eventData = this._buildEventPayload({
|
|
160
199
|
eventType: options.eventType || 'custom',
|
|
161
200
|
eventName,
|
|
@@ -190,7 +229,7 @@
|
|
|
190
229
|
event_id: eventId,
|
|
191
230
|
request_id: requestId,
|
|
192
231
|
event_name: data.eventName || 'Unnamed Event',
|
|
193
|
-
timestamp
|
|
232
|
+
timestamp,
|
|
194
233
|
|
|
195
234
|
// User and session
|
|
196
235
|
user_id: finalUserId,
|