Can we override SC_ANALYTICS_GLOBAL_COOKIE
through code? Meaning, in my configuration file I set to true
, through code I want to set it to false
for a particular user. Is it possible?
1 Answer
I wouldn't recommend overwriting cookies that are set by the platform generally. I usually recommend make your own which you have full control over.
Cookie editing is limited by domains. You couldn't edit it cross domain. Quote from mozilla when talking about cookies:
Additionally, restrictions to a specific domain and path can be set, limiting where the cookie is sent.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
For this cookie in particular I recommend using sitecore out the box approach for this first
Run the following in javascript:
Tracker.Current.Session.SetClassification(925, 925, true);
which will disable the cookie.
Tracker.Current.Session.SetClassification(0, 0, true);
this enables the cookie.
If this isn't what you want, you can manipulate the cookie via c# by requesting the cookie first. I stress sitecore SetClassification
is safer option as it may do more then just set the cookie.
HttpCookie cookie = Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"];
Check if it's null then update your values:
if (cookie == null)
{
// no cookie found, create it
cookie = new HttpCookie("SC_ANALYTICS_GLOBAL_COOKIE");
cookie.Values["yourValueName"] = "1";
cookie.Values["yourValueName2"] = "0";
}else{
// update existing cookie values
cookie.Values["relevanValueToUpdate"] = true;
}
you can even change the expiry.
// update the expiration timestamp
cookie.Expires = DateTime.UtcNow.AddDays(30);
finally.
// overwrite the cookie
Response.Cookies.Add(cookie);
code sample from: https://stackoverflow.com/questions/22070840/change-a-cookie-value-of-a-cookie-that-already-exists
-
Thank you very much for the response. The scenario is that if user dosent agree the cookies, then his interactions should not be tracked. Is there any better way? By default the setting is set to "true'. Any thoughts on this/– tpkphaniSep 20, 2019 at 14:00
-
doc.sitecore.com/developers/90/… there is a pipeline you can disable web tracking from, create a custom process for the pipeline that'll check if they've consented or not, before disabling?– AnichoSep 20, 2019 at 14:17
-
1To prevent tracking before cookie consent it give, we typically disable tracking on the user's session until he agrees and then enable it. Because really you would even not want to set this cookie before it at all.– JoostSep 20, 2019 at 14:21