예제 #1
0
        public virtual IEnumerable <VisitorSegment> ParseContacts(JToken definition, JobType type)
        {
            var segments = new List <VisitorSegment>();

            if (JobType.Contacts != type)
            {
                return(segments);
            }

            foreach (var contact in (JArray)definition)
            {
                if (contact["interactions"] == null)
                {
                    continue;
                }
                foreach (var interactionJObject in contact["interactions"])
                {
                    var segment     = new VisitorSegment(contact.Value <string>("email"));
                    var interaction = interactionJObject.ToObject <Interaction>();

                    //set city
                    if (interaction.GeoData != null)
                    {
                        var city = _geoDataRepository.Cities.FirstOrDefault(x => x.GeoNameId == interaction.GeoData.GeoNameId);
                        segment.VisitorVariables.Add(new GeoVariables(() => city));
                    }

                    //set contact
                    segment.VisitVariables.Add(ExtractContact(contact));

                    //set channel (can be overriden below)
                    segment.VisitVariables.Add(new SingleVisitorVariable <string>("Channel", visit => interaction.ChannelId));


                    //set search options
                    if (!string.IsNullOrEmpty(interaction.SearchEngine))
                    {
                        var searchEngine = SearchEngine.SearchEngines.First(s => s.Id.Equals(interaction.SearchEngine, StringComparison.InvariantCultureIgnoreCase));
                        if (!string.IsNullOrEmpty(interaction.SearchKeyword))
                        {
                            var searchKeywords = interaction.SearchKeyword.Split(' ');
                            segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => searchEngine, () => searchKeywords));
                        }
                    }

                    var pageItemInfos = interaction.Pages?.ToArray() ?? Enumerable.Empty <PageItemInfo>();
                    var pages         = new List <PageDefinition>();

                    foreach (var page in pageItemInfos)
                    {
                        var pageDefinition = new PageDefinition
                        {
                            Path = page.Path
                        };

                        //set goals
                        if (page.Goals != null)
                        {
                            pageDefinition.RequestVariables.Add("TriggerEvents", page.Goals.Select(x => new TriggerEventData
                            {
                                Id     = x.Id,
                                Name   = x.DisplayName,
                                IsGoal = true
                            }).ToList());
                        }
                        pages.Add(pageDefinition);
                    }


                    //set userAgent
                    SetUserAgent(segment);

                    //set datetime
                    segment.DateGenerator.Start = DateTime.Today.AddHours(12).AddMinutes(Randomness.Random.Next(-240, 240)).AddSeconds(Randomness.Random.Next(-240, 240)).Add(TimeSpan.Parse(interaction.Recency));


                    //set outcomes
                    if (interaction.Outcomes != null)
                    {
                        var outcomes = interaction.Outcomes.Select(x => x.Id.ToString());
                        var value    = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(() => new HashSet <string>(outcomes), value.Next));
                    }

                    //set campaign (can be overriden below)
                    if (!string.IsNullOrEmpty(interaction.CampaignId) && pageItemInfos.Any())
                    {
                        var pageItemInfo = pageItemInfos.First();
                        pageItemInfo.Path = pageItemInfo.Path + "?sc_camp=" + interaction.CampaignId;
                    }



                    var visitorBehavior = new StrictWalk(_sitecoreRoot, pages);
                    segment.Behavior = () => visitorBehavior;
                    segments.Add(segment);
                }
            }
            return(segments);
        }
예제 #2
0
        public virtual IEnumerable <VisitorSegment> ParseContacts(JToken definition, JobType type)
        {
            var segments = new List <VisitorSegment>();

            if (JobType.Contacts != type)
            {
                return(segments);
            }

            foreach (var contact in (JArray)definition)
            {
                if (contact["interactions"] == null)
                {
                    continue;
                }
                foreach (var interactionJObject in contact["interactions"])
                {
                    var segment     = new VisitorSegment(contact.Value <string>("email"));
                    var interaction = interactionJObject.ToObject <Interaction>();

                    //set city
                    if (interaction.GeoData != null)
                    {
                        var city = _geoDataRepository.Cities.FirstOrDefault(x => x.GeoNameId == interaction.GeoData.GeoNameId);
                        segment.VisitorVariables.Add(new GeoVariables(() => city));
                    }

                    //set contact
                    segment.VisitVariables.Add(ExtractContact(contact));

                    //set channel (can be overriden below)
                    segment.VisitVariables.Add(new SingleVisitorVariable <string>("Channel", visit => interaction.ChannelId));


                    //set search options
                    if (!string.IsNullOrEmpty(interaction.SearchEngine))
                    {
                        var searchEngine = SearchEngine.SearchEngines.First(s => s.Id.Equals(interaction.SearchEngine, StringComparison.InvariantCultureIgnoreCase));
                        if (!string.IsNullOrEmpty(interaction.SearchKeyword))
                        {
                            var searchKeywords = interaction.SearchKeyword.Split(' ');
                            segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => searchEngine, () => searchKeywords));
                        }
                    }
                    //set userAgent
                    SetUserAgent(segment);

                    //set datetime

                    //Visits while happen in this interval adjusted for the interaction's local time.
                    const double minHour = 6, maxHour = 23;

                    //Does the interaction have a time zone? If not use UTC.
                    var tz = interaction.GeoData?.TimeZoneInfo ?? TimeZoneInfo.Utc;

                    //Pick a random time between min and max hour. DateTimeKind.Unspecified is used because we are converting to local time next.
                    var startDate = DateTime.SpecifyKind(DateTime.Today.AddHours(minHour + Randomness.Random.NextDouble() * (maxHour - minHour)), DateTimeKind.Unspecified);

                    startDate = TimeZoneInfo.ConvertTimeToUtc(startDate, tz); //Convert from local time to UTC.

                    //"Recency" indicates how many days ago the visit happened. Hence substract this number of days.
                    int parseRecency;
                    var success = Int32.TryParse(interaction.Recency, out parseRecency);
                    if (!success)
                    {
                        throw new Exception("Error parsing interaction recency (days since interaction) to integer value");
                    }
                    var ensurePositiveRecency = Math.Abs(parseRecency);
                    segment.DateGenerator.Start = startDate.Add(-TimeSpan.Parse(ensurePositiveRecency.ToString()));

                    //set outcomes
                    if (interaction.Outcomes != null)
                    {
                        var outcomes = interaction.Outcomes.Select(x => x.Id.ToString());
                        var value    = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(() => new HashSet <string>(outcomes), value.Next));
                    }

                    var pageItemInfos = interaction.Pages?.ToArray() ?? Enumerable.Empty <PageItemInfo>();
                    var pages         = new List <PageDefinition>();

                    //set campaign (can be overriden below)
                    if (!string.IsNullOrEmpty(interaction.CampaignId) && pageItemInfos.Any())
                    {
                        var pageItemInfo = pageItemInfos.First();
                        pageItemInfo.Path = pageItemInfo.Path + "?sc_camp=" + interaction.CampaignId;
                    }

                    foreach (var page in pageItemInfos)
                    {
                        var pageDefinition = new PageDefinition
                        {
                            Path = page.Path
                        };

                        //set goals
                        if (page.Goals != null)
                        {
                            pageDefinition.RequestVariables.Add("TriggerEvents", page.Goals.Select(x => new TriggerEventData
                            {
                                Id     = x.Id,
                                Name   = x.DisplayName,
                                IsGoal = true
                            }).ToList());
                        }
                        pages.Add(pageDefinition);
                    }

                    var visitorBehavior = new StrictWalk(_sitecoreRoot, pages);
                    segment.Behavior = () => visitorBehavior;
                    segments.Add(segment);
                }
            }
            return(segments);
        }