private static void RunBackgroundThread() { _isBgThreadRunning = true; while (!_stopThread) { try { DispatchOperations(); } catch (Exception e) { Mixpanel.LogError(e.ToString()); } } _isBgThreadRunning = false; }
internal static IEnumerator SendData(Queue <Value> queue, string url) { if (queue.Count == 0) { yield break; } int depth = queue.Count; int numBatches = (depth / Config.BatchSize) + (depth % Config.BatchSize != 0 ? 1 : 0); for (int i = 0; i < numBatches; i++) { if (_stopThread) { yield break; } Value batch = Value.Array; int count = 0; while (count < Config.BatchSize && queue.Count > 0) { Value data = queue.Dequeue(); if (data == null) { break; } try { batch.Add(data); } catch (Exception e) { Mixpanel.LogError($"There was an error processing event [{count}] from the internal object pool: " + e); } ++count; } if (count == 0) { yield break; } string payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(batch.ToString())); Mixpanel.Log($"Sending Request - '{url}' with payload '{payload}'"); bool successful = false; int responseCode = -1; string response = null; if (_isBgThreadRunning) { try { var content = new StringContent("data=" + payload, Encoding.UTF8, "application/json"); var responseRequest = _client.PostAsync(url, content).Result; responseCode = (int)responseRequest.StatusCode; response = responseRequest.Content.ReadAsStringAsync().Result; } catch (Exception e) { Mixpanel.LogError("There was an error sending the request: " + e); } } else { WWWForm form = new WWWForm(); form.AddField("data", payload); UnityWebRequest request = UnityWebRequest.Post(url, form); yield return(request.SendWebRequest()); while (!request.isDone) { yield return(new WaitForEndOfFrame()); } responseCode = (int)request.responseCode; response = request.downloadHandler.text; } Mixpanel.Log($"Response - '{url}' was '{response}'"); successful = responseCode == (int)HttpStatusCode.OK; if (successful) { _retryCount = 0; } else { _retryCount += 1; double retryIn = Math.Pow(2, _retryCount) * 60000; retryIn = Math.Min(retryIn, 10 * 60 * 1000); // limit 10 min Mixpanel.Log("Retrying request in " + retryIn / 1000 + " seconds (retryCount=" + _retryCount + ")"); _retryTimer = new System.Threading.Timer((obj) => { ForceFlushOp(); _retryTimer.Dispose(); }, null, (int)retryIn, System.Threading.Timeout.Infinite); yield break; } } }
private void MigrateFrom1To2() { if (!MixpanelStorage.HasMigratedFrom1To2) { string stateFile = Application.persistentDataPath + "/mp_state.json"; try { if (System.IO.File.Exists(stateFile)) { string state = System.IO.File.ReadAllText(stateFile); Value stateValue = Value.Deserialize(state); string distinctIdKey = "distinct_id"; if (stateValue.ContainsKey(distinctIdKey) && !stateValue[distinctIdKey].IsNull) { string distinctId = stateValue[distinctIdKey]; MixpanelStorage.DistinctId = distinctId; } string optedOutKey = "opted_out"; if (stateValue.ContainsKey(optedOutKey) && !stateValue[optedOutKey].IsNull) { bool optedOut = stateValue[optedOutKey]; MixpanelStorage.IsTracking = !optedOut; } string trackedIntegrationKey = "tracked_integration"; if (stateValue.ContainsKey(trackedIntegrationKey) && !stateValue[trackedIntegrationKey].IsNull) { bool trackedIntegration = stateValue[trackedIntegrationKey]; MixpanelStorage.HasIntegratedLibrary = trackedIntegration; } } } catch (Exception) { Mixpanel.LogError("Error migrating state from v1 to v2"); } finally { System.IO.File.Delete(stateFile); } string superPropertiesFile = Application.persistentDataPath + "/mp_super_properties.json"; try { if (System.IO.File.Exists(superPropertiesFile)) { string superProperties = System.IO.File.ReadAllText(superPropertiesFile); Value superPropertiesValue = Value.Deserialize(superProperties); foreach (KeyValuePair <string, Value> kvp in superPropertiesValue) { if (!kvp.Key.StartsWith("$")) { Mixpanel.Register(kvp.Key, kvp.Value); } } } } catch (Exception) { Mixpanel.LogError("Error migrating super properties from v1 to v2"); } finally { System.IO.File.Delete(superPropertiesFile); } MixpanelStorage.HasMigratedFrom1To2 = true; } }