如何在Sitecore 9.1中获取匿名联系人ID?

如何在Sitecore 9.1中获取匿名联系人ID?,sitecore,sitecore9,Sitecore,Sitecore9,如何使用Sitecore API获取匿名联系人ID 我正在使用此代码,但在xconnect DB中找不到联系人ID using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { // var enumerator = client.Interactions.Where(x =

如何使用Sitecore API获取匿名联系人ID

我正在使用此代码,但在xconnect DB中找不到联系人ID

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
    }
}
使用(XConnectClient=Sitecore.XConnect.client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
尝试
{
//var枚举器=client.Interactions.Where(x=>x.DeviceProfile.Id==contactId).GetBatchEnumeratorSync(10);
Event ev=new事件(Guid.NewGuid(),DateTime.UtcNow){Duration=new TimeSpan(20)};
var参考=新的ContactReference(contactId);
Contact Contact=client.Get(引用,new ContactExpandOptions(){});
如果(联系人!=null)
{
委托人、执行人、授权人(联系人);
client.Submit();
}
}
捕获(XdbExecutionException ex)
{
//管理异常
}
}

您可以使用代码
获取新的IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource、Tracker.Current.Contact.ContactId.ToString(“N”)
这是xDB标识符,是匿名联系人获取的标识符

这是我使用的代码,ActudioDistabyCalpSealStand在基础库中。contactIdentificationRepository.GetContactReference()将获取匿名或已识别的联系人引用

呼叫xConnect
你能解释一下这行代码吗?this.contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);我为ContactIdentificationRepository()类添加了构造函数。contactManager.SaveContactToCollectionDb强制将联系人推回xConnect。如果没有SaveContactToCollectionDb,我们就不会有浏览器和xConnect知道的contactId。当您访问站点时,xConnect还不知道您的情况。这通常是好的。但是,由于您想写入xConnect,我们必须首先用当前会话更新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()));

    .....
}
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);
}