/// <summary> /// Sends conversion event to Optimizely. /// </summary> /// <param name="eventKey">Event key representing the event which needs to be recorded</param> /// <param name="userId">ID for user</param> /// <param name="userAttributes">Attributes of the user</param> /// <param name="eventTags">eventTags array Hash representing metadata associated with the event.</param> public void Track(string eventKey, string userId, UserAttributes userAttributes = null, EventTags eventTags = null) { if (!IsValid) { Logger.Log(LogLevel.ERROR, "Datafile has invalid format. Failing 'track'."); return; } var inputValues = new Dictionary <string, string> { { USER_ID, userId }, { EVENT_KEY, eventKey } }; if (!ValidateStringInputs(inputValues)) { return; } var eevent = Config.GetEvent(eventKey); if (eevent.Key == null) { Logger.Log(LogLevel.INFO, string.Format("Not tracking user {0} for event {1}.", userId, eventKey)); return; } if (eventTags != null) { eventTags = eventTags.FilterNullValues(Logger); } var conversionEvent = EventBuilder.CreateConversionEvent(Config, eventKey, userId, userAttributes, eventTags); Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId)); Logger.Log(LogLevel.DEBUG, string.Format("Dispatching conversion event to URL {0} with params {1}.", conversionEvent.Url, conversionEvent.GetParamsAsJson())); try { EventDispatcher.DispatchEvent(conversionEvent); } catch (Exception exception) { Logger.Log(LogLevel.ERROR, string.Format("Unable to dispatch conversion event. Error {0}", exception.Message)); } NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId, userAttributes, eventTags, conversionEvent); }
/// <summary> /// Sends conversion event to Optimizely. /// </summary> /// <param name="eventKey">Event key representing the event which needs to be recorded</param> /// <param name="userId">ID for user</param> /// <param name="userAttributes">Attributes of the user</param> /// <param name="eventTags">eventTags array Hash representing metadata associated with the event.</param> public void Track(string eventKey, string userId, UserAttributes userAttributes = null, EventTags eventTags = null) { var config = ProjectConfigManager?.GetConfig(); if (config == null) { Logger.Log(LogLevel.ERROR, "Datafile has invalid format. Failing 'Track'."); return; } var inputValues = new Dictionary <string, string> { { USER_ID, userId }, { EVENT_KEY, eventKey } }; if (!ValidateStringInputs(inputValues)) { return; } var eevent = config.GetEvent(eventKey); if (eevent.Key == null) { Logger.Log(LogLevel.INFO, string.Format("Not tracking user {0} for event {1}.", userId, eventKey)); return; } if (eventTags != null) { eventTags = eventTags.FilterNullValues(Logger); } var userEvent = UserEventFactory.CreateConversionEvent(config, eventKey, userId, userAttributes, eventTags); EventProcessor.Process(userEvent); Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId)); if (NotificationCenter.GetNotificationCount(NotificationCenter.NotificationType.Track) > 0) { var conversionEvent = EventFactory.CreateLogEvent(userEvent, Logger); NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId, userAttributes, eventTags, conversionEvent); } }
/// <summary> /// Sends conversion event to Optimizely. /// </summary> /// <param name="eventKey">Event key representing the event which needs to be recorded</param> /// <param name="userId">ID for user</param> /// <param name="userAttributes">Attributes of the user</param> /// <param name="eventTags">eventTags array Hash representing metadata associated with the event.</param> public void Track(string eventKey, string userId, UserAttributes userAttributes = null, EventTags eventTags = null) { if (!IsValid) { Logger.Log(LogLevel.ERROR, "Datafile has invalid format. Failing 'track'."); return; } var inputValues = new Dictionary <string, string> { { USER_ID, userId }, { EVENT_KEY, eventKey } }; if (!ValidateStringInputs(inputValues)) { return; } var eevent = Config.GetEvent(eventKey); if (eevent.Key == null) { Logger.Log(LogLevel.ERROR, string.Format("Not tracking user {0} for event {1}.", userId, eventKey)); return; } // Filter out experiments that are not running or when user(s) do not meet conditions. var validExperimentIdToVariationMap = new Dictionary <string, Variation>(); var experimentIds = eevent.ExperimentIds; foreach (string id in eevent.ExperimentIds) { var experiment = Config.GetExperimentFromId(id); //Validate experiment var variation = DecisionService.GetVariation(experiment, userId, userAttributes); if (variation != null) { validExperimentIdToVariationMap[experiment.Id] = variation; } else { Logger.Log(LogLevel.INFO, string.Format("Not tracking user \"{0}\" for experiment \"{1}\"", userId, experiment.Key)); } } if (validExperimentIdToVariationMap.Count > 0) { if (userAttributes != null) { userAttributes = userAttributes.FilterNullValues(Logger); } if (eventTags != null) { eventTags = eventTags.FilterNullValues(Logger); } var conversionEvent = EventBuilder.CreateConversionEvent(Config, eventKey, validExperimentIdToVariationMap, userId, userAttributes, eventTags); Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId)); Logger.Log(LogLevel.DEBUG, string.Format("Dispatching conversion event to URL {0} with params {1}.", conversionEvent.Url, conversionEvent.GetParamsAsJson())); try { EventDispatcher.DispatchEvent(conversionEvent); } catch (Exception exception) { Logger.Log(LogLevel.ERROR, string.Format("Unable to dispatch conversion event. Error {0}", exception.Message)); } NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId, userAttributes, eventTags, conversionEvent); } else { Logger.Log(LogLevel.INFO, string.Format("There are no valid experiments for event {0} to track.", eventKey)); } }