public void TestTrackerSetterFunctions() { Subject s1 = new Subject(); Session sess1 = new Session(); IEmitter e1 = new AsyncEmitter("acme.com"); Tracker t = new Tracker(e1, "aNamespace", "aAppId", s1, sess1); Assert.NotNull(t.GetEmitter()); Assert.AreEqual("http://acme.com/com.snowplowanalytics.snowplow/tp2", t.GetEmitter().GetCollectorUri()); Assert.NotNull(t.GetSubject()); Assert.NotNull(t.GetSession()); Assert.AreEqual("aNamespace", t.GetTrackerNamespace()); Assert.AreEqual("aAppId", t.GetAppId()); Assert.AreEqual(true, t.GetBase64Encoded()); Assert.AreEqual(DevicePlatforms.Mobile.Value, t.GetPlatform().Value); IEmitter e2 = new AsyncEmitter("acme.com.au"); t.SetEmitter(e2); Assert.AreEqual("http://acme.com.au/com.snowplowanalytics.snowplow/tp2", t.GetEmitter().GetCollectorUri()); t.SetSession(null); Assert.Null(t.GetSession()); t.SetSubject(null); Assert.Null(t.GetSubject()); t.SetTrackerNamespace("newNamespace"); Assert.AreEqual("newNamespace", t.GetTrackerNamespace()); t.SetAppId("newAppId"); Assert.AreEqual("newAppId", t.GetAppId()); t.SetBase64Encoded(false); Assert.AreEqual(false, t.GetBase64Encoded()); t.SetPlatform(DevicePlatforms.Desktop); Assert.AreEqual(DevicePlatforms.Desktop.Value, t.GetPlatform().Value); }
public void testLoadGet() { storage = new LiteDBStorage(_testDbFilename); Assert.AreEqual(0, storage.TotalItems); var queue = new PersistentBlockingQueue(storage, new PayloadToJsonString()); var endpoint = new SnowplowHttpCollectorEndpoint(host: _collectorHostUri, port: 8080, protocol: HttpProtocol.HTTP, method: HttpMethod.GET, byteLimitGet: 50000); var emitter = new AsyncEmitter(endpoint: endpoint, queue: queue, sendLimit: 25); var clientSession = new ClientSession(_testClientSessionFilename); Assert.IsFalse(tracker.Started); tracker.Start(emitter: emitter, clientSession: clientSession, trackerNamespace: "testNamespace", appId: "testAppId", encodeBase64: false, synchronous: false); Assert.IsTrue(tracker.Started); for (int i = 0; i < 100; i++) { tracker.Track(new Structured() .SetCategory("exampleCategory") .SetAction("exampleAction") .SetLabel("exampleLabel") .SetProperty("exampleProperty") .SetValue(17) .Build() ); } tracker.Flush(); tracker.Stop(); Assert.IsFalse(tracker.Started); }
private AsyncEmitter buildMockEmitter() { var q = new PersistentBlockingQueue(new MockStorage(), new PayloadToJsonString()); AsyncEmitter e = new AsyncEmitter(new MockEndpoint(), q); return(e); }
public void testFailedItemsEnqueuedAgain() { var q = new PersistentBlockingQueue(new MockStorage(), new PayloadToJsonString()); AsyncEmitter e = new AsyncEmitter(new MockEndpoint() { Response = false }, q); // no events will send, and so they should be at the start of the queue e.Start(); var p = new Payload(); p.AddDict(new Dictionary <string, string>() { { "foo", "bar" } }); e.Input(p); Thread.Sleep(100); // this could be done better with triggers of some kind e.Stop(); var inQueue = q.Peek(1); Assert.AreEqual(1, inQueue.Count); }
public void testFlushStopsAfterFirstFailure() { var storage = new MockStorage(); var queue = new PersistentBlockingQueue(storage, new PayloadToJsonString()); var mockEndpoint = new MockEndpoint() { Response = false }; AsyncEmitter e = new AsyncEmitter(mockEndpoint, queue, sendLimit: 1); for (int i = 0; i < 100; i++) { var p = new Payload(); p.AddDict(new Dictionary <string, string>() { { "foo", "bar" } }); e.Input(p); } Assert.AreEqual(100, storage.TotalItems); Assert.IsFalse(e.Running); e.Flush(true); Assert.IsFalse(e.Running); Assert.AreEqual(1, mockEndpoint.CallCount); Assert.AreEqual(0, mockEndpoint.Result.SuccessIds.Count); Assert.AreEqual(1, mockEndpoint.Result.FailureIds.Count); Assert.AreEqual(100, storage.TotalItems); }
public void RegisterUser(Player player) { PlayerGuid = player.Guid; HttpProtocol httpProtocol = HttpProtocol.HTTPS; if (debugMode) { httpProtocol = HttpProtocol.HTTP; endPointURI = "54.172.83.105"; } IEmitter emitter = new AsyncEmitter(endPointURI, httpProtocol, HttpMethod.POST, 500, 52000, 52000); #if UNITY_IOS string appID = "quixel_ios"; #else string appID = "quixel_android"; #endif Debug.Log($"[ANALYTICS][SETUP] appID: '{appID}' URI: '{endPointURI}' protocol: '{httpProtocol}'"); tracker = new Tracker(emitter, "peak", appID, GetSubject(), GetSession(), DevicePlatforms.Mobile); tracker.StartEventTracking(); DebugLog($"[ANALYTICS] SetUserId={PlayerGuid} should skip send events: {ShouldSkipSendEvent}"); }
// --- Tracker Init /// <summary> /// Gets the tracker. /// </summary> /// <returns>The tracker.</returns> private static void SetupTracker() { IEmitter emitter = new AsyncEmitter(trackerUri, trackerProto, trackerType); tracker = new Tracker(emitter, "SnowplowPong-Namespace", "SnowplowPong-AppId", null, GetSession()); tracker.StartEventTracking(); tracker.Track(new Structured().SetCategory("SnowplowPong").SetAction("Tracker").SetLabel("Init").Build()); }
public void TestAsyncEmitterInitException() { IEmitter e1 = null; try { e1 = new AsyncEmitter(null); } catch (Exception e) { Assert.AreEqual("Endpoint cannot be null or empty.", e.Message); } Assert.IsNull(e1); }
public void TestAsyncEmitterInit() { IEmitter e1 = new AsyncEmitter("acme.com"); Assert.NotNull(e1); Assert.AreEqual("http://acme.com/com.snowplowanalytics.snowplow/tp2", e1.GetCollectorUri()); Assert.AreEqual(HttpProtocol.HTTP, e1.GetHttpProtocol()); Assert.AreEqual(HttpMethod.POST, e1.GetHttpMethod()); Assert.AreEqual(500, e1.GetSendLimit()); Assert.AreEqual(52000, e1.GetByteLimitGet()); Assert.AreEqual(52000, e1.GetByteLimitPost()); Assert.NotNull(e1.GetEventStore()); Assert.False(e1.IsSending()); }
public void testAsyncPostOnSuccess() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = fake; int successes = -1; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, (successCount) => { successes = successCount; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(2, successes); } }
public void TestAsyncEmitterSetFunctions() { IEmitter e1 = new AsyncEmitter("acme.com"); Assert.AreEqual("http://acme.com/com.snowplowanalytics.snowplow/tp2", e1.GetCollectorUri()); e1.SetCollectorUri("acme.com.au"); Assert.AreEqual("http://acme.com.au/com.snowplowanalytics.snowplow/tp2", e1.GetCollectorUri()); e1.SetHttpProtocol(HttpProtocol.HTTPS); Assert.AreEqual("https://acme.com.au/com.snowplowanalytics.snowplow/tp2", e1.GetCollectorUri()); e1.SetHttpMethod(HttpMethod.GET); Assert.AreEqual("https://acme.com.au/i", e1.GetCollectorUri()); Assert.AreEqual(500, e1.GetSendLimit()); e1.SetSendLimit(1000); Assert.AreEqual(1000, e1.GetSendLimit()); Assert.AreEqual(52000, e1.GetByteLimitGet()); e1.SetByteLimitGet(100000); Assert.AreEqual(100000, e1.GetByteLimitGet()); Assert.AreEqual(52000, e1.GetByteLimitPost()); e1.SetByteLimitPost(100000); Assert.AreEqual(100000, e1.GetByteLimitPost()); }
public void testAsyncPostOnFailure() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = badFake; int?successes = null; List <Dictionary <string, string> > failureList = null; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, null, (successCount, failures) => { successes = successCount; failureList = failures; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(0, successes); Assert.AreEqual("first", failureList[0]["url"]); Assert.AreEqual("second", failureList[1]["url"]); } }
public void testBackoffInterval() { // because of the back off period (5sec +), this event should only be sent once var q = new PersistentBlockingQueue(new MockStorage(), new PayloadToJsonString()); var mockEndpoint = new MockEndpoint() { Response = false }; AsyncEmitter e = new AsyncEmitter(mockEndpoint, q); e.Start(); var p = new Payload(); p.AddDict(new Dictionary <string, string>() { { "foo", "bar" } }); e.Input(p); Thread.Sleep(100); e.Stop(); Assert.AreEqual(1, mockEndpoint.CallCount); }
public void testAsyncPostOnFailure() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = badFake; int? successes = null; List<Dictionary<string, string>> failureList = null; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, null, (successCount, failures) => { successes = successCount; failureList = failures; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(0, successes); Assert.AreEqual("first", failureList[0]["url"]); Assert.AreEqual("second", failureList[1]["url"]); } }
public void testAsyncPostOnSuccess() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = fake; int successes = -1; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, (successCount) => { successes = successCount; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(2, successes); } }
/// <summary> /// Inits the Snowplow Tracker; after this point it can be accessed globally. /// </summary> /// <param name="emitterUri">The emitter URI</param> /// <param name="protocol">The protocol to use</param> /// <param name="port">The port the collector is on</param> /// <param name="method">The method to use</param> /// <param name="useClientSession">Whether to enable client session</param> /// <param name="useMobileContext">Whether to enable mobile contexts</param> /// <param name="useGeoLocationContext">Whether to enable geo-location contexts</param> public static void Init( string emitterUri, HttpProtocol protocol = HttpProtocol.HTTP, int?port = null, HttpMethod method = HttpMethod.GET, bool useClientSession = false, bool useMobileContext = false, bool useGeoLocationContext = false) { var logger = new ConsoleLogger(); var dest = new SnowplowHttpCollectorEndpoint(emitterUri, method: method, port: port, protocol: protocol, l: logger); // Note: Maintain reference to Storage as this will need to be disposed of manually _storage = new LiteDBStorage(SnowplowTrackerPlatformExtension.Current.GetLocalFilePath("events.db")); var queue = new PersistentBlockingQueue(_storage, new PayloadToJsonString()); // Note: When using GET requests the sendLimit equals the number of concurrent requests - to many of these will break your application! var sendLimit = method == HttpMethod.GET ? 10 : 100; // Note: To make the tracker more battery friendly and less likely to drain batteries there are two settings to take note of here: // 1. The stopPollIntervalMs: Controls how often we look to the database for more events // 2. The deviceOnlineMethod: Is run before going to the database or attempting to send events, this will prevent any I/O from // occurring unless you have an active network connection var emitter = new AsyncEmitter(dest, queue, sendLimit: sendLimit, stopPollIntervalMs: 1000, sendSuccessMethod: EventSuccessCallback, deviceOnlineMethod: SnowplowTrackerPlatformExtension.Current.IsDeviceOnline, l: logger); var userId = PropertyManager.GetStringValue(KEY_USER_ID, SnowplowCore.Utils.GetGUID()); PropertyManager.SaveKeyValue(KEY_USER_ID, userId); var subject = new Subject() .SetPlatform(Platform.Mob) .SetUserId(userId) .SetLang("en"); if (useClientSession) { _clientSession = new ClientSession(SnowplowTrackerPlatformExtension.Current.GetLocalFilePath("client_session.dict"), l: logger); } // Note: You can either attach contexts to each event individually or for the more common contexts such as Desktop, Mobile and GeoLocation // you can pass a delegate method which will then be called for each event automatically. MobileContextDelegate mobileContextDelegate = null; if (useMobileContext) { mobileContextDelegate = SnowplowTrackerPlatformExtension.Current.GetMobileContext; } GeoLocationContextDelegate geoLocationContextDelegate = null; if (useMobileContext) { geoLocationContextDelegate = SnowplowTrackerPlatformExtension.Current.GetGeoLocationContext; } // Attach the created objects and begin all required background threads! Instance.Start(emitter: emitter, subject: subject, clientSession: _clientSession, trackerNamespace: _trackerNamespace, appId: _appId, encodeBase64: false, synchronous: false, mobileContextDelegate: mobileContextDelegate, geoLocationContextDelegate: geoLocationContextDelegate, l: logger); // Reset session counters SessionMadeCount = 0; SessionSuccessCount = 0; SessionFailureCount = 0; }