@@ -19,7 +19,7 @@ export class AnalyticsService {
1919 async recordClick ( event : UrlRedirectedEvent ) : Promise < void > {
2020 const urlId = event . urlId ;
2121 const req = event . req ;
22- const ip = ( req . headers [ 'x-forwarded-for' ] as string )
22+ const ipAddress = ( req . headers [ 'x-forwarded-for' ] as string )
2323 ?. split ( ',' ) [ 0 ]
2424 ?. trim ( ) ;
2525
@@ -30,25 +30,31 @@ export class AnalyticsService {
3030 }
3131 ) . parse ( userAgent ) ;
3232
33+ // Match the first section inside parentheses of the User-Agent string (up to the first semicolon).
34+ // This typically represents the device or platform, e.g. "Windows NT 10.0" or "iPhone".
3335 const deviceMatch = parsed . source . match ( / \( ( [ ^ ; ] + ) ; / ) ;
3436 const device = deviceMatch ? deviceMatch [ 1 ] : 'Unknown Device' ;
3537
38+ // Look for known browser names followed by a version number,
39+ // e.g. "Chrome/120.0", "Firefox/118.0".
3640 const browserMatch = parsed . source . match (
3741 / ( C h r o m e | F i r e f o x | S a f a r i | E d g e | O p e r a ) \/ [ \d . ] + / ,
3842 ) ;
3943 const browser = browserMatch ? browserMatch [ 0 ] : 'Unknown Browser' ;
4044
45+ // Match the substring inside parentheses that follows the first semicolon.
46+ // For example, from "(Windows NT 10.0; Win64; x64)" → captures "Win64; x64".
4147 const osMatch = parsed . source . match ( / \( (?: [ ^ ; ] + ) ; \s * ( [ ^ ) ] + ) \) / ) ;
4248
4349 const os = osMatch ? osMatch [ 1 ] : 'Unknown OS' ;
4450
45- const geo = geoip . lookup ( ip ) ;
51+ const geo = geoip . lookup ( ipAddress ) ;
4652 const country = geo ?. country || 'Unknown' ;
4753
4854 const analytics = this . analyticsRepo . create ( {
4955 urlId,
5056 os,
51- ip ,
57+ ipAddress ,
5258 browser : browser ,
5359 userAgent,
5460 device : device ,
@@ -70,9 +76,13 @@ export class AnalyticsService {
7076 start : requestData . startDate ,
7177 end : requestData . endDate ,
7278 } ) ;
73- } else if ( requestData . startDate ) {
79+ }
80+
81+ if ( requestData . startDate ) {
7482 qb . andWhere ( 'a.redirectedAt >= :start' , { start : requestData . startDate } ) ;
75- } else if ( requestData . endDate ) {
83+ }
84+
85+ if ( requestData . endDate ) {
7686 qb . andWhere ( 'a.redirectedAt <= :end' , { end : requestData . endDate } ) ;
7787 }
7888
0 commit comments