private void CreateInteraction(Contact contact, Guid goalId, string goalData) { using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { var newContact = new Sitecore.XConnect.Contact(); client.AddContact(newContact); // Create new interaction for this contact Guid channelId = Guid.NewGuid(); // Replace with channel ID from Sitecore string userAgent = "Mozilla/5.0 (Nintendo Switch; ShareApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341"; var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent); // Create new instance of goal Sitecore.XConnect.Goal goal = new Goal(goalId, DateTime.UtcNow); { }; goal.Data = goalData; // Add goal to interaction interaction.Events.Add(goal); // Add interaction operation to client client.AddInteraction(interaction); // Submit interaction client.Submit(); } catch (Exception ex) { // Handle exception } } }
public void RecordAnalytics(string[] contactDetails, string channel = null, string goalId = null, string chatId = null, string questionId = null, string answerId = null) { using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { Contact contact = null, existingContact = null; //// Retrieve contact existingContact = client.Get <Contact>(new IdentifiedContactReference(contactDetails[0], contactDetails[1]), new ContactExpandOptions()); if (existingContact != null) { contact = existingContact; } else { contact = new Contact(new ContactIdentifier(contactDetails[0], contactDetails[1], ContactIdentifierType.Known)); client.AddContact(contact); } if (contact != null) { var result = getInteractionFacets(contact.Id.GetValueOrDefault(), chatId, questionId, answerId); if (!result && !String.IsNullOrEmpty(answerId)) { var channelId = Guid.Parse(channel); string userAgent = HttpContext.Current.Request.UserAgent; Interaction interaction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent); ChatBotAnalytics CBData = new ChatBotAnalytics() { ChatId = chatId, Question = questionId, Answer = answerId }; client.SetFacet <ChatBotAnalytics>(interaction, ChatBotAnalytics.DefaultFacetKey, CBData); var offlineGoal = Guid.Parse(goalId); var xConnectEvent = new Goal(offlineGoal, DateTime.UtcNow); interaction.Events.Add(xConnectEvent); client.AddInteraction(interaction); client.Submit(); } } } catch (Exception ex) { Log.Info(ex.ToString(), this); } } }
public void RegisterGoal(Guid channelID, Guid goalID, string userAgent) { using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { // var contact = new Sitecore.XConnect.Contact(); //Guid channelID = Guid.NewGuid(); //string userAgent = "Sample User Agent"; //string globalCookie = Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"].Value; //var reference = new IdentifiedContactReference(Sitecore.XConnect.Constants.AliasIdentifierSource, globalCookie.Split('|')[0]); string trackerID = Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString(); //var reference = new IdentifiedContactReference(Sitecore.XConnect.Constants.AliasIdentifierSource,trackerID); var reference = new IdentifiedContactReference("xDB.Tracker", trackerID); Sitecore.XConnect.Contact contact = client.Get <Sitecore.XConnect.Contact>(reference, new ContactExpandOptions() { }); if (contact == null) { contact = new Sitecore.XConnect.Contact(); } var interaction = new Sitecore.XConnect.Interaction(contact, InteractionInitiator.Brand, channelID, userAgent); // Guid goalID = Guid.Parse("{9B6DC6A2-EB41-47B5-B10F-39AC37DE214F}"); // ID of goal item var goal = new Goal(goalID, DateTime.UtcNow); goal.EngagementValue = 20; // Manually setting engagement value rather than going to defintion item interaction.Events.Add(goal); client.AddContact(contact); client.AddInteraction(interaction); client.Submit(); } catch (XdbExecutionException ex) { // Handle exception } } }
/// <summary> /// This method adds the contact details in xdb - right now we are passing hardcoded data. /// </summary> public void AddContactDetails() { using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { // New anonymous contact //test var newContact = new Sitecore.XConnect.Contact(); client.AddContact(newContact); Guid channelId = Guid.Parse("86c7467a-d019-460d-9fa9-85d6d5d77fc4"); // Replace with real channel ID GUID string userAgent = "Astha Prasad"; // Interaction var interaction = new Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent); var fakeItemID = Guid.Parse("5746c4f3-7e16-40d9-ba1d-14c70875724c"); // Replace with real item ID Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, 3, "en") { Duration = new TimeSpan(0, 0, 30), Url = "/test/url/test/url?query=testing" }; interaction.Events.Add(pageView); client.AddInteraction(interaction); client.Submit(); } catch (XdbExecutionException ex) { // Handle exception } } }