EPiServer – Disable Google Tag Manager by Reading Cookie

Even though this is not completely related to EPiServer, sometimes we need to give our clients the option to disable tracking them when for instance a user does not approve a GDPR modal.
In this post, we are going to disable google tag manager which is set in a field in the home page and a method which will read a cookie to check if the user authorized to be tracked.

So, lets begin !!!!
First, we will create the base class for pages and the settings page which will also act as the start/home page.
namespace Data.Models.Page { [ContentType(GUID = "98448299-51bd-45a9-8d68-7dcdfee0a031", AvailableInEditMode = false)] public class SitePage : PageData { } }
namespace Data.Models.Page { [ContentType(GUID = "98448299-51bd-45a9-8d68-7dcdfee0a030")] public class StartPage : SitePage { [Display(GroupName = "Metadata", Order = 1000)] public virtual GoogleTagManagerBlock GoogleTagManager { get; set; } } }
Then, we will create a block to store the information that google tag manager requires.
namespace Data.Models.Block.Metadata { [ContentType(GUID = "f5ae4c27-3b62-4392-9754-9a82a6fdd6d4", AvailableInEditMode = false)] public class GoogleTagManagerBlock : BlockData { [CultureSpecific] [Display(GroupName ="Data Layer", Order = 100)] [UIHint(UIHint.Textarea)] public virtual string DataLayer { get; set; } [CultureSpecific] [Display(GroupName = "Tag Manager Script", Order = 200)] [UIHint(UIHint.Textarea)] public virtual string TagManagerSnippet { get; set; } } }
Now, we will create some methods to create and read cookies as part of the session service static class.
namespace Business.Helpers { public static class SessionService { public static string ReadCookie(string name) { var request = HttpContext.Current.Request; if (request?.Cookies == null) { return null; } var cookie = request.Cookies[name]; if (cookie == null) return null; return cookie.Value; } public static void CreateCookie(string name, string value, int days = 30) { var response = HttpContext.Current.Response; if (response.IsRequestBeingRedirected) return; var cookie = new HttpCookie(name, value) { Expires = DateTime.Now.AddDays(days) }; response.Cookies.Add(cookie); } } }
And a helper to get the start page using the content service static class
namespace Business.Helpers { public static class ContentService { private static readonly Injected ContentRepository; public static T GetPage<T>(ContentReference reference) where T : PageData { if (reference == null) return null; return ContentRepository.Service.TryGet(reference, out T content) ? content : null; } public static StartPage GetStartPage() { return GetPage(ContentReference.StartPage); } } }
Finally, we will add the code to control if we track a user or not inside the root cshtml layout file which will be the default template for all pages in the site.
@using Business.Helpers @using EPiServer.Framework.Web.Mvc.Html @using EPiServer.Web.Mvc.Html @model Data.Models.Page.SitePage <!DOCTYPE html> @{ var gdprCookie = SessionService.ReadCookie("GMO_allow-analytics"); var enableAnalytics = gdprCookie == "1"; var settings = ContentService.GetStartPage(); } <html> <head> @{ if (enableAnalytics) { if (!string.IsNullOrWhiteSpace(settings?.GoogleTagManager?.DataLayer)) { Write(Html.Raw(settings.GoogleTagManager.DataLayer)); } } } @Html.RequiredClientResources("Header") </head> <body> @{ if (enableAnalytics) { if (!string.IsNullOrWhiteSpace(settings?.GoogleTagManager?.TagManagerSnippet)) { Write(Html.Raw(settings.GoogleTagManager.TagManagerSnippet)); } } } @Html.FullRefreshPropertiesMetaData() <main id="site-shell" role="main"> @RenderBody() </main> @Html.RequiredClientResources("Footer") </body> </html>
And that is all. Now, if your site set a cookie called Allow-analytics to 1, it will track the user as usual. If the cookie is set to 0 it will not. You can set a cookie using the create cookie method inside the SessionService class. I hope it will help someone and as always keep learning !!!
Leave a Reply