コード例 #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);
        }
コード例 #2
0
 protected void LogAndAddReason(string reason, SwrveQAUser qaUser)
 {
     if (qaUser != null && !qaUser.campaignReasons.ContainsKey(Id))
     {
         qaUser.campaignReasons.Add(Id, reason);
     }
     SwrveLog.Log(string.Format("{0} {1}", this, reason));
 }
コード例 #3
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);
        }
コード例 #4
0
        /// <summary>
        /// Search for a conversation related to the given trigger event at the given
        /// time. This function will return null if too many conversations were dismissed,
        /// the campaign start is in the future, the campaign end is in the past or
        /// the given event is not contained in the trigger set.
        /// </summary>
        /// <param name="triggerEvent">
        /// Event triggered. Must be a trigger for the campaign.
        /// </param>
        /// <param name="campaignReasons">
        /// At the exit of the function will include the reasons why a campaign the campaigns
        /// in memory were not shown or chosen.
        /// </param>
        /// <returns>
        /// SwrveConversation that contains the given event in its trigger list and satisfies all the
        /// rules.
        /// </returns>
        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(string.Format("[{0}] {1} matches a trigger in {2}", this, triggerEvent, Id));

                return(Conversation);
            }
            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Search for a message related to the given trigger event at the given
        /// time. This function will return null if too many messages were dismissed,
        /// the campaign start is in the future, the campaign end is in the past or
        /// the given event is not contained in the trigger set.
        /// </summary>
        /// <param name="triggerEvent">
        /// Event triggered. Must be a trigger for the campaign.
        /// </param>
        /// <param name="campaignReasons">
        /// At the exit of the function will include the reasons why a campaign the campaigns
        /// in memory were not shown or chosen.
        /// </param>
        /// <returns>
        /// In-app message that contains the given event in its trigger list and satisfies all the
        /// rules.
        /// </returns>
        public SwrveMessage GetMessageForEvent(string triggerEvent, IDictionary <string, string> payload, SwrveQAUser qaUser)
        {
            int messagesCount = Messages.Count;

            if (messagesCount == 0)
            {
                LogAndAddReason("No messages in campaign " + Id, qaUser);
                return(null);
            }

            if (checkCampaignLimits(triggerEvent, payload, qaUser))
            {
                SwrveLog.Log(string.Format("[{0}] {1} matches a trigger in {2}", this, triggerEvent, Id));

                return(GetNextMessage(messagesCount, qaUser));
            }
            return(null);
        }
コード例 #6
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);
        }
コード例 #7
0
 /// <summary>
 /// Check if this campaign will trigger for the given event and payload
 /// </summary>
 /// <returns>
 /// True if this campaign contains a message with the given trigger event.
 /// False otherwise.
 /// </returns>
 public bool CanTrigger(string eventName, IDictionary <string, string> payload = null, SwrveQAUser qaUser = null)
 {
     return(GetTriggers().Any(trig => trig.CanTrigger(eventName, payload)));
 }
コード例 #8
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);
        }
コード例 #9
0
 protected void LogAndAddReason(int ident, string reason, SwrveQAUser qaUser)
 {
     LogAndAddReason(reason, qaUser);
 }
コード例 #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);
        }