sitecoresitecore9

How to get anonymous contact ID in Sitecore 9.1?


How to get anonymous contact ID using Sitecore API?

I'm using this code but can't find the contact ID in xconnect DB.

using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
    try
    {
        // var enumerator = client.Interactions.Where(x => x.DeviceProfile.Id == contactId).GetBatchEnumeratorSync(10);
        Event ev = new Event(Guid.NewGuid(), DateTime.UtcNow) { Duration = new TimeSpan(20) };
        var reference = new ContactReference(contactId);
        Contact contact = client.Get<Contact>(reference, new ContactExpandOptions() { });
        if (contact != null)
        {
            client.ExecuteRightToBeForgotten(contact);
            client.Submit();
        }
    }
    catch (XdbExecutionException ex)
    {
        // Manage exceptions
    }
}

Solution

  • You can get it with the code new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Tracker.Current.Contact.ContactId.ToString("N")) This is the xDB identifier which is the identifier anonymous contacts get.

    Here is the code I use _contactIdentificationRepository is in a Foundation repository. _contactIdentificationRepository.GetContactReference() will get the anonymous or identified contact reference.

    Call xConnect

    var contactReference = _contactIdentificationRepository.GetContactReference();
    
    using (var client = SitecoreXConnectClientConfiguration.GetClient())
    {
        // we can have 1 to many facets
        // PersonalInformation.DefaultFacetKey
        // EmailAddressList.DefaultFacetKey
        // Avatar.DefaultFacetKey
        // PhoneNumberList.DefaultFacetKey
        // AddressList.DefaultFacetKey
        // plus custom ones
        var facets = new List<string> { PersonalInformation.DefaultFacetKey };
    
        // get the contact
        var contact = client.Get(contactReference, new ContactExpandOptions(facets.ToArray()));
    
        .....
    }
    

    _contactIdentificationRepository

    private readonly ContactManager contactManager;
    
    public ContactManager Manager => contactManager;
    
    public ContactIdentificationRepository()
    {
        contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager;
    }
    
    public IdentifiedContactReference GetContactReference()
    {
        // get the contact id from the current contact
        var id = GetContactId();
    
        // if the contact is new or has no identifiers
        var anon = Tracker.Current.Contact.IsNew || Tracker.Current.Contact.Identifiers.Count == 0;
    
        // if the user is anon, get the xD.Tracker identifier, else get the one we found
        return anon
            ? new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Tracker.Current.Contact.ContactId.ToString("N"))
            : new IdentifiedContactReference(id.Source, id.Identifier);
    }
    
    public Analytics.Model.Entities.ContactIdentifier GetContactId()
    {
        if (Tracker.Current?.Contact == null)
        {
            return null;
        }
        if (Tracker.Current.Contact.IsNew)
        {
            // write the contact to xConnect so we can work with it
            this.SaveContact();
        }
    
        return Tracker.Current.Contact.Identifiers.FirstOrDefault();
    }
    
    public void SaveContact()
    {
        // we need the contract to be saved to xConnect. It is only in session now
        Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
        this.contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
    }