public Entities.Visitor GetCohorts(Entities.Visitor visitor)
        {
            if (visitor.Cohorts == null)
            {
                visitor.Cohorts = new List <Cohort>();
            }

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

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        visitor.Cohorts.Add(ReaderToCohort(reader));
                    }

                    reader.NextResult();

                    while (reader.Read())
                    {
                        int cohortId = Convert.ToInt32(reader["CohortId"]);
                        var cohort   = visitor.Cohorts.FirstOrDefault(a => a.Id == cohortId);
                        if (cohort != null)
                        {
                            var kvp = ReaderToKVP(reader);
                            cohort.Properties.Add(kvp.Key, kvp.Value);
                        }
                    }
                }
            }

            return(visitor);
        }
        public Entities.Visitor GetSegments(Entities.Visitor visitor)
        {
            if (visitor.ExperimentSegments == null)
            {
                visitor.ExperimentSegments = new List <ExperimentSegment>();
            }

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

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        visitor.ExperimentSegments.Add(ReaderToExperimentSegment(reader));
                    }

                    reader.NextResult();

                    while (reader.Read())
                    {
                        int segmentId = Convert.ToInt32(reader["ExperimentSegmentId"]);
                        var segment   = visitor.ExperimentSegments.FirstOrDefault(a => a.Id == segmentId);
                        if (segment != null)
                        {
                            var variableName = reader["Name"].ToString();
                            segment.Variables.Add(variableName, reader["Value"].ToString());
                        }
                    }
                }
            }

            return(visitor);
        }
        public Entities.Visitor GetReferrers(Entities.Visitor visitor)
        {
            if (visitor.Referrers == null)
            {
                visitor.Referrers = new List <string>();
            }

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

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        if (reader["ReferrerUrl"] != DBNull.Value)
                        {
                            visitor.Referrers.Add(reader["ReferrerUrl"].ToString());
                        }
                    }
                }
            }

            return(visitor);
        }
        public Entities.Visitor GetConversions(Entities.Visitor visitor)
        {
            if (visitor.Conversions == null)
            {
                visitor.Conversions = new List <Goal>();
            }

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

                using (IDataReader reader = _db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        visitor.Conversions.Add(ReaderToGoal(reader));
                    }
                }
            }

            return(visitor);
        }
        public Entities.Visitor SaveChanges(Entities.Visitor visitor, bool saveRequest)
        {
            if (visitor.IsNew)
            {
                visitor = AddVisitor(visitor);
            }
            else
            {
                if (visitor.IsDirty)
                {
                    UpdateVisitor(visitor);
                }
            }

            //We need the ID for the rest of the inserts.
            if (visitor.Id == 0 && (
                    (visitor.Request.Attributes != null && visitor.Request.Attributes.Count > 0) ||
                    (visitor.Request.Cohorts != null && visitor.Request.Cohorts.Count > 0) ||
                    (visitor.Request.Conversions != null && visitor.Request.Conversions.Count > 0) ||
                    (visitor.Request.ExperimentSegments != null && visitor.Request.ExperimentSegments.Count > 0) ||
                    (!string.IsNullOrEmpty(visitor.Request.FilteredReferrer)) ||
                    (!string.IsNullOrEmpty(visitor.Request.LandingUrl)) ||
                    saveRequest
                    ))
            {
                var saved = GetDetails(visitor);
                visitor.Id = saved.Id;
            }

            if (visitor.Request.Attributes != null && visitor.Request.Attributes.Count > 0)
            {
                AddAttributes(visitor);
            }

            if (visitor.Request.Cohorts != null && visitor.Request.Cohorts.Count > 0)
            {
                AddCohorts(visitor);
            }

            if (visitor.Request.Conversions != null && visitor.Request.Conversions.Count > 0)
            {
                AddConversions(visitor);
            }

            if (visitor.Request.ExperimentSegments != null && visitor.Request.ExperimentSegments.Count > 0)
            {
                AddSegments(visitor);
            }

            if (!string.IsNullOrEmpty(visitor.Request.FilteredReferrer))
            {
                AddReferrer(visitor);
            }

            if (!string.IsNullOrEmpty(visitor.Request.LandingUrl))
            {
                AddLandingPage(visitor);
            }

            if (saveRequest)
            {
                AddRequest(visitor);
            }

            return(visitor);
        }