Example #1
0
        protected SwrveMessage GetNextMessage(int messagesCount, SwrveQAUser qaUser)
        {
            if (RandomOrder)
            {
                List <SwrveMessage> randomMessages = new List <SwrveMessage> (Messages);
                randomMessages.Shuffle();
                for (int mi = 0; mi < randomMessages.Count; mi++)
                {
                    SwrveMessage message = randomMessages[mi];
                    if (message.IsDownloaded())
                    {
                        return(message);
                    }
                }
            }
            else if (Next < messagesCount)
            {
                SwrveMessage message = Messages [Next];
                if (message.IsDownloaded())
                {
                    return(message);
                }
            }

            LogAndAddReason("Campaign " + this.Id + " hasn't finished downloading.", qaUser);
            return(null);
        }
 protected SwrveMessage GetNextMessage(int messagesCount, SwrveQAUser qaUser)
 {
     if (RandomOrder)
     {
         List <SwrveMessage> list = new List <SwrveMessage>(Messages);
         list.Shuffle();
         for (int i = 0; i < list.Count; i++)
         {
             SwrveMessage swrveMessage = list[i];
             if (swrveMessage.IsDownloaded())
             {
                 return(swrveMessage);
             }
         }
     }
     else if (base.Next < messagesCount)
     {
         SwrveMessage swrveMessage = Messages[base.Next];
         if (swrveMessage.IsDownloaded())
         {
             return(swrveMessage);
         }
     }
     LogAndAddReason("Campaign " + Id + " hasn't finished downloading.", qaUser);
     return(null);
 }
Example #3
0
 protected void LogAndAddReason(string reason, SwrveQAUser qaUser)
 {
     if (qaUser != null && !qaUser.campaignReasons.ContainsKey(this.Id))
     {
         qaUser.campaignReasons.Add(this.Id, reason);
     }
     SwrveLog.Log(string.Format("{0} {1}", this, reason));
 }
 protected void LogAndAddReason(string reason, SwrveQAUser qaUser)
 {
     if (!(qaUser?.campaignReasons.ContainsKey(Id) ?? true))
     {
         qaUser.campaignReasons.Add(Id, reason);
     }
     SwrveLog.Log($"{this} {reason}");
 }
 public bool CheckImpressions(SwrveQAUser qaUser)
 {
     if (Impressions >= maxImpressions)
     {
         LogAndAddReason("{Campaign throttle limit} Campaign " + Id + " has been shown " + maxImpressions + " times already", qaUser);
         return(false);
     }
     return(true);
 }
Example #6
0
        public bool IsActive(SwrveQAUser qaUser)
        {
            DateTime utcNow = SwrveHelper.GetUtcNow();

            if (this.StartDate > utcNow)
            {
                this.LogAndAddReason(string.Format("Campaign {0} not started yet (now: {1}, end: {2})", this.Id, utcNow, this.StartDate), qaUser);
                return(false);
            }
            if (this.EndDate < utcNow)
            {
                this.LogAndAddReason(string.Format("Campaign {0} has finished (now: {1}, end: {2})", this.Id, utcNow, this.EndDate), qaUser);
                return(false);
            }
            return(true);
        }
        public bool IsActive(SwrveQAUser qaUser)
        {
            DateTime utcNow = SwrveHelper.GetUtcNow();

            if (StartDate > utcNow)
            {
                LogAndAddReason($"Campaign {Id} not started yet (now: {utcNow}, end: {StartDate})", qaUser);
                return(false);
            }
            if (EndDate < utcNow)
            {
                LogAndAddReason($"Campaign {Id} has finished (now: {utcNow}, end: {EndDate})", qaUser);
                return(false);
            }
            return(true);
        }
Example #8
0
        public bool IsActive(SwrveQAUser qaUser)
        {
            // Use UTC to compare to start/end dates from DB
            DateTime utcNow = SwrveHelper.GetUtcNow();

            if (StartDate > utcNow)
            {
                LogAndAddReason(string.Format("Campaign {0} not started yet (now: {1}, end: {2})", Id, utcNow, StartDate), qaUser);
                return(false);
            }

            if (EndDate < utcNow)
            {
                LogAndAddReason(string.Format("Campaign {0} has finished (now: {1}, end: {2})", Id, utcNow, EndDate), qaUser);
                return(false);
            }

            return(true);
        }
Example #9
0
        public SwrveMessage GetMessageForEvent(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            int count = this.Messages.Count;

            if (count == 0)
            {
                base.LogAndAddReason("No messages in campaign " + this.Id, qaUser);
                return(null);
            }
            if (base.checkCampaignLimits(triggerEvent, payload, qaUser))
            {
                SwrveLog.Log(string.Format("[{0}] {1} matches a trigger in {2}", this, triggerEvent, this.Id));
                return(this.GetNextMessage(count, qaUser));
            }
            return(null);
        }
Example #10
0
        public bool checkCampaignLimits(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            // Use local time to track throttle limits (want to show local time in logs)
            DateTime localNow = SwrveHelper.GetNow();

            if (!CanTrigger(triggerEvent, payload, qaUser))
            {
                LogAndAddReason("There is no trigger in " + Id + " that matches " + triggerEvent, qaUser);
                return(false);
            }

            if (!IsActive(qaUser))
            {
                return(false);
            }

            if (Impressions >= maxImpressions)
            {
                LogAndAddReason("{Campaign throttle limit} Campaign " + Id + " has been shown " + maxImpressions + " times already", qaUser);
                return(false);
            }

            if (!string.Equals(triggerEvent, SwrveSDK.DefaultAutoShowMessagesTrigger, StringComparison.OrdinalIgnoreCase) && IsTooSoonToShowMessageAfterLaunch(localNow))
            {
                LogAndAddReason("{Campaign throttle limit} Too soon after launch. Wait until " + showMessagesAfterLaunch.ToString(WaitTimeFormat), qaUser);
                return(false);
            }

            if (IsTooSoonToShowMessageAfterDelay(localNow))
            {
                LogAndAddReason("{Campaign throttle limit} Too soon after last message. Wait until " + showMessagesAfterDelay.ToString(WaitTimeFormat), qaUser);
                return(false);
            }

            return(true);
        }
 public SwrveConversation GetConversationForEvent(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
 {
     if (null == Conversation)
     {
         LogAndAddReason("No conversation in campaign " + Id, qaUser);
         return(null);
     }
     if (CheckCampaignLimits(triggerEvent, payload, qaUser))
     {
         SwrveLog.Log($"[{this}] {triggerEvent} matches a trigger in {Id}");
         if (AreAssetsReady())
         {
             return(Conversation);
         }
         LogAndAddReason("Assets not downloaded to show conversation in campaign " + Id, qaUser);
     }
     return(null);
 }
        public bool CheckCampaignLimits(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            DateTime now = SwrveHelper.GetNow();

            if (!CanTrigger(triggerEvent, payload, qaUser))
            {
                LogAndAddReason("There is no trigger in " + Id + " that matches " + triggerEvent, qaUser);
                return(false);
            }
            if (!IsActive(qaUser))
            {
                return(false);
            }
            if (!CheckImpressions(qaUser))
            {
                return(false);
            }
            if (!string.Equals(triggerEvent, "Swrve.Messages.showAtSessionStart", StringComparison.OrdinalIgnoreCase) && IsTooSoonToShowMessageAfterLaunch(now))
            {
                LogAndAddReason("{Campaign throttle limit} Too soon after launch. Wait until " + showMessagesAfterLaunch.ToString("HH\\:mm\\:ss zzz"), qaUser);
                return(false);
            }
            if (IsTooSoonToShowMessageAfterDelay(now))
            {
                LogAndAddReason("{Campaign throttle limit} Too soon after last message. Wait until " + showMessagesAfterDelay.ToString("HH\\:mm\\:ss zzz"), qaUser);
                return(false);
            }
            return(true);
        }
        public static SwrveMessagesCampaign LoadFromJSON(ISwrveAssetsManager swrveAssetsManager, Dictionary <string, object> campaignData, int id, DateTime initialisedTime, SwrveQAUser qaUser, Color?defaultBackgroundColor)
        {
            SwrveMessagesCampaign swrveMessagesCampaign = new SwrveMessagesCampaign(initialisedTime);
            object value = null;

            campaignData.TryGetValue("messages", out value);
            IList <object> list = null;

            try
            {
                list = (IList <object>)value;
            }
            catch (Exception ex)
            {
                swrveMessagesCampaign.LogAndAddReason("Campaign [" + id + "] invalid messages found, skipping.  Error: " + ex, qaUser);
            }
            if (list == null)
            {
                swrveMessagesCampaign.LogAndAddReason("Campaign [" + id + "] JSON messages are null, skipping.", qaUser);
                return(null);
            }
            int i = 0;

            for (int count = list.Count; i < count; i++)
            {
                Dictionary <string, object> messageData = (Dictionary <string, object>)list[i];
                SwrveMessage swrveMessage = SwrveMessage.LoadFromJSON(swrveAssetsManager, swrveMessagesCampaign, messageData, defaultBackgroundColor);
                if (swrveMessage.Formats.Count > 0)
                {
                    swrveMessagesCampaign.AddMessage(swrveMessage);
                }
            }
            if (swrveMessagesCampaign.Messages.Count == 0)
            {
                swrveMessagesCampaign.LogAndAddReason("Campaign [" + id + "] no messages found, skipping.", qaUser);
            }
            return(swrveMessagesCampaign);
        }
Example #14
0
        public bool checkCampaignLimits(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            DateTime now = SwrveHelper.GetNow();

            if (!this.CanTrigger(triggerEvent, payload, qaUser))
            {
                this.LogAndAddReason(string.Concat(new object[]
                {
                    "There is no trigger in ",
                    this.Id,
                    " that matches ",
                    triggerEvent
                }), qaUser);
                return(false);
            }
            if (!this.IsActive(qaUser))
            {
                return(false);
            }
            if (this.Impressions >= this.maxImpressions)
            {
                this.LogAndAddReason(string.Concat(new object[]
                {
                    "{Campaign throttle limit} Campaign ",
                    this.Id,
                    " has been shown ",
                    this.maxImpressions,
                    " times already"
                }), qaUser);
                return(false);
            }
            if (!string.Equals(triggerEvent, "Swrve.Messages.showAtSessionStart", StringComparison.OrdinalIgnoreCase) && this.IsTooSoonToShowMessageAfterLaunch(now))
            {
                this.LogAndAddReason("{Campaign throttle limit} Too soon after launch. Wait until " + this.showMessagesAfterLaunch.ToString("HH\\:mm\\:ss zzz"), qaUser);
                return(false);
            }
            if (this.IsTooSoonToShowMessageAfterDelay(now))
            {
                this.LogAndAddReason("{Campaign throttle limit} Too soon after last message. Wait until " + this.showMessagesAfterDelay.ToString("HH\\:mm\\:ss zzz"), qaUser);
                return(false);
            }
            return(true);
        }
        public SwrveMessage GetMessageForEvent(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            int count = Messages.Count;

            if (count == 0)
            {
                LogAndAddReason("No messages in campaign " + Id, qaUser);
                return(null);
            }
            if (CheckCampaignLimits(triggerEvent, payload, qaUser))
            {
                SwrveLog.Log($"[{this}] {triggerEvent} matches a trigger in {Id}");
                return(GetNextMessage(count, qaUser));
            }
            return(null);
        }
        public static SwrveBaseCampaign LoadFromJSON(ISwrveAssetsManager swrveAssetsManager, Dictionary <string, object> campaignData, DateTime initialisedTime, SwrveQAUser qaUser, Color?defaultBackgroundColor)
        {
            int @int = MiniJsonHelper.GetInt(campaignData, "id");
            SwrveBaseCampaign swrveBaseCampaign = null;

            if (campaignData.ContainsKey("conversation"))
            {
                swrveBaseCampaign = SwrveConversationCampaign.LoadFromJSON(swrveAssetsManager, campaignData, @int, initialisedTime);
            }
            else if (campaignData.ContainsKey("messages"))
            {
                swrveBaseCampaign = SwrveMessagesCampaign.LoadFromJSON(swrveAssetsManager, campaignData, @int, initialisedTime, qaUser, defaultBackgroundColor);
            }
            if (swrveBaseCampaign == null)
            {
                return(null);
            }
            swrveBaseCampaign.Id = @int;
            AssignCampaignTriggers(swrveBaseCampaign, campaignData);
            swrveBaseCampaign.MessageCenter = campaignData.ContainsKey("message_center") && (bool)campaignData["message_center"];
            if (!swrveBaseCampaign.MessageCenter && swrveBaseCampaign.GetTriggers().Count == 0)
            {
                swrveBaseCampaign.LogAndAddReason("Campaign [" + swrveBaseCampaign.Id + "], has no triggers. Skipping this campaign.", qaUser);
                return(null);
            }
            AssignCampaignRules(swrveBaseCampaign, campaignData);
            AssignCampaignDates(swrveBaseCampaign, campaignData);
            swrveBaseCampaign.Subject = (campaignData.ContainsKey("subject") ? ((string)campaignData["subject"]) : "");
            if (swrveBaseCampaign.MessageCenter)
            {
                SwrveLog.Log($"message center campaign: {swrveBaseCampaign.GetType()}, {swrveBaseCampaign.subject}");
            }
            return(swrveBaseCampaign);
        }
Example #17
0
 public SwrveConversation GetConversationForEvent(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
 {
     if (this.Conversation == null)
     {
         base.LogAndAddReason("No conversation in campaign " + this.Id, qaUser);
         return(null);
     }
     if (base.checkCampaignLimits(triggerEvent, payload, qaUser))
     {
         SwrveLog.Log(string.Format("[{0}] {1} matches a trigger in {2}", this, triggerEvent, this.Id));
         if (this.AreAssetsReady())
         {
             return(this.Conversation);
         }
         base.LogAndAddReason("Assets not downloaded to show conversation in campaign " + this.Id, qaUser);
     }
     return(null);
 }
 protected void LogAndAddReason(int ident, string reason, SwrveQAUser qaUser)
 {
     LogAndAddReason(reason, qaUser);
 }
Example #19
0
        /// <summary>
        /// Load an in-app campaign from a JSON response.
        /// </summary>
        /// <param name="campaignData">
        /// JSON object with the individual campaign data.
        /// </param>
        /// <param name="initialisedTime">
        /// Time that the SDK was initialised. Used for rules checking.
        /// </param>
        /// <param name="assetPath">
        /// Path to the folder that will store all the assets.
        /// </param>
        /// <returns>
        /// Parsed in-app campaign.
        /// </returns>
        public static SwrveBaseCampaign LoadFromJSON(SwrveSDK sdk, Dictionary <string, object> campaignData, DateTime initialisedTime, SwrveQAUser qaUser)
        {
            int id = MiniJsonHelper.GetInt(campaignData, ID_KEY);
            SwrveBaseCampaign campaign = null;

            if (campaignData.ContainsKey(CONVERSATION_KEY))
            {
                campaign = SwrveConversationCampaign.LoadFromJSON(sdk, campaignData, id, initialisedTime);
            }
            else if (campaignData.ContainsKey(MESSAGES_KEY))
            {
                campaign = SwrveMessagesCampaign.LoadFromJSON(sdk, campaignData, id, initialisedTime, qaUser);
            }

            if (campaign == null)
            {
                return(null);
            }
            campaign.Id = id;

            AssignCampaignTriggers(campaign, campaignData);
            campaign.MessageCenter = campaignData.ContainsKey(MESSAGE_CENTER_KEY) && (bool)campaignData[MESSAGE_CENTER_KEY];

            if ((!campaign.MessageCenter) && (campaign.GetTriggers().Count == 0))
            {
                campaign.LogAndAddReason("Campaign [" + campaign.Id + "], has no triggers. Skipping this campaign.", qaUser);
                return(null);
            }

            AssignCampaignRules(campaign, campaignData);
            AssignCampaignDates(campaign, campaignData);
            campaign.Subject = campaignData.ContainsKey(SUBJECT_KEY) ? (string)campaignData[SUBJECT_KEY] : "";

            if (campaign.MessageCenter)
            {
                SwrveLog.Log(string.Format("message center campaign: {0}, {1}", campaign.GetType(), campaign.subject));
            }

            return(campaign);
        }
 public bool CanTrigger(string eventName, IDictionary <string, string> payload = null, SwrveQAUser qaUser = null)
 {
     return(GetTriggers().Any((SwrveTrigger trig) => trig.CanTrigger(eventName, payload)));
 }
Example #21
0
        new public static SwrveMessagesCampaign LoadFromJSON(SwrveSDK sdk, Dictionary <string, object> campaignData, int id, DateTime initialisedTime, SwrveQAUser qaUser)
        {
            SwrveMessagesCampaign campaign = new SwrveMessagesCampaign(initialisedTime);

            object _messages = null;

            campaignData.TryGetValue("messages", out _messages);
            IList <object> messages = null;

            try {
                messages = (IList <object>)_messages;
            }
            catch (Exception e) {
                campaign.LogAndAddReason("Campaign [" + id + "] invalid messages found, skipping.  Error: " + e, qaUser);
            }

            if (messages == null)
            {
                campaign.LogAndAddReason("Campaign [" + id + "] JSON messages are null, skipping.", qaUser);
                return(null);
            }

            for (int k = 0, t = messages.Count; k < t; k++)
            {
                Dictionary <string, object> messageData = (Dictionary <string, object>)messages [k];
                SwrveMessage message = SwrveMessage.LoadFromJSON(sdk, campaign, messageData);
                if (message.Formats.Count > 0)
                {
                    campaign.AddMessage(message);
                }
            }
            if (campaign.Messages.Count == 0)
            {
                campaign.LogAndAddReason("Campaign [" + id + "] no messages found, skipping.", qaUser);
            }

            return(campaign);
        }