Created
November 23, 2019 18:30
-
-
Save dnstommy/d4e92c46b4878f70cb1f570439bd68c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Sitecore.Analytics; | |
using Sitecore.Analytics.Model; | |
using Sitecore.Analytics.Tracking; | |
using Sitecore.Configuration; | |
using Sitecore.XConnect; | |
using Sitecore.XConnect.Client.Configuration; | |
namespace Sitecore.Foundation.Accounts.Repositories | |
{ | |
public class 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); | |
} | |
public IXdbContext CreateContext() | |
{ | |
return SitecoreXConnectClientConfiguration.GetClient(); | |
} | |
public XConnect.Contact CreateContact(string email) | |
{ | |
var reference = new ContactIdentifier(Constants.Strings.UserLoginIdentifier, email.ToLower(), ContactIdentifierType.Known); | |
var contact = new XConnect.Contact(reference); | |
return contact; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment