1+ using System . Globalization ;
2+ using BMM . Api . Framework ;
3+ using BMM . Api . Implementation . Constants ;
4+ using BMM . Core . Constants ;
5+ using BMM . Core . Extensions ;
6+ using BMM . Core . Helpers ;
7+ using BMM . Core . Implementations . FirebaseRemoteConfig ;
8+ using BMM . Core . Implementations . Security ;
9+ using BMM . Core . Utils ;
10+ using RudderStack ;
11+
12+ namespace BMM . Core . Implementations . Analytics
13+ {
14+ public class RudderStackAnalytics : IAnalytics
15+ {
16+ private readonly ILogger _logger ;
17+ private readonly IUserStorage _userStorage ;
18+ private readonly IFirebaseRemoteConfig _remoteConfig ;
19+ private readonly IConnection _connection ;
20+ private static bool _isInitialized = false ;
21+
22+ public RudderStackAnalytics (
23+ ILogger logger ,
24+ IUserStorage userStorage ,
25+ IFirebaseRemoteConfig remoteConfig ,
26+ IConnection connection )
27+ {
28+ _logger = logger ;
29+ _userStorage = userStorage ;
30+ _remoteConfig = remoteConfig ;
31+ _connection = connection ;
32+
33+ InitializeIfNeeded ( ) ;
34+ }
35+
36+ private void InitializeIfNeeded ( )
37+ {
38+ if ( _isInitialized ||
39+ GlobalConstants . RudderStackWriteKey . Contains ( GlobalConstants . Placeholder ) ||
40+ GlobalConstants . RudderStackDataPlaneUrl . Contains ( GlobalConstants . Placeholder ) )
41+ return ;
42+
43+ try
44+ {
45+ var config = new RudderConfig ( dataPlaneUrl : GlobalConstants . RudderStackDataPlaneUrl , async: true ) ;
46+ RudderAnalytics . Initialize ( GlobalConstants . RudderStackWriteKey , config ) ;
47+ _isInitialized = true ;
48+ _logger . Info ( "RudderStack" , "RudderStack Analytics initialized successfully" ) ;
49+ }
50+ catch ( Exception ex )
51+ {
52+ _logger . Error ( "RudderStack" , $ "Failed to initialize RudderStack: { ex . Message } ") ;
53+ }
54+ }
55+
56+ public void LogEvent ( string eventName )
57+ {
58+ LogEvent ( eventName , new Dictionary < string , object > ( ) ) ;
59+ }
60+
61+ public void LogEvent ( string eventName , IDictionary < string , object > parameters )
62+ {
63+ if ( ! _isInitialized )
64+ {
65+ _logger . Warn ( "RudderStack" , "RudderStack not initialized, skipping event" ) ;
66+ return ;
67+ }
68+
69+ try
70+ {
71+ var user = _userStorage . GetUser ( ) ;
72+ if ( user != null )
73+ {
74+ parameters . AddIfNew ( nameof ( user . AnalyticsId ) , user . AnalyticsId ) ;
75+
76+ if ( user . Age != null )
77+ parameters . Add ( nameof ( user . Age ) , user . Age ) ;
78+ }
79+
80+ if ( ! string . IsNullOrEmpty ( _remoteConfig . ExperimentId ) )
81+ parameters . AddIfNew ( HeaderNames . ExperimentId , _remoteConfig . ExperimentId ) ;
82+
83+ parameters . Add ( AnalyticsConstants . ConnectionParameterName , AnalyticsUtils . GetConnectionType ( _connection ) ) ;
84+
85+ var dString = parameters . ToDictionary (
86+ k => k . Key ,
87+ k =>
88+ {
89+ return k . Value switch
90+ {
91+ null => "null" ,
92+ IFormattable formattable => formattable . ToString ( null , CultureInfo . InvariantCulture ) ,
93+ _ => k . Value . ToString ( )
94+ } ;
95+ } ) ;
96+
97+ _logger . Info ( "Analytics" , eventName + " {" + string . Join ( "," , dString . Select ( kv => $ "{ kv . Key } ={ kv . Value } ") ) + "}" ) ;
98+
99+ // Get the user ID for RudderStack tracking
100+ var userId = user ? . AnalyticsId ? . ToString ( ) ?? "anonymous" ;
101+
102+ RudderAnalytics . Client . Track ( userId , eventName , parameters ) ;
103+ }
104+ catch ( Exception ex )
105+ {
106+ _logger . Error ( "RudderStack" , $ "Failed to log event '{ eventName } ': { ex . Message } ") ;
107+ }
108+ }
109+ }
110+ }
0 commit comments