how to collect sitecore information for anonymous

695 views Asked by At

I need to get sitecore information that collected from anonymous users to give him availability to export it or opt out - [GDPR]

any idea about contact ID for anonymous !

2

There are 2 answers

0
Madawa Illesinghe On BEST ANSWER

The way of doing it is dependent on the sitecore version.

  • Sitcore 9 you can use right to be forgotten
  • Sitecore 8+ you have to implement the feature from scratch.

Regarding Anonymous user - If the user is really anonymous, then you done need to worry about the GDPR (my view). But sometimes we map user email and sensitive personal info to anonymous user by using forms or WFFM. You can use email address of that user to query xDB (Contact Identifiers) to get the contact and contactID. Then reset informations.

Also: please note that based on WFFFM save action config, anonymous user will store in Core DB and Contact List.

0
Chris Auer On

To forget a user, you can use the following code. It will execute the ExecuteRightToBeForgotten function on the contact and scrub their data.

Forget User

public bool ForgetUser()
{
    var id = _contactIdentificationRepository.GetContactId();
    if (id == null)
    {
        return false;
    }

    var contactReference = new IdentifiedContactReference(id.Source, id.Identifier);

    using (var client = _contactIdentificationRepository.CreateContext())
    {
        var contact = client.Get(contactReference, new ContactExpandOptions());
        if (contact != null)
        {
            client.ExecuteRightToBeForgotten(contact);
            client.Submit();
        }
    }

    return false;
}

Fake up some data

public void FakeUserInfo()
{
    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()));

        // pull the facet from the contact (if it exists)
        var facet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey);

        // if it exists, change it, else make a new one
        if (facet != null)
        {
            facet.FirstName = $"Myrtle-{DateTime.Now.Date.ToString(CultureInfo.InvariantCulture)}";
            facet.LastName = $"McSitecore-{DateTime.Now.Date.ToString(CultureInfo.InvariantCulture)}";

            // set the facet on the client connection
            client.SetFacet(contact, PersonalInformation.DefaultFacetKey, facet);
        }
        else
        {
            // make a new one
            var personalInfoFacet = new PersonalInformation()
            {
                FirstName = "Myrtle",
                LastName = "McSitecore"
            };

            // set the facet on the client connection
            client.SetFacet(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet);
        }

        if (contact != null)
        {
            // submit the changes to xConnect
            client.Submit();

            // reset the contact
            _contactIdentificationRepository.Manager.RemoveFromSession(Analytics.Tracker.Current.Contact.ContactId);
            Analytics.Tracker.Current.Session.Contact = _contactIdentificationRepository.Manager.LoadContact(Analytics.Tracker.Current.Contact.ContactId);
        }
    }
}

ContactIdentificationRepository

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