Skip to content

Commit c8c8000

Browse files
authored
Fix analytics endpoint (#1119)
* fix-analytics-endpoint * remove unused file * handle anon users
1 parent aab5602 commit c8c8000

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

packages/sdk-socket-server-next/src/analytics-api.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Analytics from 'analytics-node';
44
import bodyParser from 'body-parser';
55
import cors from 'cors';
66
import express from 'express';
7-
import { v4 as uuidv4 } from 'uuid';
87
import { rateLimit } from 'express-rate-limit';
98
import helmet from 'helmet';
109
import { Cluster, ClusterOptions, Redis, RedisOptions } from 'ioredis';
@@ -225,7 +224,6 @@ app.get('/', (req, res) => {
225224
res.json({ success: true });
226225
});
227226

228-
229227
// Redirect /debug to /evt for backwards compatibility
230228
app.post('/debug', (req, _res, next) => {
231229
req.url = '/evt'; // Redirect to /evt
@@ -258,16 +256,21 @@ app.post('/evt', evtMetricsMiddleware, async (_req, res) => {
258256
return res.status(400).json({ status: 'error' });
259257
}
260258

259+
let isAnonUser = false;
260+
261261
if (channelId === 'sdk') {
262-
channelId = uuidv4();
262+
isAnonUser = true;
263263
isExtensionEvent = true;
264264
}
265265

266266
logger.debug(
267267
`Received event /evt channelId=${channelId} isExtensionEvent=${isExtensionEvent}`,
268268
body,
269269
);
270-
let userIdHash = await pubClient.get(channelId);
270+
271+
let userIdHash = isAnonUser
272+
? crypto.createHash('sha1').update(channelId).digest('hex')
273+
: await pubClient.get(channelId);
271274

272275
incrementRedisCacheOperation('analytics-get-channel-id', !!userIdHash);
273276

@@ -292,7 +295,9 @@ app.post('/evt', evtMetricsMiddleware, async (_req, res) => {
292295
}
293296

294297
let channelInfo: ChannelInfo | null;
295-
const cachedChannelInfo = await pubClient.get(userIdHash);
298+
const cachedChannelInfo = isAnonUser
299+
? null
300+
: await pubClient.get(userIdHash);
296301

297302
incrementRedisCacheOperation(
298303
'analytics-get-channel-info',
@@ -353,16 +358,6 @@ app.post('/evt', evtMetricsMiddleware, async (_req, res) => {
353358
},
354359
};
355360

356-
// Always check for userId to avoid hot sharding events
357-
if (!event.userId || event.userId === SDK_EXTENSION_DEFAULT_ID) {
358-
const newUserId = uuidv4();
359-
logger.debug(
360-
`event: ${event.event} - Replacing 'sdk' id with '${newUserId}'`,
361-
event,
362-
);
363-
event.userId = newUserId;
364-
}
365-
366361
if (!event.properties.dappId) {
367362
// Prevent "N/A" in url and ensure a valid dappId
368363
const newDappId =
@@ -395,22 +390,21 @@ app.post('/evt', evtMetricsMiddleware, async (_req, res) => {
395390

396391
incrementAnalyticsEvents(
397392
body.from,
398-
channelId === 'sdk',
393+
!isAnonUser,
399394
event.event,
400395
body.platform,
401396
body.sdkVersion,
402397
);
403398

404399
analytics.track(event, function (err: Error) {
405-
incrementAnalyticsError('SegmentError');
406-
407400
if (EVENTS_DEBUG_LOGS) {
408401
logger.info('Segment batch', JSON.stringify({ event }, null, 2));
409402
} else {
410403
logger.info('Segment batch', { event });
411404
}
412405

413406
if (err) {
407+
incrementAnalyticsError('SegmentError');
414408
logger.error('Segment error:', err);
415409
}
416410
});

0 commit comments

Comments
 (0)