sitecore - 如何在 Sitecore 9.1 中获取匿名联系人 ID?
问题描述
如何使用 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
}
}
解决方案
您可以使用代码获取它new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Tracker.Current.Contact.ContactId.ToString("N"))
这是 xDB 标识符,它是匿名联系人获取的标识符。
这是我使用的代码 _contactIdentificationRepository 位于 Foundation 存储库中。_contactIdentificationRepository.GetContactReference() 将获取匿名或已识别的联系人参考。
致电 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);
}
推荐阅读
- excel - 激活最后一个可见的活动工作表
- docker - 无法在 GNS3 中为 docker 容器打开 UDP 端口以进行容器间端口访问
- android - 如何在调试模式下构建 Flutter aar
- .net - 与 http://doubleclick.net/ 上的资源关联的 cookie 设置为 `SameSite=None` 但没有 `Secure`。Chrome 的未来版本
- javascript - Sapper中的Axios请求两次?
- mysql - 为多个复合索引设计表架构和/或索引的最佳实践
- javascript - 如何在 index.js 文件中没有触发函数的情况下从 firestore 数据库中读取数据?
- payment-gateway - 验证万事达卡支付网关中已取消交易的订单详细信息
- amazon-web-services - 如何更改 Alexa 的失败消息?
- wpf - Nest reactiveui usercontrols wpf / 将 ViewModel 传递给 usercontrol