2

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

2 Answers 2

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.

0

Overview

It sounds like what you're trying to do is set the CustomValues and have the key/values stored in xDB. There is a way to do that which I will show, but you will want to be familiar with Sitecore's recommendations regarding the use of CustomValues.

Understanding CustomValues

To drive the point home, here are my favorite notes about CustomValues:

  • Store any string/object combination.
  • This property is never saved to xConnect and should only be used to store data that will be copied to a custom xConnect event model in the event conversion pipeline.
    • That is... Unless you manually override ConvertPageDataToPageViewEvent -- more on that in a following section.
  • CustomValues is never saved to xConnect and can only be accessed during session or in the context of the event conversion pipeline.
  • The CustomValues property is never saved to xConnect. For each custom event model, you must create a conversion pipeline that copies data from CustomValues to that model.
  • The CustomValues property lacks contextual meaning and makes it difficult to analyze data in a third party tool. It is recommended that you create custom types for all events that include custom data and do not rely on the xConnect event CustomValues.
  • Converting session events into xConnect events: you must create a conversion pipeline processor to convert a session event into a custom xConnect event. Each custom event will have its own processor. In each conversion pipeline processor, you will:
    • Create an instance of your xConnect event - such as NewsletterGoal.
    • Copy any custom data from the session event to the xConnect event - for example, you might copy the value of pageData.CustomValues["NewsletterImageCategory"] to a property on the NewsletterGoal class named NewsletterImageCategory.
    • Exact instructions depend on whether you are converting to an xConnect event model, a xConnect goal model, or an xConnect outcome model.
  • The Sitecore.XConnect.Event class has a CustomValues property of type Dictionary<string,string> for legacy reasons. If you are only storing string values in your PageEventData or OutcomeData custom values, you can technically create a conversion processor that copies custom values from the tracker event to the xConnect event without changing the event type. The tracker does not automatically perform this conversion as it is not possible to guarantee that each dictionary object will be a string.
  • Do not rely on the Data, DataKey, Text, and CustomValues properties on the Event class to store custom data. These properties are only available for legacy purposes.

Recommended Approach

This guide by Sitecore provides a perfect example of how to do it.

The TLDR is:

  1. Create a custom event model
  2. Create an event conversion processor
  3. Register event conversion processor
  4. Trigger the event (and set your CustomValues as you have done already)

Changing the Default Behavior of PageViewEvent Storage

This section is for educational purposes only.

As mentioned above, you can customize Sitecore such that CustomValues are stored in xdb by default (kind of), but it's not recommended. I outlined how to do this in one of my related questions.

Override the default behavior in Sitecore.Analytics.XConnect.DataAccess.Pipelines.ConvertToXConnectEventPipeline.ConvertPageDataToPageViewEvent:

private PageViewEvent CreatePageViewEvent(PageData pageData)
{
    ...

    // Note that there is no access to Sitecore context here 

    // Create a new entry in CustomValues (NOT RECOMMENDED)  
    if(pageData.CustomValues["CustomKey1"] != null && !string.IsNullOrWhiteSpace(pageData.CustomValues["CustomKey1"]){
      pageViewEvent.CustomValues.Add("CustomKey1", pageData.CustomValues["CustomKey1"]);
    }
    ...
}

As well as the associated config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <convertToXConnectEvent>
        <processor type="Sitecore.Analytics.XConnect.DataAccess.Pipelines.ConvertToXConnectEventPipeline.ConvertPageDataToPageViewEvent, Sitecore.Analytics.XConnect">
          <patch:attribute name="type" value="Client.Foundation.DataExchange.Pipelines.ConvertToXConnectEventPipeline.ConvertPageDataToPageViewEvent, Client.Foundation.DataExchange" />
        </processor>
      </convertToXConnectEvent>
    </pipelines>
  </sitecore>
</configuration>

Once the session / interaction is complete, the above code will run and you will see the values in xdb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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