7

I am trying to update Profile data through C#. But I don't see the values getting updated in the profile of the user, below is my code for updating values.

if (!Tracker.IsActive || Tracker.Current == null)
{
  Tracker.StartTracking();
}

if (Tracker.Current == null)
{
  return;
}
var scores = new Dictionary<string, double>();
var profile = Tracker.Current.Interaction.Profiles["Persona"];
scores.Add("Empowered", 6);
scores.Add("Guidance", 3);
profile.Score(scores);
profile.UpdatePattern();

When I am going to the experience profile, I cannot see those values getting reflected: enter image description here

The values remains 11 and 9, no matter how many times I run the above code.

I added the following code to submit the scores to xConnect as below:

private void submitInteraction()
{
    var anyIdentifier = Sitecore.Analytics.Tracker.Current.Contact.Identifiers.FirstOrDefault();
    using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
    {
        var contact = client.Get<Sitecore.XConnect.Contact>(new IdentifiedContactReference(anyIdentifier.Source, anyIdentifier.Identifier), new Sitecore.XConnect.ContactExpandOptions());
        if (contact != null)
        {
            var interaction = new Sitecore.XConnect.Interaction(contact, InteractionInitiator.Brand, Tracker.Current.Interaction.ChannelId, Tracker.Current.Interaction.UserAgent);
            var profilescore = new ProfileScores();
            profilescore.Scores.Add(new Guid("{B5BDEE45-C945-476F-9EE6-3B8A9255C17E}"), new ProfileScore() { MatchedPatternId = new Guid("{C5EA4A36-B2BF-4505-95EE-4C07CA72EA09}"), ProfileDefinitionId = new Guid("{B5BDEE45-C945-476F-9EE6-3B8A9255C17E}"), Score = 10, Values = new Dictionary<Guid, double>() { { new Guid("{D539DA39-C19F-4E0A-B144-1A5DD3364E68}"), 7 }, { new Guid("{2DEA2E60-8D89-4FB8-BB4C-CAD1369D4BB5}"), 3 } } });
            client.SetProfileScores(interaction, profilescore);
            //interaction.ProfileScores().Scores.Add(Guid.NewGuid(), new ProfileScore() { MatchedPatternId = new Guid("{C5EA4A36-B2BF-4505-95EE-4C07CA72EA09}"), ProfileDefinitionId = new Guid("{B5BDEE45-C945-476F-9EE6-3B8A9255C17E}"), Score = 10, Values = new Dictionary<Guid, double>() { { new Guid("{D539DA39-C19F-4E0A-B144-1A5DD3364E68}"), 7 }, { new Guid("{2DEA2E60-8D89-4FB8-BB4C-CAD1369D4BB5}"), 3 } } });
            //client.AddInteraction(interaction);
            client.Submit();
         }
    }
}

The above code is throwing following exception:

Exception: Sitecore.XConnect.Operations.FacetOperationException
Message: Operation #0, ReferenceNotFound, Interaction, ProfileScores
Source: Sitecore.Xdb.Common.Web
   at Sitecore.Xdb.Common.Web.Synchronous.SynchronousExtensions.SuspendContextLock[TResult](Func`1 taskFactory)
   at Sitecore.XConnect.Client.XConnectSynchronousExtensions.SuspendContextLock(Func`1 taskFactory)
   at Sitecore.Foundation.XConnect.Repositories.XConnectRepository.submitInteraction() in C:\projects\BUSSQ\src\foundation\XDB\code\Repositories\XConnectRepository.cs:line 362
   at Sitecore.Foundation.XConnect.Repositories.XConnectRepository.SetProfileCard(String personas) in C:\projects\BUSSQ\src\foundation\XDB\code\Repositories\XConnectRepository.cs:line 345
   at BUSSQ.Website.Repositories.SliderComponentRepository.SetProfileCard(String personas) in C:\projects\BUSSQ\src\Project\BUSSQ\code\Repositories\SliderComponentRepository.cs:line 39
   at BUSSQ.Website.Controllers.SliderController.setProfile(SliderFormat sliderValue) in C:\projects\BUSSQ\src\Project\BUSSQ\code\Controllers\SliderController.cs:line 64
   at System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
6
  • 2
    Have you tried end the session and see if it gets updated? Also, try to change double to float for the dictionary? Commented Feb 21, 2018 at 6:45
  • what is the code for ending the session?
    – Arun Kumar
    Commented Feb 21, 2018 at 6:53
  • Also profile.Score(scores); accepts the argument as Dictionary<string,double> so it won't accept the dictionary<string,float>
    – Arun Kumar
    Commented Feb 21, 2018 at 6:54
  • 1
    To end the session you will need to use the following: HttpContext.Current.Session.Abandon();. Note that you should not use the mention code on production. Only use this for testing purposes Commented Feb 21, 2018 at 9:59
  • I tried adding the session abandon but no affect. Even when I am doing the debugging I see the actual values in the profile scores but those are not reflected on the dashboard. Do I have to submit the profile data to xConnect with Sitecore 9?
    – Arun Kumar
    Commented Feb 22, 2018 at 0:05

3 Answers 3

3
+50

As mentioned above, you will need to use xConnect. For the channelId, you can use an existing one such as Direct or create your own (like I did) under Marketing Control Panel > Taxonomies > Channel. Make sure to deploy if you create your own.

This code assumes you know how to use the XConnectClient and get an xConnect Contact. There are plenty of sample code in Sitecore Docs.

The LocationId points to an existing location. Just use an existing one by querying the xdb.collection.InteractionFacets table in one of the Shard DBs. The IpInfo FacetKey will have this property.

The SitecoreDeviceData is the Default device's ID.

REMOVED

While the DeviceProfile is an existing entry in the xdb.collection.DeviceProfiles. Just use any of them for now.

Obviously, the most important part of the code is the one that updates the profile scores.

UPDATE The MatchedPatternIdin the ProfileScore should be an existing pattern card. By default, there's no pattern card. Create one under the profile that has the keys you want to update, deploy, and use the item ID in the code.

To check if it worked:

  1. Open SQL and query the xdb.collection.InteractionFacets table in one of the Shard DBs. You may need to check both to find the right one.

  2. Sort the entries in descending order to see the latest interaction quickly. You should see a ProfileScores facet key.

  3. Copy the FacetData column's value and paste it on a text editor. You should see the values you specified for each of the profile keys.

Notice that the values were not "added" to the existing values. That's another set of code.

    protected void UpdateProfileScoresDemo(XConnectClient client, XConnect.Contact xcontact)
    {
        // to update the scores, use interactions
        Guid channelId = new Guid("{097F90E5-D120-4B4E-9181-AB08F45A8E6D}"); // Demo: Test Channel
        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(xcontact, InteractionInitiator.Brand, channelId, userAgent);

        // Create a new web visit facet model
        var webVisitFacet = new WebVisit
        {

            // Populate data about the web visit
            Browser = new BrowserData() { BrowserMajorName = "Chrome", BrowserMinorName = "62.0", BrowserVersion = "62.0" },
            Language = "en",
            OperatingSystem = new OperatingSystemData() { Name = "WinNT", MajorVersion = "", MinorVersion = "" },
            Referrer = "www.google.com",
            Screen = new ScreenData() { ScreenHeight = 1080, ScreenWidth = 685 },
            SearchKeywords = "sitecore habitat",
            SiteName = "habitat"
        };

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

        var webIpInfo = new IpInfo("127.0.0.1")
        {
            AreaCode = "N/A",
            BusinessName = "N/A",
            City = "N/A",
            Country = "N/A",
            Isp = "N/A",
            LocationId = new Guid("{9955c4d5-7a61-58c7-907c-f5737133685a}"),
            MetroCode = "N/A",
            PostalCode = "N/A",
            Region = "N/A",
            Url = "N/A"
        };

        client.SetIpInfo(interaction, webIpInfo);


        PageViewEvent pageView = new PageViewEvent(DateTime.Now.ToUniversalTime(), Sitecore.Context.Item.ID.ToGuid(), Sitecore.Context.Item.Version.Number, "en");

        pageView.ItemLanguage = "en";
        pageView.Duration = new TimeSpan(3000);
        pageView.SitecoreRenderingDevice = new SitecoreDeviceData(new Guid("{fe5d7fdf-89c0-4d99-9aa3-b5fbd009c9f3}"), "Default");
        pageView.Url = LinkManager.GetItemUrl(Sitecore.Context.Item);

        // Looks like we don't need this
        // interaction.DeviceProfile = new DeviceProfile(new Guid("{DF10B45C-B1EC-4AD0-A26B-61CDCDB50B33}"));

        interaction.Events.Add(pageView);

        ProfileScores ps = new ProfileScores();

        var focusScore = new ProfileScore
        {
            MatchedPatternId = new Guid("{67e258ca-9555-495b-a2c1-aaa6a4c42468}"),
            ProfileDefinitionId = new Guid("{24dff2cf-b30a-4b75-8967-2fe3ded82271}"),
            ScoreCount = 1,
            Score = 14,
            Values = new Dictionary<Guid, double>
        {
            { new Guid("{03379af5-f1ae-4610-b15b-4c7f1032b464}"), 2 },      // bg
            { new Guid("{5FDD9829-E689-454D-9ABC-8F95AE68744C}"), 3 },      // practical
            { new Guid("{F5652C06-676B-4E12-A9D0-06D000E5F1C8}"), 4 },      // process
            { new Guid("{B32BFACC-3494-4127-B050-CF50078E2B4C}"), 5 }       // scope
        }
        };


        ps.Scores.Add(new Guid("{24dff2cf-b30a-4b75-8967-2fe3ded82271}"), focusScore);


        client.SetFacet<ProfileScores>(interaction, ProfileScores.DefaultFacetKey, ps);

        client.AddInteraction(interaction);
        client.Submit();
 }
7
  • I have tried the above code but I am getting {"Operation #0, ReferenceNotFound, Interaction, IpInfo"} {"Operation #1, ReferenceNotFound, Interaction, ProfileScores"} {"Operation #2, ReferenceNotFound, Interaction"} on client.submit()
    – Arun Kumar
    Commented Feb 27, 2018 at 1:42
  • Make sure that the GUIDs in the ProfileScores exist in your instance. The possible issue is the MatchedPatternId. I've updated my answer to include this.
    – Marco
    Commented Feb 27, 2018 at 16:16
  • I have updated the patternID Guid to match the profile card item. Still no luck? I am getting the same issues Message "Operation #0, ReferenceNotFound, Interaction, WebVisit" string + [1] {"Operation #1, ReferenceNotFound, Interaction, IpInfo"} System.Exception {Sitecore.XConnect.Operations.FacetOperationException} + [2] {"Operation #2, ReferenceNotFound, Interaction, ProfileScores"} System.Exception {Sitecore.XConnect.Operations.FacetOperationException} + [3] {"Operation #3, ReferenceNotFound, Interaction"} System.Exception {Sitecore.XConnect.Operations.EntityOperationException}
    – Arun Kumar
    Commented Feb 28, 2018 at 0:33
  • Did you update the channelId? The one I have in the code, as mentioned in the answer, is a custom channel. You can use an existing one.
    – Marco
    Commented Feb 28, 2018 at 1:29
  • Yes I did updated the channel ID in the code to the one found in my database. Still same error.
    – Arun Kumar
    Commented Feb 28, 2018 at 4:14
1

Tracker/Mongo DB is no more available with Sitecore 9.0.

You should use xConnect server/API.

Using the xConnect and Sitecore 9.0 Facet you can get the require information.

For using xConnect you can see few example here:

https://jitendrasitecore.wordpress.com/2018/02/02/xconnect-saving-user-profile-information-using-xconnect/

https://jitendrasitecore.wordpress.com/2018/02/12/xconnect-create-custom-facet/

2
  • I have updated my code to send the score to xConnect API. But it seems to be throwing the error. I have added code and error in the actual question.
    – Arun Kumar
    Commented Feb 26, 2018 at 5:39
  • Have you tried adding interaction while setting profile scores? client.AddInteraction(interaction); The error message means that the interaction which profile scores are referencing is not found.
    – grg
    Commented Feb 26, 2018 at 8:35
0

Apparently there is an issue with Sitecore 9 where profile scores are not updated. Sitecore support today provided me with a package to be deployed and I can see my profile scores getting updated. You don't need to submit interactions for profile scores to be updated. I have upload the sitecore support package to dropbox. Sitecore support package 209472

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.