Exemplo n.º 1
0
        private bool SaveFileToSend(FileSystemInfo file)
        {
            FileProcessInfo fileProcessInfo = null;

            // the file is being processed or has been processed
            if (_queue.ContainsKey(FilePrefix + file.Name))
            {
                fileProcessInfo = _queue[FilePrefix + file.Name];
            }

            // the file is new
            if (fileProcessInfo == null)
            {
                fileProcessInfo = new FileProcessInfo {
                    Status        = FileProcessStatus.InProcess,
                    NumberOfTries = 0
                };
                _queue.TryAdd(FilePrefix + file.Name, fileProcessInfo);
            }
            else if (fileProcessInfo.Status == FileProcessStatus.InProcess)
            {
                AnalyticsLog.Log(TAG, "The file '" + file.Name + "' is being processed");
                return(false);
            }
            else if (fileProcessInfo.NextProcessDate != null && fileProcessInfo.NextProcessDate > DateTime.Now)
            {
                AnalyticsLog.Log(TAG, "Too early to reprocess the file '" + file.Name + "'");
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        internal static void SendEvents(List <string> events, Action <bool> complete)
        {
            AnalyticsLog.Log(TAG, "Send " + events.Count + " event(s)");

            var request = new UnityWebRequest(AnalyticsGatewayUrl, "POST");

            byte[] bodyRaw = Encoding.UTF8.GetBytes("[" + string.Join(",", events) + "]");
            request.uploadHandler   = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");

            UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();

            asyncOperation.completed += operation => {
                if (!request.isNetworkError && !request.isHttpError)
                {
                    AnalyticsLog.Log(TAG, "Successfully pushed " + events.Count + " events");
                    complete(true);
                }
                else
                {
                    complete(false);
                    AnalyticsLog.Log(TAG, "Error when sending events: " + request.error);
                }

                request.Dispose();
            };
        }
Exemplo n.º 3
0
        private void RetrieveAndSendEvents()
        {
            AnalyticsLog.Log(TAG, "Retrieve events");

            var info = new DirectoryInfo(PersistenceFolder);

            FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTimeUtc).ToArray();

            if (files.Length > 0)
            {
                UpdateCurrentEventsFileName();
            }

            foreach (FileInfo file in files)
            {
                if (!SaveFileToSend(file))
                {
                    continue;
                }

                var events = new List <string>();
                lock (FileAccess) {
                    AnalyticsLog.Log(TAG, "Found '" + file.Name + "' File");
                    using (StreamReader streamReader = File.OpenText(file.FullName)) {
                        string   jsonString      = streamReader.ReadToEnd();
                        string[] jsonStringArray = jsonString.Split('\n');
                        events.AddRange(jsonStringArray.Where(value => value != ""));
                    }
                }

                SendAndDeleteFiles(events, file);
            }
        }
Exemplo n.º 4
0
        internal async Task TrackEvent(Event e)
        {
            string[] enabledEvents = _config.EnabledEvents();
            if (enabledEvents.Length > 0 && !enabledEvents.Contains(e.GetName()))
            {
                return;
            }

            AnalyticsLog.Log(TAG, "Track event: " + e);
            await Task.Run(() => {
                lock (FileAccess) {
                    string jsonString = e.ToJson();
                    AnalyticsLog.Log(TAG, "Track event: " + jsonString);
                    using (StreamWriter streamWriter = File.AppendText(_lastFilePath)) {
                        streamWriter.Write(jsonString + "\n");
                    }

                    _eventNumberInFile++;

                    if (_eventNumberInFile > _config.GetMaxNumberOfEventsPerFile())
                    {
                        UpdateCurrentEventsFileName();
                    }
                }
            });
        }
Exemplo n.º 5
0
        private static void InternalTrackEvent(string eventName,
                                               string dataJson,
                                               string eventType,
                                               string customVariablesJson)
        {
            if (!_isInitialized)
            {
                var queuedEvent = new QueuedEvent {
                    EventName                = eventName,
                    EventDataJson            = dataJson,
                    EventType                = eventType,
                    EventCustomVariablesJson = customVariablesJson
                };
                AnalyticsLog.Log(TAG, "Add event " + eventName + " to the queue (" + dataJson + ")");
                QueuedEvents.Add(queuedEvent);
                return;
            }

            AnalyticsSessionHelper.DefaultHelper().OnNewEvent();

            AnalyticsLog.Log(TAG, "Create event " + eventName + " (" + dataJson + ")");
            Event.Create(eventName,
                         AnalyticsParameters,
                         dataJson,
                         customVariablesJson,
                         eventType,
                         AnalyticsSessionHelper.DefaultHelper().SessionId,
                         AnalyticsSessionHelper.DefaultHelper().SessionLength,
                         AnalyticsSessionHelper.DefaultHelper().SessionCount,
                         async e => { await Tracker.Instance.TrackEvent(e); });
        }
Exemplo n.º 6
0
        private void SendAndDeleteFiles(List <string> events, FileSystemInfo file)
        {
            if (events.Count == 0)
            {
                return;
            }

            AnalyticsLog.Log(TAG, "Send and delete file '" + file.Name + "' File");

            AnalyticsApi.SendEvents(events, succeeded => {
                if (succeeded)
                {
                    lock (FileAccess) {
                        AnalyticsLog.Log(TAG, "Delete file: '" + file.Name + "' File");

                        if (_queue.ContainsKey(FilePrefix + file.Name))
                        {
                            FileProcessInfo info;
                            _queue.TryRemove(FilePrefix + file.Name, out info);
                        }

                        file.Delete();
                    }
                }
                else if (_queue.ContainsKey(FilePrefix + file.Name))
                {
                    FileProcessInfo fileProcessInfo = _queue[FilePrefix + file.Name];
                    fileProcessInfo.Status          = FileProcessStatus.Waiting;
                    fileProcessInfo.NumberOfTries++;
                    int delay = _backOffDelays.Last();
                    if (fileProcessInfo.NumberOfTries < _backOffDelays.Length)
                    {
                        delay = _backOffDelays[fileProcessInfo.NumberOfTries];
                    }

                    AnalyticsLog.Log(TAG, "Retry pushing '" + file.Name + "' file in " + delay + " seconds");

                    fileProcessInfo.NextProcessDate = DateTime.Now.AddSeconds(delay);
                }
            });
        }
Exemplo n.º 7
0
 private void UpdateCurrentEventsFileName()
 {
     AnalyticsLog.Log(TAG, "Events file name updated");
     _lastFilePath      = PersistenceFolder + "events_" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + ".jsonl";
     _eventNumberInFile = 0;
 }