2

This code is only droping the cookies from the cookie board, the analytics is still on for the user. Is there any way to disable/enable xdb.enabled to false/true based on country region.

if (countryCode != "IN" && countryCode != "US")
            {
                if (HasCookieConsent())
                {
                    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();
            }
        }
1
  • Did you try using the Sitecore.Analytics.Tracker.Current?.EndTracking(); before aborting the pipeline as given in this article? blog.baslijten.com/… Jan 17, 2023 at 16:06

4 Answers 4

0

Xdb.Enabled is a Sitecore setting and it can't enable/disable on the fly with code changes. It can be patched using Sitecore patch file to enable/disable it and update the value to true/false.

patch file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
   <sitecore>
     <settings>
       <setting name="Xdb.Enabled" set:value="false"/>
     </settings>
   </sitecore>
 </configuration>
0

By adding the mentioned code in the question :-

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

SC_ANALYTICS_GLOBAL_COOKIE will no longer use, args.AbortPipeline() disable SC_ANALYTICS_GLOBAL_COOKIE automatically.

0

What you are already doing is good enough for the cookie to be deleted.

I think you can try disabling the analytics tracker with the below line, before deleting the cookie:

Sitecore.Analytics.Tracker.Enabled = false;

This should stop Sitecore from tracking the user.

I hope this helps. :)

0

I would probably try to add new module to web.config to remove SC_ANALYTICS_GLOBAL_COOKIE if the IP address is located in certain countries. You could you services like https://www.iplocation.net/ and its API to ask which country this IP address is located. Then you compare that country name from Sitecore or file that contains GDPR country list.

In order to handle this scenario this way is to create a new IHttpModule like this:

public class CheckCountryHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += PreSendRequestHeaders;
    }

    private void PreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        if(Sitecore.Context.Database == null || Sitecore.Context.Database.Name == "core" || app.Context.Request.RawUrl.StartsWith("/sitecore")
        {
            return;
        }
        // Check location based on ip address
       if(IsLocatedInGDPRCountry(app))
       {
          // Remove Sitecore AnalyticsCookie from response
       }
    }

}

Lastly, you need to add this to web.config section

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add type.....
        <add type="namespace.CheckCountryHttpModule, AssemblyFile" name"CheckCountryHttpModule" />
</modules>
<system.webServer>

Although you have to know that if user is behind a VPN, the country can be different than where the real user is.

2
  • Can you please, show how you have removed SC_ANALYTICS_GLOBAL_COOKIE ? Jan 17, 2023 at 8:44
  • to remove cookie you can just do app.Response.Cookies.Remove("SC_ANALYTISC_GLOBAL_COOKIE") so it wont go back to user browser Jan 18, 2023 at 7:09

Your Answer

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

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