Пример #1
0
        public Visitor GetAttributes(Visitor visitor)
        {
            if (visitor.Attributes == null)
            {
                visitor.Attributes = new Dictionary<string, string>();
            }

            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.GetVisitorAttributes))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        string key = null, value = null;

                        if (reader["Name"] != DBNull.Value)
                        {
                            key = reader["Name"].ToString();
                        }

                        if (reader["Value"] != DBNull.Value)
                        {
                            value = reader["Value"].ToString();
                        }

                        visitor.Attributes.Add(key, value);
                    }
                }

            }

            return visitor;
        }
Пример #2
0
        public Visitor AddVisitor(Visitor visitor)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitor))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);
                _db.AddInParameter(cmd, "@FirstVisit", DbType.DateTime, visitor.FirstVisit);
                _db.AddInParameter(cmd, "@UserName", DbType.String, visitor.UserName ?? string.Empty);
                _db.AddInParameter(cmd, "@IsAuthenticated", DbType.Int32, visitor.IsAuthenticated ? 1 : 0);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        var saved = ReaderToVisitor(reader);

                        //It's new, just take the ID
                        visitor.Id = saved.Id;
                    }
                }

            }

            return visitor;
        }
Пример #3
0
        protected void AddCohorts(Visitor visitor)
        {
            foreach (var cohort in visitor.Request.Cohorts)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorCohort))
                {
                    _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                    _db.AddInParameter(cmd, "@CohortId", DbType.Int32, cohort.Id);

                    _db.ExecuteNonQuery(cmd);
                }
            }
        }
Пример #4
0
        protected void AddAttributes(Visitor visitor)
        {
            foreach (var key in visitor.Request.Attributes.Keys)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorAttribute))
                {
                    _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                    _db.AddInParameter(cmd, "@Name", DbType.String, key);
                    _db.AddInParameter(cmd, "@Value", DbType.String, visitor.Request.Attributes[key]);

                    _db.ExecuteNonQuery(cmd);
                }
            }
        }
Пример #5
0
        public void RemoveVisitorAttribute(Visitor visitor, string name)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.RemoveVisitorAttribute))
            {
                _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                _db.AddInParameter(cmd, "@Name", DbType.String, name);

                _db.ExecuteNonQuery(cmd);
            }
        }
Пример #6
0
        /// <summary>
        /// Mostly for testing, allows you to specify experiment and visitor properties.
        /// </summary>
        /// <param name="experiment"></param>
        /// <param name="visitor"></param>
        /// <returns></returns>
        public ExperimentSegment GetHypothesis(Experiment experiment, Visitor visitor)
        {
            //See if user has current segments loaded.
            if (!visitor.IsNew && !visitor.ExperimentSegmentsLoaded)
            {
                visitor = _visitProvider.GetSegments(visitor);
                visitor.ExperimentSegmentsLoaded = true;
            }

            var existingSegment = visitor.ExperimentSegments.FirstOrDefault(a => a.ExperimentId == experiment.Id);
            if (existingSegment != null)
            {
                return existingSegment;
            }

            //check if user is in cohort.
            if (userIsInCohort(visitor, experiment.TargetCohort))
            {
                //Randomly select a segment
                int randNum = _random.Next(1, 100);
                double counter = 0;
                foreach (var segment in experiment.Segments)
                {
                    if (segment.TargetPercentage != 0)
                    {
                        if (counter + segment.TargetPercentage >= randNum)
                        {
                            visitor.Request.ExperimentSegments.Add(segment);
                            visitor.ExperimentSegments.Add(segment);
                            return segment;
                        }
                        counter += segment.TargetPercentage;
                    }
                }
            }
            else
            {
                //Get Default Segment
                return experiment.Segments.FirstOrDefault(a => a.IsDefault);
            }

            return null;
        }
Пример #7
0
        protected void AddSegments(Visitor visitor)
        {
            foreach (var segment in visitor.Request.ExperimentSegments)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorSegment))
                {
                    _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                    _db.AddInParameter(cmd, "@ExperimentId", DbType.Int32, segment.ExperimentId);
                    _db.AddInParameter(cmd, "@SegmentId", DbType.Int32, segment.Id);

                    _db.ExecuteNonQuery(cmd);
                }
            }
        }
Пример #8
0
        protected void AddReferrer(Visitor visitor)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorReferrer))
            {
                _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                _db.AddInParameter(cmd, "@ReferrerUrl", DbType.String, visitor.Request.FilteredReferrer);

                _db.ExecuteNonQuery(cmd);
            }
        }
Пример #9
0
        public Visitor GetDetails(Visitor visitor)
        {
            Visitor updated = null;

            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.GetVisitor))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        updated = ReaderToVisitor(reader);
                    }
                }

            }

            return updated;
        }
Пример #10
0
        protected Visitor ReaderToVisitor(IDataReader reader)
        {
            var visitor = new Visitor();

            if (reader["Id"] != DBNull.Value)
            {
                visitor.Id = Convert.ToInt32(reader["Id"]);
            }

            if (reader["UID"] != DBNull.Value)
            {
                visitor.UID = reader.GetGuid(reader.GetOrdinal("UID"));
            }

            if (reader["FirstVisit"] != DBNull.Value)
            {
                visitor.FirstVisit = Convert.ToDateTime(reader["FirstVisit"]);
            }

            if (reader["UserName"] != DBNull.Value)
            {
                visitor.UserName = reader["UserName"].ToString();
            }

            if (reader["IsAuthenticated"] != DBNull.Value)
            {
                visitor.IsAuthenticated = Convert.ToBoolean(reader["IsAuthenticated"]);
            }

            return visitor;
        }
Пример #11
0
        public void TestGetDefault()
        {
            Experiment exp = new Experiment()
            {
                Id = 100,
                Name = "TestExp",
                SystemName = "TestExp",
                TargetCohort = new AuthenticatedUsersCohort()
                {
                    Id = 200,
                    Name = "AllUsers",
                    SystemName = "AllUsers"
                },
                Segments = new List<ExperimentSegment>()
            };

            var segment1 = new ExperimentSegment()
            {
                Id = 300,
                ExperimentId = 100,
                TargetPercentage = 50,
                Name = "Option1",
                SystemName = "Option1"
            };

            exp.Segments.Add(segment1);

            var segment2 = new ExperimentSegment()
            {
                Id = 301,
                ExperimentId = 100,
                TargetPercentage = 50,
                Name = "Option2",
                SystemName = "Option2"
            };

            exp.Segments.Add(segment2);

            var defSeg = new ExperimentSegment()
            {
                Id = 302,
                ExperimentId = 100,
                TargetPercentage = 0,
                Name = "Default",
                SystemName = "Default",
                IsDefault=true
            };

            exp.Segments.Add(defSeg);

            var visitor = new Visitor()
            {
                ExperimentSegmentsLoaded = true,
                CohortsLoaded = true,
                AttributesLoaded = true,
                ConverstionsLoaded = true,
                DetailsLoaded = true,
                LandingUrlsLoaded = true,
                ReferrersLoaded = true,
                ExperimentSegments = new List<ExperimentSegment>(),
                Cohorts = new List<Cohort>(),
                Attributes = new Dictionary<string, string>(),
                Conversions = new List<Goal>(),
                FirstVisit = DateTime.Now,
                IsAuthenticated=false,
            };

            var hyp = ExperimentManager.Current.GetHypothesis(exp, visitor);

            Assert.IsNotNull(hyp);
            Assert.IsTrue(hyp == defSeg);
            Assert.AreEqual<int>(0, visitor.ExperimentSegments.Count);
            Assert.AreEqual<int>(0, visitor.Request.ExperimentSegments.Count);
        }
Пример #12
0
        private void validateAndLoadVisitor()
        {
            if (CurrentVisitor == null)
            {
                CurrentVisitor = GetVisitor(HttpContext.Current);
            }

            if (!CurrentVisitor.DetailsLoaded)
                {
                    //reload Details
                    Visitor saved = null;

                    if (!CurrentVisitor.IsNew)
                    {
                        saved = _visitProvider.GetDetails(CurrentVisitor);

                        if (saved != null && !saved.IsAuthenticated && HttpContext.Current.Request.IsAuthenticated)
                        {
                            //Need to Reconcile User
                            var oldRequest = CurrentVisitor.Request;
                            CurrentVisitor.UserName = HttpContext.Current.User.Identity.Name;
                            CurrentVisitor = _visitProvider.ReconcileUser(CurrentVisitor);
                            CurrentVisitor.Request = oldRequest;
                            CurrentVisitor.DetailsLoaded = true;

                            CurrentVisitor.IsAuthenticated = true;
                            CurrentVisitor.UserName = HttpContext.Current.User.Identity.Name;
                            //Need to save changes to user afterward regardless.
                            CurrentVisitor.IsDirty = true;
                            return;
                        }

                        if (saved == null)
                        {
                            //somehow we had a cookie, but no real visitor record
                            CurrentVisitor.FirstVisit = DateTime.Now;
                            if (HttpContext.Current.Request.IsAuthenticated)
                            {
                                CurrentVisitor.IsAuthenticated = true;
                                CurrentVisitor.UserName = HttpContext.Current.User.Identity.Name;
                            }
                            saved = _visitProvider.AddVisitor(CurrentVisitor);
                            CurrentVisitor.DetailsLoaded = true;

                        }
                    }
                    else
                    {
                        saved = CurrentVisitor;
                    }

                    if (!string.IsNullOrEmpty(saved.UserName) && HttpContext.Current.User != null && saved.UserName != HttpContext.Current.User.Identity.Name)
                    {
                        //Update CurrentVisitor to Saved User.
                        var oldRequest = CurrentVisitor.Request;
                        CurrentVisitor = _visitProvider.GetVisitor(HttpContext.Current.User.Identity.Name);
                        CurrentVisitor.Request = oldRequest;
                        CurrentVisitor.DetailsLoaded = true;
                        return;
                    }

                    CurrentVisitor.Id = saved.Id;
                    CurrentVisitor.IsAuthenticated = saved.IsAuthenticated;
                    CurrentVisitor.UserName = saved.UserName;
                    CurrentVisitor.DetailsLoaded = true;
                }
        }
Пример #13
0
 private bool userIsInCohort(Visitor visitor, Cohort cohort)
 {
     loadCohortPrerequisites(visitor, cohort);
     if (visitor.Cohorts.FirstOrDefault(a=>a.SystemName == cohort.SystemName) != null)
     {
         return true;
     }
     if (cohort.IsInCohort(visitor))
     {
         visitor.Request.Cohorts.Add(cohort);
         visitor.Cohorts.Add(cohort);
         return true;
     }
     return false;
 }
Пример #14
0
        private void loadCohortPrerequisites(Visitor visitor, Cohort cohort)
        {
            //New Visitors don't have anything else
            if (!visitor.IsNew)
            {
                if (!visitor.CohortsLoaded)
                {
                    CurrentVisitor = _visitProvider.GetCohorts(visitor);

                    //short circuit if user is in cohort already.
                    if (visitor.Cohorts.FirstOrDefault(a => a.SystemName == cohort.SystemName) != null)
                    {
                        return;
                    }
                }

                if (cohort.RequiresAttributes && !visitor.AttributesLoaded)
                {
                    visitor = _visitProvider.GetAttributes(visitor);
                    visitor.AttributesLoaded = true;
                }

                if (cohort.RequiresLandingUrls && !visitor.LandingUrlsLoaded)
                {
                    visitor = _visitProvider.GetLandingPages(visitor);
                    visitor.LandingUrlsLoaded = true;
                }

                if (cohort.RequiresLandingUrls && !string.IsNullOrEmpty(visitor.Request.LandingUrl))
                {
                    visitor.LandingUrls.Add(visitor.Request.LandingUrl);
                }

                if (cohort.RequiresReferrers && !visitor.ReferrersLoaded)
                {
                    visitor = _visitProvider.GetReferrers(visitor);
                    visitor.ReferrersLoaded = true;
                }

                if (cohort.RequiresReferrers && !string.IsNullOrEmpty(visitor.Request.FilteredReferrer))
                {
                    visitor.Referrers.Add(visitor.Request.FilteredReferrer);
                }

            }
        }
Пример #15
0
        protected Visitor GetVisitor(HttpContext context)
        {
            var request = context.Request;

            if (CurrentVisitor == null)
            {
                if (request.Cookies[_config.Web.Cookie.Name] != null)
                {
                    CurrentVisitor = new Visitor(new Guid(request.Cookies[_config.Web.Cookie.Name].Value));
                }
                else
                {
                    if (HttpContext.Current.Request.IsAuthenticated)
                    {
                        //Get Visitor based on UserName
                        CurrentVisitor = _visitProvider.GetVisitor(HttpContext.Current.User.Identity.Name);
                        if (CurrentVisitor != null)
                        {
                            CurrentVisitor.DetailsLoaded = true;
                        }
                    }

                    if (CurrentVisitor == null)
                    {
                        CurrentVisitor = new Visitor();
                        CurrentVisitor.FirstVisit = DateTime.Now;
                        CurrentVisitor.IsNew = true;

                        if (request.IsAuthenticated)
                        {
                            CurrentVisitor.IsAuthenticated = true;
                            CurrentVisitor.UserName = HttpContext.Current.User.Identity.Name;
                        }
                    }
                }
            }
            else
            {
                CurrentVisitor.IsNew = false;
            }

            CurrentVisitor.Request.RequestUrl = request.Url.ToString();
            CurrentVisitor.Request.ReferrerUrl = request.UrlReferrer != null ? request.UrlReferrer.ToString() : string.Empty;

            return CurrentVisitor;
        }
Пример #16
0
        protected void AddConversions(Visitor visitor)
        {
            foreach (var goal in visitor.Request.Conversions)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorConversion))
                {
                    _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                    _db.AddInParameter(cmd, "@GoalId", DbType.Int32, goal.Id);

                    _db.ExecuteNonQuery(cmd);
                }
            }
        }
Пример #17
0
        protected void AddLandingPage(Visitor visitor)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorLandingPage))
            {
                _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                _db.AddInParameter(cmd, "@LandingPageUrl", DbType.String, visitor.Request.LandingUrl);

                _db.ExecuteNonQuery(cmd);
            }
        }
Пример #18
0
        public Visitor GetLandingPages(Visitor visitor)
        {
            if (visitor.LandingUrls == null)
            {
                visitor.LandingUrls = new List<string>();
            }

            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.GetVisitorLandingPages))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {

                        if (reader["LandingPageUrl"] != DBNull.Value)
                        {
                            visitor.LandingUrls.Add(reader["LandingPageUrl"].ToString());
                        }
                    }
                }

            }

            return visitor;
        }
Пример #19
0
        protected void AddRequest(Visitor visitor)
        {
            if (visitor.Request.RequestUrl.IndexOf("Glimpse.axd") == -1)
            {
                using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.AddVisitorRequest))
                {
                    _db.AddInParameter(cmd, "@VisitorId", DbType.Int32, visitor.Id);
                    _db.AddInParameter(cmd, "@RequestUrl", DbType.String, visitor.Request.RequestUrl);

                    _db.ExecuteNonQuery(cmd);
                }
            }
        }
Пример #20
0
        public Visitor ReconcileUser(Visitor visitor)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.ReconcileVisitor))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);
                _db.AddInParameter(cmd, "@UserName", DbType.String, visitor.UserName);

                _db.AddOutParameter(cmd, "@ExistingID", DbType.Int32, 8);
                _db.AddOutParameter(cmd, "@VisitorID", DbType.Int32, 8);

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {

                        var oldRequest = visitor.Request;

                        visitor = ReaderToVisitor(reader);

                        //update to user original request to preserve inflight changes
                        visitor.Request = oldRequest;
                    }
                }

            }

            return visitor;
        }
Пример #21
0
        protected void UpdateVisitor(Visitor visitor)
        {
            using (DbCommand cmd = _db.GetSqlStringCommand(MtelligentQueries.UpdateVisitor))
            {
                _db.AddInParameter(cmd, "@UID", DbType.Guid, visitor.UID);
                _db.AddInParameter(cmd, "@UserName", DbType.String, visitor.UserName ?? string.Empty);
                _db.AddInParameter(cmd, "@IsAuthenticated", DbType.Int32, visitor.IsAuthenticated ? 1 : 0);

                _db.ExecuteNonQuery(cmd);
            }
        }
Пример #22
0
 public virtual bool IsInCohort(Visitor visitor)
 {
     return false;
 }