private static void SetWebVisit(XConnectClient client, Interaction interaction, Action <Interaction> visitPageAction) { visitPageAction(interaction); var facet = new WebVisit(); // Populate data about the web visit facet.Browser = new BrowserData() { BrowserMajorName = "Chrome", BrowserMinorName = "Desktop", BrowserVersion = "22.0" }; facet.Language = "en"; facet.OperatingSystem = new OperatingSystemData() { Name = "Windows", MajorVersion = "10", MinorVersion = "4" }; facet.Referrer = "www.google.com"; facet.Screen = new ScreenData() { ScreenHeight = 1080, ScreenWidth = 685 }; facet.SearchKeywords = "banana smoothie chocolate"; facet.SiteName = "website"; client.SetWebVisit(interaction, facet); }
public void RegisterOfflineInteraction(string source, string identifier, FaceApiFacet face) { using (XConnectClient client = GetClient()) { try { IdentifiedContactReference reference = new IdentifiedContactReference(source, identifier); Contact existingContact = client.Get <Contact>( reference, new ContactExpandOptions(PersonalInformation.DefaultFacetKey)); if (existingContact == null) { return; } Interaction interaction = new Interaction(existingContact, InteractionInitiator.Contact, channelId: Settings.OfflineChannelId, userAgent: "video camera"); //Add Device profile DeviceProfile newDeviceProfile = new DeviceProfile(Guid.NewGuid()); newDeviceProfile.LastKnownContact = existingContact; client.AddDeviceProfile(newDeviceProfile); interaction.DeviceProfile = newDeviceProfile; //Add fake Ip info IpInfo fakeIpInfo = new IpInfo("127.0.0.1"); fakeIpInfo.BusinessName = "Home"; client.SetFacet <IpInfo>(interaction, IpInfo.DefaultFacetKey, fakeIpInfo); //Add fake webvisit WebVisit fakeWebVisit = new WebVisit(); fakeWebVisit.SiteName = "Offline"; client.SetFacet <WebVisit>(interaction, WebVisit.DefaultFacetKey, fakeWebVisit); //Adding FaceApi facet client.SetFacet <FaceApiFacet>(interaction, FaceApiFacet.DefaultFacetKey, face); Outcome outcome = new Outcome(Settings.OfflineGoalId, DateTime.UtcNow, "USD", 0); outcome.EngagementValue = 10; outcome.Text = "Face recognized"; interaction.Events.Add(outcome); client.AddInteraction(interaction); client.Submit(); } catch (XdbExecutionException ex) { } } }
public IHttpActionResult Put(WebVisit webVisit) { string response = crudFuction.BizUpdateVisit(webVisit.WebVisitToBizVisit()); if (!response.Equals("EXITO")) { return(BadRequest(response)); } else { return(Ok(response)); } }
public void RegisterOnlineInteraction(string source, string identifier) { using (XConnectClient client = GetClient()) { try { IdentifiedContactReference reference = new IdentifiedContactReference(source, identifier); Contact existingContact = client.Get <Contact>( reference, new ContactExpandOptions(PersonalInformation.DefaultFacetKey)); if (existingContact == null) { return; } Goal goal = new Goal(Settings.OnlineGoalId, DateTime.UtcNow); goal.EngagementValue = 100; goal.Text = "Agenda Signup"; Interaction interaction = new Interaction(existingContact, InteractionInitiator.Contact, channelId: Settings.OnlineChannelId, userAgent: "laptop/demo site"); interaction.Events.Add(goal); //Add Device profile DeviceProfile newDeviceProfile = new DeviceProfile(Guid.NewGuid()); newDeviceProfile.LastKnownContact = existingContact; client.AddDeviceProfile(newDeviceProfile); interaction.DeviceProfile = newDeviceProfile; //Add fake Ip info IpInfo fakeIpInfo = new IpInfo("127.0.0.1"); fakeIpInfo.BusinessName = "Home"; client.SetFacet <IpInfo>(interaction, IpInfo.DefaultFacetKey, fakeIpInfo); //Add fake webvisit WebVisit fakeWebVisit = new WebVisit(); fakeWebVisit.SiteName = "Online"; client.SetFacet <WebVisit>(interaction, WebVisit.DefaultFacetKey, fakeWebVisit); client.AddInteraction(interaction); client.Submit(); } catch (XdbExecutionException ex) { } } }
private static void AddInteraction(XConnectClient client, Contact contact) { // Create a new interaction for the contact Guid channelId = new Guid("{1DA15267-B0DB-4BE1-B44F-E57C2EEB8A6B}"); string userAgent = HttpContext.Current.Request.UserAgent; Interaction webInteraction = new Interaction(contact, InteractionInitiator.Contact, channelId, userAgent); // Create a new web visit facet model var webVisitFacet = new WebVisit(); // Populate data about the web visit HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser; webVisitFacet.Browser = new BrowserData() { BrowserMajorName = browser.Browser, BrowserMinorName = browser.Platform, BrowserVersion = browser.Version }; webVisitFacet.Language = Sitecore.Context.Language.Name; webVisitFacet.OperatingSystem = new OperatingSystemData() { Name = Environment.OSVersion.VersionString, MajorVersion = Environment.OSVersion.Version.Major.ToString(), MinorVersion = Environment.OSVersion.Version.Minor.ToString() }; webVisitFacet.Referrer = HttpContext.Current.Request.UrlReferrer != null ? HttpContext.Current.Request.UrlReferrer.AbsoluteUri : string.Empty; webVisitFacet.Screen = new ScreenData() { ScreenHeight = browser.ScreenPixelsHeight, ScreenWidth = browser.ScreenPixelsWidth }; webVisitFacet.SearchKeywords = "wechat"; webVisitFacet.SiteName = Sitecore.Context.Site.Name; var item = Sitecore.Context.Item; //page view PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, item.ID.ToGuid(), item.Version.Number, item.Language.Name) { Duration = new TimeSpan(0, 0, 30), Url = HttpContext.Current.Request.Url.PathAndQuery }; webInteraction.Events.Add(pageView); // Set web visit facet on interaction client.SetWebVisit(webInteraction, webVisitFacet); // Add interaction client.AddInteraction(webInteraction); }
/// <summary> /// Bizs the visit to web visit. /// </summary> /// <param name="DatVisit">The dat visit.</param> /// <returns>WebVisit.</returns> public static WebVisit BizVisitToWebVisit(this BizVisit DatVisit) { WebVisit webVisit = new WebVisit { Id = DatVisit.Id, UserId = DatVisit.UserId, VisitId = DatVisit.VisitId, CheckIn = DatVisit.CheckIn, CheckOut = DatVisit.CheckOut, VisitName = DatVisit.VisitName, Carnet = DatVisit.Carnet, Plates = DatVisit.Plates, Nombre = DatVisit.Nombre, TypeVisit = DatVisit.TypeVisit, }; return(webVisit); }
private void FillTableWithRow(DataTable rawTable, Interaction curInteraction, int index = 1) { WebVisit webVisit = CollectionModel.WebVisit(curInteraction); IpInfo ipInfo = CollectionModel.IpInfo(curInteraction); int count = Enumerable.ToList <PageViewEvent>(Enumerable.OfType <PageViewEvent>((IEnumerable)curInteraction.Events)).Count; DataRow row = rawTable.NewRow(); row["ContactId"] = (object)curInteraction.Contact.Id; row["_id"] = (object)curInteraction.Id; row["ChannelId"] = (object)curInteraction.ChannelId; row["TrafficType"] = (object)0; row["StartDateTime"] = (object)curInteraction.StartDateTime; row["EndDateTime"] = (object)curInteraction.EndDateTime; row["AspNetSessionId"] = (object)Guid.Empty; if (curInteraction.CampaignId.HasValue) { row["CampaignId"] = (object)curInteraction.CampaignId; } row["ContactVisitIndex"] = (object)index; row["DeviceId"] = curInteraction.DeviceProfile == null ? DBNull.Value : (object)curInteraction.DeviceProfile.Id; row["LocationId"] = (object)Guid.Empty; row["UserAgent"] = (object)curInteraction.UserAgent; row["SiteName"] = webVisit == null ? DBNull.Value : (object)webVisit.SiteName; row["Value"] = (object)curInteraction.EngagementValue; row["VisitPageCount"] = (object)count; if (ipInfo != null && ipInfo.IpAddress != null) { row["Ip"] = (object)ipInfo.IpAddress.ToString(); } row["Keywords"] = webVisit == null ? DBNull.Value : (object)webVisit.SearchKeywords; if (webVisit != null && !string.IsNullOrEmpty(webVisit?.Referrer)) { Uri uri = new Uri(webVisit?.Referrer); row["ReferringSite"] = (object)uri.Host; } row["GeoData_BusinessName"] = ipInfo == null ? DBNull.Value : (object)ipInfo.BusinessName; row["GeoData_City"] = ipInfo == null ? DBNull.Value : (object)ipInfo.City; row["GeoData_Region"] = ipInfo == null ? DBNull.Value : (object)ipInfo.Region; row["GeoData_Country"] = ipInfo == null ? DBNull.Value : (object)ipInfo.Country; rawTable.Rows.Add(row); }
static public long SaveVisit(this string user_name, bool?authentication, string authentication_type, string user_host_name, string user_host_address, string url, string physical_path, string areas, string controller, string action, string roles_access, bool?access) { try { EFWebVisit ef_visit = new EFWebVisit(new EFDbContext()); WebVisit new_visit = new WebVisit() { id = 0, date_time = DateTime.Now, user_name = user_name, authentication = authentication, authentication_type = authentication_type, user_host_name = user_host_name, user_host_address = user_host_address, url = url, physical_path = physical_path, areas = areas, controller = controller, action = action, roles_access = roles_access, access = access }; ef_visit.Add(new_visit); ef_visit.Save(); ef_visit.Refresh(new_visit); return(new_visit.id); } catch (Exception e) { Console.WriteLine(String.Format("Ошибка выполнения метода SaveVisit(user_name={0},authentication={1}, authentication_type={2}, user_host_name={3}, user_host_address={4}, url={5}, physical_path={6}, areas={7}, controller={8}, action={9}, roles_access={10}, access={11}). Exception:{12}", user_name, authentication, authentication_type, user_host_name, user_host_address, url, physical_path, areas, controller, action, roles_access, access, e)); return(-1); } }
// if addWebVisit=true, fake webvisit will be created for interaction // it is needed if you want to populate interaction country (to use contacts country for ML data model) public async Task <bool> Add(Customer purchase, bool addWebVisit = false) { using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient()) { try { IdentifiedContactReference reference = new IdentifiedContactReference(IdentificationSource, purchase.CustomerId.ToString()); var customer = await client.GetAsync( reference, new ContactExpandOptions( PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey, ContactBehaviorProfile.DefaultFacetKey ) { Interactions = new RelatedInteractionsExpandOptions { StartDateTime = DateTime.MinValue, EndDateTime = DateTime.MaxValue, Limit = 100 } } ); if (customer == null) { var email = "demo" + Guid.NewGuid().ToString("N") + "@gmail.com"; customer = new Contact(new ContactIdentifier(IdentificationSource, purchase.CustomerId.ToString(), ContactIdentifierType.Known)); var preferredEmail = new EmailAddress(email, true); var emails = new EmailAddressList(preferredEmail, "Work"); client.AddContact(customer); client.SetEmails(customer, emails); var identifierEmail = new ContactIdentifier(IdentificationSourceEmail, email, ContactIdentifierType.Known); client.AddContactIdentifier(customer, identifierEmail); client.Submit(); } var channel = Guid.Parse("DF9900DE-61DD-47BF-9628-058E78EF05C6"); var interaction = new Interaction(customer, InteractionInitiator.Brand, channel, "demo app"); if (addWebVisit) { //Add Device profile DeviceProfile newDeviceProfile = new DeviceProfile(Guid.NewGuid()) { LastKnownContact = customer }; client.AddDeviceProfile(newDeviceProfile); interaction.DeviceProfile = newDeviceProfile; //Add fake Ip info IpInfo fakeIpInfo = new IpInfo("127.0.0.1") { BusinessName = "Home" }; var country = purchase.Invoices.FirstOrDefault(x => !string.IsNullOrEmpty(x.Country))?.Country; fakeIpInfo.Country = GetCountryCodeByName(country); client.SetFacet(interaction, IpInfo.DefaultFacetKey, fakeIpInfo); // Add fake webvisit // Create a new web visit facet model var webVisitFacet = new WebVisit { Browser = new BrowserData { BrowserMajorName = "Chrome", BrowserMinorName = "Desktop", BrowserVersion = "22.0" }, Language = "en", OperatingSystem = new OperatingSystemData { Name = "Windows", MajorVersion = "10", MinorVersion = "4" }, Referrer = "https://www.google.com", Screen = new ScreenData { ScreenHeight = 1080, ScreenWidth = 685 }, SearchKeywords = "sitecore", SiteName = "website" }; // Populate data about the web visit var itemId = Guid.Parse("110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9"); var itemVersion = 1; // First page view var datetime = purchase.Invoices.FirstOrDefault() == null ? DateTime.Now : purchase.Invoices.First().TimeStamp.ToUniversalTime(); PageViewEvent pageView = new PageViewEvent(datetime, itemId, itemVersion, "en") { ItemLanguage = "en", Duration = new TimeSpan(3000), Url = "/home" }; // client.SetFacet(interaction, WebVisit.DefaultFacetKey, webVisitFacet); interaction.Events.Add(pageView); client.SetWebVisit(interaction, webVisitFacet); } foreach (var invoice in purchase.Invoices) { var outcome = new PurchaseOutcome(PurchaseOutcome.PurchaseEventDefinitionId, invoice.TimeStamp, invoice.Currency, invoice.Price, invoice.Number, invoice.Quantity, purchase.CustomerId, invoice.StockCode); interaction.Events.Add(outcome); } client.AddInteraction(interaction); await client.SubmitAsync(); return(true); } catch (XdbExecutionException ex) { Log.Error(ex.Message, ex, this); return(false); } } }
public void Trigger(object stateInfo) { try { if (webTrackingInProgress) { return; } webTrackingInProgress = true; var accountsWithWebAnalytics = accountService.GetWebAnalyticsProviders(new GetWebAnalyticsProvidersRequest()).WebAnalyticsProviders; DateTime currentTime = DateTime.Now.ToUniversalTime(); string currentTimeString = string.Format("{0:u}", currentTime).Replace(":", "-"); dynamic UniqueVisitors; APIManager apiManager = new APIManager(); string apiLookupStringPath = ConfigurationManager.AppSettings["WEBANALYTICS_API_RESPONSE_PATH"] + "/APILookUp" + "/APILookUp_UTC_" + currentTimeString; string smartTouchPartnerKey = ConfigurationManager.AppSettings["SMARTTOUCH_PARTNER_KEY"]; JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); ValidatePhysicalPaths(); apiManager.APICall("http://api.visistat.com/api-lookup.php?hid=" + smartTouchPartnerKey + "&act=2", apiLookupStringPath); var initialSetupProcessingStartHour = ConfigurationManager.AppSettings["INITIAL_SETUP_PROCESSING_START_HOUR"]; var initialSetupProcessingStopHour = ConfigurationManager.AppSettings["INITIAL_SETUP_PROCESSING_STOP_HOUR"]; var splitVisitInterval = ConfigurationManager.AppSettings["SPLIT_VISIT_INTERVAL"]; IList <dynamic> accountTimeZones = new List <dynamic>(); using (StreamReader streamReader = new StreamReader(apiLookupStringPath + ".txt")) { while (streamReader.Peek() >= 0) { var readLine = streamReader.ReadLine(); if (string.IsNullOrEmpty(readLine)) { break; } var accountInfo = readLine.Split('|').ToArray(); accountTimeZones.Add(new { APIKey = accountInfo[11], TimeZoneID = accountInfo[12], TrackingDomain = accountInfo[2] }); } } #region -- Processing -- foreach (var webAnalyticsProvider in accountsWithWebAnalytics) { var accountCurrentTime = currentTime; Logger.Current.Verbose("accountCurrentTime : " + accountCurrentTime.ToString()); IList <WebVisit> WebVisits = new List <WebVisit>(); var isInitialSetup = webAnalyticsProvider.LastAPICallTimeStamp == null; if (!isInitialSetup || (isInitialSetup && accountCurrentTime.Hour > int.Parse(initialSetupProcessingStartHour) && accountCurrentTime.Hour < int.Parse(initialSetupProcessingStopHour))) { IList <string> identities = new List <string>(); Logger.Current.Informational("Analyzing web visits for web analytics provider id: " + webAnalyticsProvider.Id + ". AccountID: " + webAnalyticsProvider.AccountID); var content = ""; try { webAnalyticsProvider.LastAPICallTimeStamp = webAnalyticsProvider.LastAPICallTimeStamp != null ? webAnalyticsProvider.LastAPICallTimeStamp.Value : accountCurrentTime.AddDays(-180); var accountTimeZone = accountTimeZones.Where(tz => tz.APIKey == webAnalyticsProvider.APIKey).Select(tz => tz.TimeZoneID).FirstOrDefault(); var accountUTCTimeSpan = VisiStatTimeZones.TimeZones().Where(tz => tz.TimeZoneId == byte.Parse(accountTimeZone)).Select(tz => tz.TimeSpan).FirstOrDefault(); var accountTimeZoneId = VisiStatTimeZones.TimeZones().Where(tz => tz.TimeZoneId == byte.Parse(accountTimeZone)).Select(tz => tz.GlobalTimeZoneId).FirstOrDefault(); TimeZoneInfo tzInfo = TimeZoneInfo.FindSystemTimeZoneById(accountTimeZoneId); var isDaylightSaving = tzInfo.IsDaylightSavingTime(TimeZoneInfo.ConvertTime(webAnalyticsProvider.LastAPICallTimeStamp.Value, tzInfo)); // http://www.timeanddate.com/time/change/usa/new-york?year=2015 if (isDaylightSaving) { accountUTCTimeSpan = accountUTCTimeSpan.Add(new TimeSpan(1, 0, 0)); } var accountStartDate = webAnalyticsProvider.LastAPICallTimeStamp.Value.AddHours(accountUTCTimeSpan.Hours).AddMinutes(accountUTCTimeSpan.Minutes); var accountEndDate = accountCurrentTime.AddHours(accountUTCTimeSpan.Hours).AddMinutes(accountUTCTimeSpan.Minutes); TimeSpan startEndTimeSpan = (TimeSpan)(accountEndDate - accountStartDate); if (startEndTimeSpan.TotalHours > 3) { accountEndDate = accountStartDate.AddHours(3); accountCurrentTime = webAnalyticsProvider.LastAPICallTimeStamp.Value.AddHours(3); Logger.Current.Verbose("accountEndDate: " + accountEndDate.ToString()); } string accountStartDateString = String.Format("{0:u}", accountStartDate); string accountEndDateString = String.Format("{0:u}", accountEndDate); int year = accountStartDate.Year; int month = accountStartDate.Month; var identityLocation = Path.Combine(ConfigurationManager.AppSettings["WEBANALYTICS_API_RESPONSE_PATH"], "Received", webAnalyticsProvider.AccountID.ToString(), year.ToString(), month.ToString()); if (!Directory.Exists(identityLocation)) { Directory.CreateDirectory(identityLocation); } string uvIdentitiesLocation = Path.Combine(identityLocation, "UVByIdentity_" + accountEndDateString.Replace(":", "-") + "_AccID_" + webAnalyticsProvider.AccountID + "_WAID_" + webAnalyticsProvider.Id); string uvByIddUrl = "http://api.visistat.com/stats-api-v25.php?key=" + webAnalyticsProvider.APIKey + "&qt=idd&d=json&sdate=" + accountStartDateString + "&edate=" + accountEndDateString; Logger.Current.Informational("AccountId: " + webAnalyticsProvider.AccountID + ", uvByIddURL: " + uvByIddUrl); content = apiManager.APICall(uvByIddUrl, uvIdentitiesLocation); UniqueVisitors = (dynamic)jsonSerializer.DeserializeObject(content); foreach (dynamic item in UniqueVisitors) { string identity = item[1]; identities.Add(identity); } IList <string> knownIdentities = contactService .CompareKnownIdentities(new CompareKnownContactIdentitiesRequest() { ReceivedIdentities = identities, AccountId = webAnalyticsProvider.AccountID, } ).KnownIdentities.ToList(); IEnumerable <string> cpByIdentityLocation = new List <string>(); cpByIdentityLocation = apiManager.GetCPByIdentityFiles(webAnalyticsProvider, knownIdentities, accountStartDateString, accountEndDateString); int ipIterator = 0; foreach (string location in cpByIdentityLocation) { string[] readFile = File.ReadAllLines(location + ".txt"); var visits = (dynamic)jsonSerializer.DeserializeObject(readFile[0]); int visitIterator = 0; foreach (var visit in visits) { if (visit[2] != "city") { DateTime currentPageVisitedOn = Convert.ToDateTime(visit[0]); DateTime nextPageVisitedOn = visitIterator > 1 ? Convert.ToDateTime(visits[visitIterator - 1][0]) : currentPageVisitedOn; //if (isDaylightSaving) //{ // currentPageVisitedOn = currentPageVisitedOn.AddHours(-1); // nextPageVisitedOn = nextPageVisitedOn.AddHours(-1); //} WebVisit newWebVisit = new WebVisit() { VisitedOn = currentPageVisitedOn.AddHours(-accountUTCTimeSpan.Hours).AddMinutes(-accountUTCTimeSpan.Minutes), PageVisited = visit[1], City = visit[2], State = visit[3], Country = visit[4], ISPName = visit[7], ContactReference = knownIdentities[ipIterator], IPAddress = visit[6], }; if (visitIterator > 1) { newWebVisit.Duration = (short)nextPageVisitedOn.Subtract(currentPageVisitedOn).TotalSeconds; } WebVisits.Add(newWebVisit); } visitIterator = visitIterator + 1; } ipIterator = ipIterator + 1; } try { Logger.Current.Verbose("After collecting the info the accountCurrentTime : " + accountCurrentTime.ToString()); webAnalyticsService.AddContactWebVisits(new AddContactWebVisitRequest() { ContactWebVisits = WebVisits, LastAPICallTimeStamp = accountCurrentTime, WebAnalyticsProvider = webAnalyticsProvider, SplitVisitInterval = short.Parse(splitVisitInterval) }); } catch (Exception ex) { Logger.Current.Error("Exception occurred while saving the web visits to the database. AccountID: " + webAnalyticsProvider.AccountID, ex); } } catch (Exception ex) { Logger.Current.Error("Exception occurred while making an API call. AccountID: " + webAnalyticsProvider.AccountID, ex); } Logger.Current.Informational("Analyzing web visits for web analytics provider id: " + webAnalyticsProvider.Id + " is completed"); } } #endregion } catch (Exception ex) { Logger.Current.Error("An exception occured with getting web analytics.", ex); } webTrackingInProgress = false; }