@@ -57,7 +57,7 @@ namespace gameanalytics
5757 return ;
5858 }
5959
60- getInstance ()._userId = id;
60+ getInstance ()._customUserId = id;
6161 getInstance ().cacheIdentifier ();
6262 }
6363
@@ -383,6 +383,7 @@ namespace gameanalytics
383383 logging::GALogger::i (" Ending session." );
384384 if (GAState::isEnabled () && GAState::sessionIsStarted ())
385385 {
386+ getInstance ().updateTotalSessionTime ();
386387 events::GAEvents::addHealthEvent ();
387388 events::GAEvents::addSessionEndEvent ();
388389 getInstance ()._sessionStart = 0 ;
@@ -505,17 +506,16 @@ namespace gameanalytics
505506
506507 void GAState::cacheIdentifier ()
507508 {
508- if (!_userId .empty ())
509+ if (!_customUserId .empty ())
509510 {
510- _identifier = _userId ;
511+ _identifier = _customUserId ;
511512 }
512513 else
513514 {
514515 _identifier = _defaultUserId;
515516 }
516517
517518 logging::GALogger::d (" identifier, {clean:%s}" , _identifier.c_str ());
518-
519519 }
520520
521521 std::string setStateFromCache (json& dict, std::string const & key, std::string const & value)
@@ -535,6 +535,11 @@ namespace gameanalytics
535535 return " " ;
536536 }
537537
538+ int64_t GAState::getTotalSessionLength () const
539+ {
540+ return _totalElapsedSessionTime + calculateSessionLength<std::chrono::seconds>();
541+ }
542+
538543 void GAState::ensurePersistedStates ()
539544 {
540545 try
@@ -555,6 +560,9 @@ namespace gameanalytics
555560 }
556561 }
557562 }
563+
564+ std::string s = state_dict.dump ();
565+ _gaLogger.d (" state_dict: %s" , s.c_str ());
558566
559567 // insert into GAState instance
560568 std::string defaultId = utilities::getOptionalValue<std::string>(state_dict, " default_user_id" );
@@ -575,6 +583,20 @@ namespace gameanalytics
575583 _currentCustomDimension01 = setStateFromCache (state_dict, " dimension01" , _currentCustomDimension01);
576584 _currentCustomDimension02 = setStateFromCache (state_dict, " dimension02" , _currentCustomDimension02);
577585 _currentCustomDimension03 = setStateFromCache (state_dict, " dimension03" , _currentCustomDimension03);
586+
587+ try
588+ {
589+ std::string cachedSessionTime = utilities::getOptionalValue<std::string>(state_dict, " total_session_time" , " 0" );
590+
591+ _totalElapsedSessionTime = std::stoull (cachedSessionTime);
592+
593+ }
594+ catch (const std::exception& e)
595+ {
596+ _gaLogger.w (" Failed to read total_session_time from cache!" );
597+ _totalElapsedSessionTime = 0 ;
598+ }
599+
578600
579601 // get cached init call values
580602 if (state_dict.contains (" sdk_config_cached" ) && state_dict[" sdk_config_cached" ].is_string ())
@@ -856,11 +878,6 @@ namespace gameanalytics
856878 return getInstance ()._remoteConfigsIsReady ;
857879 }
858880
859- int64_t GAState::calculateSessionLength () const
860- {
861- return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now () - _startTimepoint).count ();
862- }
863-
864881 void GAState::addRemoteConfigsListener (const std::shared_ptr<IRemoteConfigsListener>& listener)
865882 {
866883 if (std::find (getInstance ()._remoteConfigsListeners .begin (), getInstance ()._remoteConfigsListeners .end (), listener) == getInstance ()._remoteConfigsListeners .end ())
@@ -1043,6 +1060,12 @@ namespace gameanalytics
10431060 return utilities::GAUtilities::timeIntervalSince1970 ();
10441061 }
10451062
1063+ void GAState::updateTotalSessionTime ()
1064+ {
1065+ int64_t totalSessionTime = getTotalSessionLength ();
1066+ _gaStore.setState (" total_session_time" , std::to_string (totalSessionTime));
1067+ }
1068+
10461069 std::string GAState::getBuild ()
10471070 {
10481071 return getInstance ()._build ;
0 commit comments