5

I'm trying to disable/remove the SC_ANALYTICS_GLOBAL_COOKIE based on an opt in cookie.

I have updated the Sitecore.Analytics.Tracking.config to add a custom extension to disable analytics and delete cookie. But that does not seems to be working even though the code is hitting my cookie check and removing the cookie

    <processor type="Sitecore.Analytics.Pipelines.StartAnalytics.CheckPreconditions, Sitecore.Analytics" />
    <processor type="SC.Web.SitecoreExtensions.Pipelines.Analytics.CheckCookie, SC.Web.SitecoreExtensions" patch:after="processor[@type='Sitecore.Analytics.Pipelines.StartAnalytics.CheckPreconditions, Sitecore.Analytics']" />

Cookie Checking Class:

public class CheckCookie
{
    public void Process(PipelineArgs args)
    {
        if (!CheckCookieAccepted(HttpContext.Current.Request))
        {
            Sitecore.Analytics.Tracker.Enabled = false;
            args.AbortPipeline();
            DeleteAnalyticsCookie();
        }
    }

    private void DeleteAnalyticsCookie()
    {

        HttpCookie analyticsCookie = HttpContext.Current.Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"];
        if (analyticsCookie != null && !string.IsNullOrEmpty(analyticsCookie.Value))
        {
            HttpContext.Current.Response.Cookies.Remove("SC_ANALYTICS_GLOBAL_COOKIE");
            analyticsCookie.Expires = DateTime.Now.AddDays(-10);
            analyticsCookie.Value = null;
            HttpContext.Current.Response.SetCookie(analyticsCookie);
        }
    }
}

Is there any place else I should be doing this check? Which exact Analytics pipeline step is creating this cookie?

2 Answers 2

8

You need to patch your custom processor before CreateTracker processor of the StartAnalytics Pipeline.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <startAnalytics>
        <processor
          type="YOURPROCESSOR" patch:before="processor[@type='Sitecore.Analytics.Pipelines.StartAnalytics.CreateTracker, Sitecore.Analytics']" resolve="true"/>
      </startAnalytics>
    </pipelines>
  </sitecore>
</configuration>

Then,

if (SC_ANALYTICS_GLOBAL_COOKIE not allowed)
{
    if (HttpContext.Current.Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"] != null)
    {
        var myCookie = new HttpCookie("SC_ANALYTICS_GLOBAL_COOKIE");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        HttpContext.Current.Response.Cookies.Add(myCookie);
    }
    args.AbortPipeline();
}

EXPLANATION : "USE CASE : The tracker should not be started, and the analytics cookie should not be set (and removed if it was already set). This can be done easily because for every request without a tracker, the startAnalytics pipeline is started. This pipeline, as the name suggest, starts a whole bunch of things for Sitecore Analytics. If you would analyze the pipeline you would see that there is a processor called CreateTracker. The processor creates the tracker and when creating a tracker the cookie is set, therefor this is where the pipeline should be aborted when the user did not give his or her consent. This can be easily done by inserting a new processor right before the CreateTracker processor"

Reference & Credits : https://dev.to/alexdhaenens/how-make-a-sitecore-solution-gdpr-compliant-29ga

1
2

I also had to patch EnsureDevice processor on the ensureSessionContext pipeline as well

    <processor type="Sitecore.Analytics.Pipelines.EnsureSessionContext.EnsureDevice, Sitecore.Analytics"/>
    <processor type="SC.CheckCookie, SC.Web.SitecoreExtensions" patch:after="processor[@type='Sitecore.Analytics.Pipelines.EnsureSessionContext.EnsureDevice, Sitecore.Analytics']" />
1
  • Thanks, I had to do the same just now (Sitecore 9.3).
    – WulfgarDK
    Jul 28, 2020 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.