sitecore 9.1 получить anynoums контакт ID - PullRequest
0 голосов
/ 29 ноября 2018

как получить анонимный идентификатор контакта с помощью API sitecore?

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
    }
}

Я использую этот код, но не могу найти идентификатор контакта в xconnect DB

1 Ответ

0 голосов
/ 29 ноября 2018

Вы можете получить его с помощью кода 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);
}
...