Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Google Analytics and Tag Manager when user consents #13834

Merged
merged 1 commit into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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