Skip to content

Commit

Permalink
Include Google Analytics and Tag Manage when user concents. (#13834)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Jun 10, 2023
1 parent 970f002 commit 3e937ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -32,11 +34,13 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
if ((context.Result is ViewResult || context.Result is PageResult) &&
!AdminAttribute.IsApplied(context.HttpContext))
{
if (_scriptsCache == null)
var canTrack = context.HttpContext.Features.Get<ITrackingConsentFeature>()?.CanTrack ?? true;

if (_scriptsCache == null && canTrack)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<GoogleAnalyticsSettings>();

if (!string.IsNullOrWhiteSpace(settings?.TrackingID))
if (!String.IsNullOrWhiteSpace(settings?.TrackingID))
{
_scriptsCache = new HtmlString($"<!-- Global site tag (gtag.js) - Google Analytics -->\n<script async src=\"https://www.googletagmanager.com/gtag/js?id={settings.TrackingID}\"></script>\n<script>window.dataLayer = window.dataLayer || [];function gtag() {{ dataLayer.push(arguments); }}gtag('js', new Date());gtag('config', '{settings.TrackingID}')</script>\n<!-- End Global site tag (gtag.js) - Google Analytics -->");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -32,11 +34,13 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
if ((context.Result is ViewResult || context.Result is PageResult) &&
!AdminAttribute.IsApplied(context.HttpContext))
{
if (_scriptsCache == null)
var canTrack = context.HttpContext.Features.Get<ITrackingConsentFeature>()?.CanTrack ?? true;

if (_scriptsCache == null && canTrack)
{
var settings = (await _siteService.GetSiteSettingsAsync()).As<GoogleTagManagerSettings>();

if (!string.IsNullOrWhiteSpace(settings?.ContainerID))
if (!String.IsNullOrWhiteSpace(settings?.ContainerID))
{
_scriptsCache = new HtmlString("<!-- Google Tag Manager -->\n<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\n new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\n })(window,document,'script','dataLayer','" + settings.ContainerID + "');</script>\n<!-- End Google Tag Manager -->");
}
Expand Down

0 comments on commit 3e937ab

Please sign in to comment.