0

Why Sitecore.Analytics.Tracker.Current.Contact.Identifiers.FirstOrDefault() is null in incognito mode of browser. I applied the following code but still same.

    public void LoadContact(Guid customIdentifier)
    {
        var manager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as Sitecore.Analytics.Tracking.ContactManager;

        if (manager != null)
        {
            using (var client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    if (Sitecore.Analytics.Tracker.Current.Contact.IsNew)
                    {
                        Sitecore.Analytics.Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                        manager.SaveContactToCollectionDb(AnalyticsHelpers.CurrentTracker.Contact);
                    }

                    var identifier = Sitecore.Analytics.Tracker.Current.Contact.Identifiers.FirstOrDefault();

                    if (identifier != null)
                    {
                        var reference = new IdentifiedContactReference(identifier.Source, customIdentifier.ToString("N"));

                        var existingContact = client.Get<Contact>(reference,
                            new Sitecore.XConnect.ContactExpandOptions(EmailAddressList.DefaultFacetKey));

                        var contact = client.Get(
                            new IdentifiedContactReference(identifier.Source, identifier.Identifier),
                            new ContactExpandOptions(EmailAddressList.DefaultFacetKey));

                        client.MergeContacts(existingContact, contact);

                        client.Submit();
                    }
                }
                catch (XdbExecutionException e)
                {
                    Sitecore.Diagnostics.Log.Error(e.Message, e, "LoadContact");
                }
            }
        }
}

I cannot IdentifyAs(...) here which will generate the id.

I reloaded the page few times, but nothing happens. I am using Sitecore 9.0.1. I checked the cookie, the analytics cookie is there. The contact is also not null. I am not making any async call. It is a normal page load.

2

Thanks to @peter-prochazka for showing me the way. Here is my working code:

var manager = AnalyticsHelpers.GetContactManager();

if (manager != null)
{
    using (var client = SitecoreXConnectClientConfiguration.GetClient())
    {

        var trackerIdentifier = new IdentifiedContactReference(
            Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource,
            Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));

        var contact = client.Get<Contact>(trackerIdentifier,
            new Sitecore.XConnect.ContactExpandOptions());

        if (contact == null)
        {
            manager.CreateContact(ID.NewID);
        }

        if (Sitecore.Analytics.Tracker.Current.Contact.IsNew)
        {
            Sitecore.Analytics.Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
            manager.SaveContactToCollectionDb(Sitecore.Analytics.Tracker.Current.Contact);
        }
    }
}

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.