如何收集匿名的网站核心信息

时间:2018-11-29 13:54:52

标签: sitecore sitecore-analytics sitecore9 sitecore-sxa

我需要获取从匿名用户那里收集的站点核心信息,以便他可以导出或选择退出-[GDPR]

关于匿名联系人ID的任何想法!

2 个答案:

答案 0 :(得分:2)

要忘记用户,可以使用以下代码。它将在联系人上执行ExecuteRightToBeForgotten函数并清理其数据。

忘记用户

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

补充一些数据

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

答案 1 :(得分:2)

执行此操作的方式取决于sitecore版本。

  • 您可以使用right to be forgotten
  • 的Sitcore 9
  • Sitecore 8+,您必须从头开始实现该功能。

关于匿名用户-如果用户确实是匿名用户,那么您确实需要担心GDPR(我的看法)。但是有时我们会使用表格或WFFM将用户电子邮件和敏感的个人信息映射到匿名用户。您可以使用该用户的电子邮件地址来查询xDB(Contact Identifiers)以获得联系人和contactID。然后重置信息。

另外:请注意,基于WFFFM保存操作配置,匿名用户将存储在核心数据库和联系人列表中。