@@ -4,7 +4,6 @@ import Analytics from 'analytics-node';
44import bodyParser from 'body-parser' ;
55import cors from 'cors' ;
66import express from 'express' ;
7- import { v4 as uuidv4 } from 'uuid' ;
87import { rateLimit } from 'express-rate-limit' ;
98import helmet from 'helmet' ;
109import { 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
230228app . 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