/// <summary> /// Adds the tracker payload to the emitter. /// </summary> /// <param name="payload">The base event payload.</param> /// <param name="contexts">The list of contexts from the event.</param> private void AddTrackerPayload(TrackerPayload payload, List <IContext> contexts, string eventId) { // Add default parameters to the payload payload.Add(Constants.PLATFORM, this.platform.Value); payload.Add(Constants.APP_ID, this.appId); payload.Add(Constants.NAMESPACE, this.trackerNamespace); payload.Add(Constants.TRACKER_VERSION, Version.VERSION); // Add the subject data if available if (subject != null) { payload.AddDict(subject.GetPayload().GetDictionary()); } // Add the session context if available if (session != null) { contexts.Add(session.GetSessionContext(eventId)); } // Build the final context and add it to the payload if (contexts != null && contexts.Count > 0) { SelfDescribingJson envelope = GetFinalContext(contexts); payload.AddJson(envelope.GetDictionary(), this.base64Encoded, Constants.CONTEXT_ENCODED, Constants.CONTEXT); } Log.Verbose("Tracker: Sending event to the emitter."); Log.Verbose(" + Event: " + payload.ToString()); // Add the event to the emitter. emitter.Add(payload); }
public void testInitSelfDescribing() { var sdj = new SelfDescribingJson("iglu:com.acme/some_event/jsonschema/1-0-0", new Dictionary <string, string> { { "hello", "world" } }); var sd = new SelfDescribing() .SetEventData(sdj) .SetTrueTimestamp(123456789123) .Build(); Assert.IsNotNull(sd); Assert.AreEqual(Constants.EVENT_UNSTRUCTURED, sd.GetPayload().Payload[Constants.EVENT]); Assert.AreEqual(@"{""schema"":""iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"",""data"":{""schema"":""iglu:com.acme/some_event/jsonschema/1-0-0"",""data"":{""hello"":""world""}}}", sd.GetPayload().Payload[Constants.UNSTRUCTURED]); sd.SetBase64Encode(true); Assert.AreEqual("eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy91bnN0cnVjdF9ldmVudC9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6eyJzY2hlbWEiOiJpZ2x1OmNvbS5hY21lL3NvbWVfZXZlbnQvanNvbnNjaGVtYS8xLTAtMCIsImRhdGEiOnsiaGVsbG8iOiJ3b3JsZCJ9fX0=", sd.GetPayload().Payload[Constants.UNSTRUCTURED_ENCODED]); Assert.IsNotNull(sd.GetContexts()); Assert.IsTrue(sd.GetPayload().Payload.ContainsKey(Constants.EID)); Assert.IsTrue(sd.GetPayload().Payload.ContainsKey(Constants.TIMESTAMP)); Assert.IsTrue(sd.GetPayload().Payload.ContainsKey(Constants.TRUE_TIMESTAMP)); }
// --- Interface Methods /// <summary> /// Gets the event payload. /// </summary> /// <returns>The event payload</returns> public override IPayload GetPayload() { Payload payload = new Payload(); payload.Add(Constants.EVENT, Constants.EVENT_UNSTRUCTURED); SelfDescribingJson envelope = new SelfDescribingJson(Constants.SCHEMA_UNSTRUCT_EVENT, this.eventData.Payload); payload.AddJson(envelope.Payload, this.base64Encode, Constants.UNSTRUCTURED_ENCODED, Constants.UNSTRUCTURED); return(AddDefaultPairs(payload)); }
// --- Helpers /// <summary> /// Gets a ready request containing a POST /// </summary> /// <returns>The POST request that is already being sent</returns> /// <param name="events">Events to send in the post</param> protected HttpContent GetPOSTRequest(List <Dictionary <string, object> > events) { // Add STM to event AddSentTimeToEvents(events); // Build the event SelfDescribingJson sdj = new SelfDescribingJson(Constants.SCHEMA_PAYLOAD_DATA, events); // Build the HTTP Content Body HttpContent httpContent = new StringContent(sdj.ToString(), Encoding.UTF8, Constants.POST_CONTENT_TYPE); return(httpContent); }
// --- Tracking Functions /// <summary> /// Tracks an example SelfDescribing event /// </summary> public static void TrackSelfDescribing() { SelfDescribingJson sdj = new SelfDescribingJson("iglu:com.snowplowanalytics.snowplow/timing/jsonschema/1-0-0", new Dictionary <string, object> { { "category", "SdjTimingCategory" }, { "variable", "SdjTimingVariable" }, { "timing", 0 }, { "label", "SdjTimingLabel" } }); Instance.Track(new SelfDescribing() .SetEventData(sdj) .Build()); SessionMadeCount++; }
// --- Helpers /// <summary> /// Gets a ready request containing a POST /// </summary> /// <returns>The POST request that is already being sent</returns> /// <param name="events">Events to send in the post</param> /// <param name="ids">The row ids</param> /// <param name="oversize">If the event list is oversize</param> protected UnityHTTP.Request GetPOSTRequest(List <Dictionary <string, object> > events) { // Add STM to event AddSentTimeToEvents(events); // Build the event SelfDescribingJson sdj = new SelfDescribingJson(Constants.SCHEMA_PAYLOAD_DATA, events); byte[] data = Utils.StringToBytes(sdj.ToString()); UnityHTTP.Request httpRequest = new UnityHTTP.Request(UnityHTTP.Enums.RequestType.POST, this.collectorUri, data); httpRequest.AddHeader("Content-Type", Constants.POST_CONTENT_TYPE); httpRequest.synchronous = this.synchronous; return(httpRequest); }
public void testInitSDJWithObject() { object nullObj = null; var sdj = new SelfDescribingJson("iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4", nullObj); Assert.AreEqual(@"{""schema"":""iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4""}", sdj.ToString()); object dictObj = new Dictionary <string, object> { { "hello", "world" } }; sdj.SetData(dictObj); Assert.AreEqual(@"{""schema"":""iglu:com.snowplowanalytics.snowplow/payload_data/jsonschema/1-0-4"",""data"":{""hello"":""world""}}", sdj.ToString()); }
private void TrackEvent(iEventInterface snowPlowEvent) { SelfDescribingJson eventData = new SelfDescribingJson(GetSchemaForEvent(snowPlowEvent), snowPlowEvent.snowplowProperties()); Unstructured unstructured = new Unstructured(); unstructured.SetCustomContext(new List <IContext> { UserContext(), MobileContext() }); unstructured.SetEventData(eventData); unstructured.Build(); tracker.Track(unstructured); DebugLog($"[ANALYTICS] Track event: {snowPlowEvent.name()} -> {snowPlowEvent.debugDescription()}"); DebugLog($"[ANALYTICS] Payload: {unstructured.GetPayload().ToString()}"); Crashlytics.Log($"Snowplow event: {snowPlowEvent.debugDescription()}"); }
public void TestInitWithSdj() { Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add("demo", 5); SelfDescribingJson data = new SelfDescribingJson("iglu:acme.com/demo_app/jsonschema/1-0-0", dict); SelfDescribingJson sdj = new SelfDescribingJson("iglu:acme.com/demo/jsonschema/1-0-0", data); Assert.NotNull(sdj); Assert.AreEqual(126, sdj.GetByteSize()); CollectionAssert.AreEquivalent(JSON.Deserialize <Dictionary <string, object> >("{\"data\":{\"data\":{\"demo\":5}, \"schema\":\"iglu:acme.com/demo_app/jsonschema/1-0-0\"}, \"schema\":\"iglu:acme.com/demo/jsonschema/1-0-0\"}"), JSON.Deserialize <Dictionary <string, object> >(sdj.ToString())); dict.Add("app", "hello"); data.SetData(dict); sdj.SetData(data); CollectionAssert.AreEquivalent(JSON.Deserialize <Dictionary <string, object> >("{\"data\":{\"data\":{\"app\":\"hello\", \"demo\":5}, \"schema\":\"iglu:acme.com/demo_app/jsonschema/1-0-0\"}, \"schema\":\"iglu:acme.com/demo/jsonschema/1-0-0\"}"), JSON.Deserialize <Dictionary <string, object> >(sdj.ToString())); }
public void TestInitWithObject() { Dictionary <string, object> dict = new Dictionary <string, object> (); dict.Add("demo", 5); SelfDescribingJson sdj = new SelfDescribingJson("iglu:acme.com/demo_app/jsonschema/1-0-0", dict); Assert.NotNull(sdj); Assert.AreEqual(71, sdj.GetByteSize()); Assert.AreEqual("{\"data\":{\"demo\":5}, \"schema\":\"iglu:acme.com/demo_app/jsonschema/1-0-0\"}", sdj.ToString()); sdj.SetSchema("iglu:acme.com/demo_app/jsonschema/1-0-1"); Assert.AreEqual("{\"data\":{\"demo\":5}, \"schema\":\"iglu:acme.com/demo_app/jsonschema/1-0-1\"}", sdj.ToString()); dict.Add("app", "hello"); sdj.SetData(dict); Assert.AreEqual("{\"data\":{\"app\":\"hello\", \"demo\":5}, \"schema\":\"iglu:acme.com/demo_app/jsonschema/1-0-1\"}", sdj.ToString()); }
private bool ensureUnstructEventGet(Tracker t, MockGet g, bool expectBase64 = false) { var eventJson = new SelfDescribingJson( "iglu:com.acme/test/jsonschema/1-0-0", new Dictionary <string, string> { { "page", "testpage" }, { "user", "tester" } } ); t.Track(new SelfDescribing().SetEventData(eventJson).Build()); t.Flush(); var actual = g.Queries[0]; var expectedPayload = @"{""schema"":""iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"",""data"":{""schema"":""iglu:com.acme/test/jsonschema/1-0-0"",""data"":{""page"":""testpage"",""user"":""tester""}}}"; var expected = new Dictionary <string, string> { { "e", "ue" } }; if (expectBase64) { string base64encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(expectedPayload)); expected.Add("ue_px", base64encoded); } else { expected.Add("ue_pr", expectedPayload); } assertQueryContainsDefaults(actual); assertQueryContainsAll(actual, expected); return(true); }
/// <summary> /// Sets the event data. /// </summary> /// <returns>The event data.</returns> /// <param name="eventData">Event data.</param> public SelfDescribing SetEventData(SelfDescribingJson eventData) { this.eventData = eventData; return(this); }
/// <summary> /// Sets the event data. /// </summary> /// <returns>The event data.</returns> /// <param name="eventData">Event data.</param> public Unstructured SetEventData(SelfDescribingJson eventData) { this.eventData = eventData; return(this); }
/// <summary> /// Adds the standard NV Pairs to the payload and stitches any available /// contexts to the final payload. /// </summary> /// <param name="payload">The basee event payload</param> /// <param name="contexts">The contexts array</param> /// <param name="eventId">The event ID</param> private void CompleteAndTrackPayload(Payload payload, List <IContext> contexts, string eventId) { payload.AddDict(_standardNvPairs); // Add the subject data if available if (_subject != null) { payload.AddDict(_subject._payload.Payload); } // Add the session context if available if (_clientSession != null) { contexts.Add(_clientSession.GetSessionContext(eventId)); } // Add the desktop context if available if (_desktopContextDelegate != null) { DesktopContext desktopContext = _desktopContextDelegate.Invoke(); if (desktopContext != null) { contexts.Add(desktopContext); } } // Add the mobile context if available if (_mobileContextDelegate != null) { MobileContext mobileContext = _mobileContextDelegate.Invoke(); if (mobileContext != null) { contexts.Add(mobileContext); } } // Add the geo-location context if available if (_geoLocationDelegate != null) { GeoLocationContext geoLocationContext = _geoLocationDelegate.Invoke(); if (geoLocationContext != null) { contexts.Add(geoLocationContext); } } // Build the final context and it to the payload if (contexts != null && contexts.Any()) { var contextArray = new List <Dictionary <string, object> >(); foreach (IContext context in contexts) { contextArray.Add(context.GetJson().Payload); } var contextEnvelope = new SelfDescribingJson(Constants.SCHEMA_CONTEXTS, contextArray); payload.AddJson(contextEnvelope.Payload, _encodeBase64, Constants.CONTEXT_ENCODED, Constants.CONTEXT); } // Send the payload to the emitter if (_synchronous) { _emitter.Input(payload); } else { Task.Factory.StartNew(() => _emitter.Input(payload)); } }