1

I'm using sitecore 9.1.1 and SQL server is used for xDB.

my concern is that sitecore also adding interaction for every page visit and instead of adding interaction using below code I just want to update CustomValues dictionary object with custom key/value pair and adding interaction must be happen by sitecore by default.

using below code I'm able to add interaction into xDB against new/existing contact.

if (Tracker.Current != null && Tracker.Current.Contact != null)
            {
                Sitecore.Analytics.Tracker.Current.Session.IdentifyAs("testsource", "testuser1");
                
                using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                {
                    var trackerIdentifier = new IdentifiedContactReference(
                       Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource,
                       Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));

                    Contact contact = client.Get<Contact>(trackerIdentifier, new ContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey }));
                    if (contact != null)
                    {
                        bool IsConactFacetExist = false;
                        if (contact != null & contact.Facets != null && contact.Facets.Count > 0)
                        {
                            IsConactFacetExist = true;
                        }

                        if (!IsConactFacetExist)
                        {
                            var personalInfoFacet = new PersonalInformation()
                            {
                                FirstName = "FNTest",
                                LastName = "LNTest"
                            };

                            client.SetPersonal(contact, personalInfoFacet); 

                            //client.AddContact(contact);//commented to fix error "Concurrency token must not be specified for new entities"
                            //ref for fix: https://blogagilereactionio.azurewebsites.net/xconnect/ 
                        }

                        // Create a new interaction for the contact
                        Guid channelId = Guid.NewGuid(); // Use real channel ID from Sitecore
                        string userAgent = "Testing User Agent";
                        Interaction webInteraction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

                        // Create a new web visit facet model
                        var webVisitFacet = new WebVisit();

                        // Populate data about the web visit
                        webVisitFacet.Browser = new Sitecore.XConnect.Collection.Model.BrowserData() { BrowserMajorName = "Chrome", BrowserMinorName = "Desktop", BrowserVersion = "22.0" };
                        webVisitFacet.Language = "en";
                        webVisitFacet.OperatingSystem = new Sitecore.XConnect.Collection.Model.OperatingSystemData() { Name = "Windows", MajorVersion = "10", MinorVersion = "4" };
                        webVisitFacet.Referrer = "www.google.com";
                        webVisitFacet.Screen = new Sitecore.XConnect.Collection.Model.ScreenData() { ScreenHeight = 1080, ScreenWidth = 685 };
                        webVisitFacet.SearchKeywords = "sitecore";
                        webVisitFacet.SiteName = "website";


                        var itemId = Sitecore.Context.Item?.ID.Guid ?? Guid.NewGuid(); //Guid.NewGuid();
                        var itemVersion = Sitecore.Context.Item?.Version.ToInt32() ?? 5; //5;

                        //// First page view
                        PageViewEvent pageView = new PageViewEvent(new DateTime(2016, 10, 10, 13, 20, 22).ToUniversalTime(), itemId, itemVersion, "en");

                        pageView.ItemLanguage = "en";
                        pageView.Duration = new TimeSpan(3000);
                        pageView.CustomValues.Add("MemberId", "testmember123");
                        pageView.CustomValues.Add("PlanId", "testplan123");
                        pageView.Url = Sitecore.Context.RawUrl;
                        
                        pageView.CustomValues.Add("SessionId", Guid.NewGuid().ToString());
                        pageView.CustomValues.Add("User", Sitecore.Context.GetUserName());

                        webInteraction.Events.Add(pageView);

                        //Set web visit facet on interaction
                        client.SetWebVisit(webInteraction, webVisitFacet);

                        //Add interaction
                        client.AddInteraction(webInteraction);

                        client.Submit();
                    }
                }
            }
1

I believe you can access custom values of the current interactions using Sitecore.Analytics.Tracker.Current.Interaction.CustomValues. This way you should be able to change the custom values of the interaction submitted automatically by sitecore.

I am not sure however what you are trying to achieve, so if that's not it please describe the desired result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.