Beispiel #1
0
 public static void EndSessionAndStopQueue()
 {
     GAThreading.IgnoreTimer(Instance.SuspendBlockId);
     if (Initialized)
     {
         GALogger.I("Ending session.");
         GAEvents.StopEventQueue();
         if (IsEnabled() && SessionIsStarted())
         {
             GAEvents.AddSessionEndEvent();
             SessionStart = 0;
         }
         GAThreading.StopThread();
     }
 }
Beispiel #2
0
        public static void EndSessionAndStopQueue(bool endThread)
        {
            if (Initialized)
            {
                if (IsEnabled() && SessionIsStarted())
                {
                    GALogger.I("Ending session.");
                    GAEvents.StopEventQueue();
                    GAEvents.AddSessionEndEvent();
                    SessionStart = 0;
                }
            }

            if (endThread)
            {
                GAThreading.StopThread();
            }
        }
Beispiel #3
0
        public static void StartNewSession(EGAHTTPApiResponse initResponse, JSONObject initResponseDict)
        {
            // init is ok
            if (initResponse == EGAHTTPApiResponse.Ok && initResponseDict != null)
            {
                // set the time offset - how many seconds the local time is different from servertime
                long timeOffsetSeconds = 0;
                if (initResponseDict["server_ts"] != null)
                {
                    long serverTs = initResponseDict["server_ts"].AsLong;
                    timeOffsetSeconds = CalculateServerTimeOffset(serverTs);
                }
                initResponseDict.Add("time_offset", new JSONNumber(timeOffsetSeconds));

                // insert new config in sql lite cross session storage
                GAStore.SetState(SdkConfigCachedKey, initResponseDict.SaveToBinaryBase64());

                // set new config and cache in memory
                Instance.sdkConfigCached = initResponseDict;
                Instance.sdkConfig       = initResponseDict;

                Instance.InitAuthorized = true;
            }
            else if (initResponse == EGAHTTPApiResponse.Unauthorized)
            {
                GALogger.W("Initialize SDK failed - Unauthorized");
                Instance.InitAuthorized = false;
            }
            else
            {
                // log the status if no connection
                if (initResponse == EGAHTTPApiResponse.NoResponse || initResponse == EGAHTTPApiResponse.RequestTimeout)
                {
                    GALogger.I("Init call (session start) failed - no response. Could be offline or timeout.");
                }
                else if (initResponse == EGAHTTPApiResponse.BadResponse || initResponse == EGAHTTPApiResponse.JsonEncodeFailed || initResponse == EGAHTTPApiResponse.JsonDecodeFailed)
                {
                    GALogger.I("Init call (session start) failed - bad response. Could be bad response from proxy or GA servers.");
                }
                else if (initResponse == EGAHTTPApiResponse.BadRequest || initResponse == EGAHTTPApiResponse.UnknownResponseCode)
                {
                    GALogger.I("Init call (session start) failed - bad request or unknown response.");
                }

                // init call failed (perhaps offline)
                if (Instance.sdkConfig == null)
                {
                    if (Instance.sdkConfigCached != null)
                    {
                        GALogger.I("Init call (session start) failed - using cached init values.");
                        // set last cross session stored config init values
                        Instance.sdkConfig = Instance.sdkConfigCached;
                    }
                    else
                    {
                        GALogger.I("Init call (session start) failed - using default init values.");
                        // set default init values
                        Instance.sdkConfig = Instance.sdkConfigDefault;
                    }
                }
                else
                {
                    GALogger.I("Init call (session start) failed - using cached init values.");
                }
                Instance.InitAuthorized = true;
            }

            {
                JSONNode currentSdkConfig = SdkConfig;

                if (currentSdkConfig["enabled"].IsBoolean && !currentSdkConfig["enabled"].AsBool)
                {
                    Instance.Enabled = false;
                }
                else if (!Instance.InitAuthorized)
                {
                    Instance.Enabled = false;
                }
                else
                {
                    Instance.Enabled = true;
                }
            }

            // set offset in state (memory) from current config (config could be from cache etc.)
            Instance.ClientServerTimeOffset = SdkConfig["time_offset"] != null ? SdkConfig["time_offset"].AsLong : 0;

            // populate configurations
            PopulateConfigurations(SdkConfig);

            // if SDK is disabled in config
            if (!IsEnabled())
            {
                GALogger.W("Could not start session: SDK is disabled.");
                // stop event queue
                // + make sure it's able to restart if another session detects it's enabled again
                GAEvents.StopEventQueue();
                return;
            }
            else
            {
                GAEvents.EnsureEventQueueIsRunning();
            }

            // generate the new session
            string newSessionId          = Guid.NewGuid().ToString();
            string newSessionIdLowercase = newSessionId.ToLowerInvariant();

            // Set session id
            SessionId = newSessionIdLowercase;

            // Set session start
            SessionStart = GetClientTsAdjusted();

            // Add session start event
            GAEvents.AddSessionStartEvent();
        }