private async Task <bool> IdentifyAsync(AmplitudeIdentify amplitudeIdentify)
        {
            string identification = JsonConvert.SerializeObject(amplitudeIdentify.identification, Formatting.Indented, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }).ToLower();

            try
            {
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.amplitude.com/identify"))
                    {
                        var contentList = new List <string>();
                        contentList.Add($"api_key={amplitudeIdentify.Api_key}");
                        contentList.Add($"identification=[{identification}]");
                        request.Content = new StringContent(string.Join("&", contentList));
                        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

                        var postResult = await httpClient.SendAsync(request);

                        return(postResult.IsSuccessStatusCode);
                    }
                }
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set user and device identification parameters.
        /// </summary>
        public void Identify(UserProperties user, DeviceProperties device)
        {
            AmplitudeIdentify identify = new AmplitudeIdentify();

            foreach (var extraProps in new[] { user.ExtraProperties, device.ExtraProperties })
            {
                foreach (var value in extraProps)
                {
                    identify.UserProperties[value.Key] = value.Value;
                }
            }

            foreach (var obj in new object[] { user, device })
            {
                foreach (PropertyInfo property in obj.GetType().GetRuntimeProperties())
                {
                    object value = property.GetValue(obj);
                    if ((value != null) &&
                        (property.GetCustomAttribute <JsonIgnoreAttribute>() == null))
                    {
                        var    jsonProp = property.GetCustomAttribute <JsonPropertyAttribute>();
                        string name     = jsonProp?.PropertyName ?? property.Name;

                        identify[name] = value;
                    }
                }
            }

            identification = identify;
            QueueEvent(identify);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The background thread for uploading events
        /// </summary>
        private async void UploadThread()
        {
            const int MaxEventBatch = 10;

            try
            {
                while (true)
                {
                    await eventsReady.WaitAsync(cancellationToken.Token);

                    List <AmplitudeEvent> eventsToSend   = new List <AmplitudeEvent>();
                    AmplitudeIdentify     identification = null;
                    bool backOff = false;

                    lock (lockObject)
                    {
                        foreach (IAmplitudeEvent e in eventQueue)
                        {
                            identification = e as AmplitudeIdentify;

                            if ((identification != null) || (eventsToSend.Count >= MaxEventBatch))
                            {
                                break;
                            }

                            eventsToSend.Add((AmplitudeEvent)e);
                        }
                    }

                    if (eventsToSend.Count > 0)
                    {
                        AmplitudeApi.SendResult result = await api.SendEvents(eventsToSend);

                        if (result == AmplitudeApi.SendResult.Success || result == AmplitudeApi.SendResult.ServerError)
                        {
                            // Remove these events from the list:
                            lock (lockObject)
                            {
                                eventQueue.RemoveRange(0, eventsToSend.Count);
                            }
                        }
                        else
                        {
                            // If we failed to send events don't sent the identification:
                            identification = null;
                            backOff        = true;
                        }
                    }

                    if (identification != null)
                    {
                        AmplitudeApi.SendResult result = await api.Identify(identification);

                        if (result == AmplitudeApi.SendResult.Success || result == AmplitudeApi.SendResult.ServerError)
                        {
                            // Remove this identify call from the list
                            lock (lockObject)
                            {
                                eventQueue.RemoveRange(0, 1);
                            }
                        }
                        else
                        {
                            backOff = true;
                        }
                    }

                    if (backOff)
                    {
                        await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken.Token);
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                // No matter what exception happens, we just quit
                s_logger(LogLevel.Error, "Upload thread terminated with: " + e.ToString());
            }
        }
 public async Task <bool> Identify(string userid, object userProperties)
 {
     identification = new AmplitudeIdentify(API_Key, userid, userProperties);
     return(await IdentifyAsync(identification));
 }