diff --git a/AmplitudeSharp/AmplitudeService.cs b/AmplitudeSharp/AmplitudeService.cs index 87c2207..53f9c12 100644 --- a/AmplitudeSharp/AmplitudeService.cs +++ b/AmplitudeSharp/AmplitudeService.cs @@ -54,10 +54,10 @@ public bool OfflineMode /// public Dictionary ExtraEventProperties { get; private set; } = new Dictionary(); - private AmplitudeService(string apiKey) + private AmplitudeService(string apiKey, string apiRegion) { lockObject = new object(); - api = new AmplitudeApi(apiKey); + api = new AmplitudeApi(apiKey, apiRegion); eventQueue = new List(); cancellationToken = new CancellationTokenSource(); eventsReady = new SemaphoreSlim(0); @@ -70,28 +70,34 @@ private AmplitudeService(string apiKey) } public void Dispose() - { + { Uninitialize(); s_instance = null; } /// /// Initialize AmplitudeSharp - /// Takes an API key for the project and, optionally, + /// Takes an API key for the project and, optionally, /// a stream where offline/past events are stored /// /// api key for the project to stream data to + /// Amplitude region to use /// optinal, stream with saved event data /// Action delegate for logging purposes, if none is specified is used /// - public static AmplitudeService Initialize(string apiKey, Action logger = null, Stream persistenceStream = null) + public static AmplitudeService Initialize(string apiKey, string apiRegion = "us", Action logger = null, Stream persistenceStream = null) { if (apiKey == "") { throw new ArgumentOutOfRangeException(nameof(apiKey), "Please specify Amplitude API key"); } - AmplitudeService instance = new AmplitudeService(apiKey); + if (apiRegion == null) + { + throw new ArgumentOutOfRangeException(nameof(apiRegion), "Please specify the Amplitude region to use"); + } + + AmplitudeService instance = new AmplitudeService(apiKey, apiRegion); instance.NewSession(); if (Interlocked.CompareExchange(ref s_instance, instance, null) == null) diff --git a/AmplitudeSharp/Api/AmplitudeApi.cs b/AmplitudeSharp/Api/AmplitudeApi.cs index ae5a6ff..92036ce 100644 --- a/AmplitudeSharp/Api/AmplitudeApi.cs +++ b/AmplitudeSharp/Api/AmplitudeApi.cs @@ -27,12 +27,15 @@ public enum SendResult class AmplitudeApi : IAmplitudeApi { private string apiKey; + private string apiRegion; + private HttpClient httpClient; private HttpClientHandler httpHandler; - public AmplitudeApi(string apiKey) + public AmplitudeApi(string apiKey, string apiRegion) { this.apiKey = apiKey; + this.apiRegion = apiRegion; httpHandler = new HttpClientHandler(); httpHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; @@ -49,9 +52,9 @@ public void ConfigureProxy(string proxyUserName, string proxyPassword) httpHandler.Proxy = WebRequest.GetSystemWebProxy(); } else - { + { httpHandler.Proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword); - } + } } public override Task Identify(AmplitudeIdentify identification) @@ -83,9 +86,13 @@ private async Task DoApiCall(string endPoint, string paramName, stri var data = new StringContent(paramData, UTF8Encoding.UTF8, "application/json"); content.Add(data, paramName); + var apiURL = "https://api.amplitude.com"; + if (apiRegion == "eu") + apiURL = "https://api.eu.amplitude.com"; + try { - var postResult = await httpClient.PostAsync($"https://api.amplitude.com/{endPoint}", content); + var postResult = await httpClient.PostAsync($"{apiURL}/{endPoint}", content); if (postResult.StatusCode >= HttpStatusCode.InternalServerError) { diff --git a/readme.md b/readme.md index 154a6b9..45ba8e9 100644 --- a/readme.md +++ b/readme.md @@ -28,8 +28,11 @@ public partial class App : Application protected override void OnStartup(StartupEventArgs e) { - // Only call this once per lifetime of the object - analytics = AmplitudeService.Initialize(""); + // Only call this once per lifetime of the object. The constructor defines + // an optional API region parameter that defines the region (data center) + // to use. Defaults to US + // Set the region to "eu" for the European data center + analytics = AmplitudeService.Initialize("", "(optional) "); base.OnStartup(e); }