protected void setData(KeyValuePair <Guid, string> keyValuePair) { // Initialize a client using the validated configuration using (var client = XConnectHelper.GetClient()) { try { var channelId = Guid.Parse("52B75873-4CE0-4E98-B63A-B535739E6180"); // "email newsletter" channel // Create a new contact with the identifier Contact knownContact = new Contact(); PersonalInformation personalInfoFacet = new PersonalInformation(); personalInfoFacet.FirstName = "Abhi" + Guid.NewGuid().ToString(); personalInfoFacet.LastName = "Marwah"; personalInfoFacet.JobTitle = "Sitecore Architect"; personalInfoFacet.Gender = "Male"; personalInfoFacet.Nickname = "Aussie"; client.SetFacet <PersonalInformation>(knownContact, PersonalInformation.DefaultFacetKey, personalInfoFacet); EmailAddressList emails = new EmailAddressList(new EmailAddress("*****@*****.**", true), "Email"); client.SetFacet(knownContact, emails); PageViewEvent pageView = new PageViewEvent(DateTime.Now.ToUniversalTime(), keyValuePair.Key, 1, "en"); pageView.ItemLanguage = "en"; pageView.Duration = new TimeSpan(3000); pageView.SitecoreRenderingDevice = new SitecoreDeviceData(new Guid("{fe5d7fdf-89c0-4d99-9aa3-b5fbd009c9f3}"), "Default"); pageView.Url = keyValuePair.Value; client.AddContact(knownContact); // Create a new interaction for that contact Interaction interaction = new Interaction(knownContact, InteractionInitiator.Brand, channelId, ""); // Add events - all interactions must have at least one event interaction.Events.Add(pageView); IpInfo ipInfo = new IpInfo("127.0.0.1"); ipInfo.BusinessName = "Sitecore Consultancy"; client.SetFacet <IpInfo>(interaction, IpInfo.DefaultFacetKey, ipInfo); // Add the contact and interaction client.AddInteraction(interaction); // Submit contact and interaction - a total of two operations client.Submit(); } catch (XdbExecutionException ex) { // Deal with exception } } }
public static int GetPageViewsCount(string itemId) { int counter = 0; using (var client = XConnectHelper.GetClient()) { try { IAsyncQueryable <Sitecore.XConnect.Contact> queryable = client.Contacts .Where(c => c.Interactions.Any(f => f.Events.OfType <PageViewEvent>().Any())) .WithExpandOptions(new Sitecore.XConnect.ContactExpandOptions() { Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions() { Limit = 2000 // Returns top 20 of all contact's interactions - interactions not affected by query } }); var enumerable = queryable.GetBatchEnumeratorSync(10); var urlList = new List <string>(); while (enumerable.MoveNext()) { foreach (var contact in enumerable.Current) { InteractionReference interactionReference = new InteractionReference((Guid)contact.Id, (Guid)contact.Interactions.ElementAt(0).Id); Interaction interaction = client.Get <Interaction>(interactionReference, new InteractionExpandOptions(new string[] { IpInfo.DefaultFacetKey }) { Contact = new RelatedContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey }) }); var pageViews = interaction.Events.OfType <PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList(); foreach (var page in pageViews) { // item id is in page if (page.ItemId.Equals(Guid.Parse(itemId))) { counter++; } urlList.Add(page.Url); } Console.WriteLine("Page Url: " + pageViews.FirstOrDefault().Url); } } //var count = urlList.GroupBy(x => x).Select(y => new { } x.Count()); var count = urlList.GroupBy(n => n).Select(c => new { Key = c.Key, total = c.Count() }); return(counter); } catch (XdbExecutionException ex) { } return(counter); } }