Manage a contact's tracking consent choices

Abstract

How to use the consent manager to manage a contact's tracking consent choices.

You can use the IConsentManager service to give consent, revoke consent, and retrieve a contact's consent choice for the context website:

Important

Sitecore.Analytics.Tracking.Identification.IContactIdentificationManager is a transient service.

var consentManager = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetRequiredService<Sitecore.Analytics.Tracking.Consent.IConsentManager>();

Note

<name of a service> depends on the Sitecore.Context.Site and must be resolved in the context where it is available.

If a website requires explicit tracking, contacts are not tracked until they give consent - for example, by clicking a button in a cookie consent banner.

Note

To manage a contact's consent choice in any other context, use the xConnect Client API to update the ConsentInformation facet. For example, you can implement a user interface that allows a call center employee to update a contact's consent choices over the phone.

Tracking consent choices are stored in the Sitecore.XConnect.Collection.Model.ConsentInformation contact facet in xConnect. This facet provides the Consents dictionary, which is where the consent choices are stored. Consents that need to be tracked are stored by the following key: xDB.Tracker.Consent.WebSiteName. For example, a contact's tracking choice for a web site called mywebsite is stored by the xDB.Tracker.Consent.mywebsite key. The consent key prefix, xDB.Tracker.Consent., is also accessible as the Sitecore.Analytics.XConnect.DataAccess.Constants.ConsentKeyPrefix constant.

To get the context contact's consent choice for the current site:

using Microsoft.Extensions.DependencyInjection;
using Sitecore.Analytics.Tracking.Consent;
using Sitecore.Analytics.Tracking.Identification;
using Sitecore.DependencyInjection;
using System.Linq;

namespace Documentation.Examples
{
    public class ConsentExamples
    {
        public void GetConsentChoice()
        {
            var consentManager = ServiceLocator.ServiceProvider.GetRequiredService<IConsentManager>();

            // If the known identifier is null, the consent choice is retrieved from a cookie.
            // Supply the relevant Source and KnownIdentifier as parameters below.            
            TrackingConsent consentChoice = consentManager.GetConsent(new KnownContactIdentifier(Source, KnownIdentifier);

            var consentGivenForSite = consentChoice.IsGiven;
        }
    }

In this scenario:

  • If the identifier is not provided (that is, null is passed to the method), the tracker attempts to get the contact's consent choice from the IConsentStorage service. This is stored in the SC_TRACKING_CONSENT cookie by default.

  • If the identifier is provided, the tracker attempts to get the consent choice from the contact's facet, loaded from xConnect.

To give consent for the context site:

using Microsoft.Extensions.DependencyInjection;
using Sitecore.Analytics.Tracking.Consent;
using Sitecore.Analytics.Tracking.Identification;
using Sitecore.DependencyInjection;
using System.Linq;

namespace Documentation.Examples
{
    public class ConsentExamples
    {
        public void GetConsentChoiceAndIdentify()
        {
            var consentManager = ServiceLocator.ServiceProvider.GetRequiredService<IConsentManager>();
                        
             // Supply the relevant Source and KnownIdentifier as parameters below. 
            consentManager.GiveConsent(new KnownContactIdentifier(Source, KnownIdentifier);
        }
    }
}

In this scenario:

  • The tracker writes the contact's consent choice to the consent storage using the IConsentStorage service. By default, this choice is stored in the SC_TRACKING_CONSENT cookie.

  • Tracking is started.

  • If the identifier parameter is provided (it is not null), the tracker identifies the contact and the updated consent choice is set for the current contact by the ContactManager.SetTrackingConsent() API.

To revoke consent:

using Microsoft.Extensions.DependencyInjection;
using Sitecore.Analytics.Tracking.Consent;
using Sitecore.Analytics.Tracking.Identification;
using Sitecore.DependencyInjection;
using System.Linq;

namespace Documentation.Examples
{
    public class ConsentExamples
    {
        public void RevokeConsent()
        {
            var consentManager = ServiceLocator.ServiceProvider.GetRequiredService<IConsentManager>();

            // Supply the relevant Source and KnownIdentifier as parameters below.
            consentManager.RevokeConsent(new KnownContactIdentifier(Source, KnownIdentifier);
        }
    }

In this scenario:

  • If tracking is in progress, it stops.

  • The tracker invalidates the SC_ANALYTICS_GLOBAL_COOKIE and and updates the contact's consent choice to the consent storage using the IConsentStorage service. By default, this choice is stored in the SC_TRACKING_CONSENT cookie.

  • If the identifier parameter is provided (it is not null) and tracking has started, the tracker ensures that the identifier matches the current contact and sets the consent choice to the contact using the ContactManager.SetTrackingConsent() API.

  • If the identifier parameter is provided (it is not null) but tracking has not started, the tracker ensures that the contact is loaded from xDB and sets the consent choice for the contact using the ContactManager.SetTrackingConsent() API.

  • If the identifier parameter is not provided (it is null), the tracker uses information in the SC_ANALYTICS_GLOBAL_COOKIE to determine which contact is associated with the current device. It then ensures that the contact is loaded from xDB and sets the consent choice for the contact using the ContactManager.SetTrackingConsent() API.