Skip to content

Commit f6421b6

Browse files
authored
Merge pull request #270 from TechnologyEnhancedLearning/Develop/Fixes/TH-TD-4009_Google-Tag-Manager-tracking-for-all-Non-Prod-Environments
TD-4009: Google Tag Manager tracking for all Non Prod Environments
2 parents 9908007 + 29446c2 commit f6421b6

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

LearningHub.Nhs.WebUI/Views/Shared/_GoogleTagManagerNoScriptPartial.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@inject LearningHub.Nhs.WebUI.Configuration.Settings settings
2-
3-
@if (settings.GoogleTagManagerEnabled)
2+
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv
3+
@if (settings.GoogleTagManagerEnabled && hostingEnv.EnvironmentName == "Production")
44
{
55
<!-- Google Tag Manager (noscript) -->
66
<noscript><iframe src="https://www.googletagmanager.com/[email protected]"
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
@inject LearningHub.Nhs.WebUI.Configuration.Settings settings
2+
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv
23
@{
3-
var detectCookieStatusBeforePageLoad = (string)TempData["ObtainUserConsentToTrackPriorToPageLoad"]; // Need to capture cookie status before page load to avoid inconsistencies.
4+
var detectCookieStatusBeforePageLoad = (string)TempData["ObtainUserConsentToTrackPriorToPageLoad"]; // Need to capture cookie status before page load to avoid inconsistencies.
45
}
56

6-
@if (detectCookieStatusBeforePageLoad == "true") // [BY] When the user consent for first time load the follolwing JS. To avoid inconsistencies a temp varaible has been set when the user consent cookie choice
7+
@if (detectCookieStatusBeforePageLoad == "true" && settings.GoogleTagManagerEnabled && hostingEnv.EnvironmentName == "Production") // [BY] When the user consent for first time load the follolwing JS. To avoid inconsistencies a temp varaible has been set when the user consent cookie choice
78
{
8-
@if (settings.GoogleTagManagerEnabled)
9-
{
10-
<!-- Google Tag Manager -->
11-
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
12-
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
13-
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
14-
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
15-
})(window,document,'script','dataLayer','@settings.GoogleTagManagerContainerId');</script>
16-
<!-- End Google Tag Manager -->
17-
}
9+
<!-- Google Tag Manager -->
10+
<script>
11+
(function (w, d, s, l, i) {
12+
w[l] = w[l] || []; w[l].push({
13+
'gtm.start':
14+
new Date().getTime(), event: 'gtm.js'
15+
}); var f = d.getElementsByTagName(s)[0],
16+
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
17+
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
18+
})(window, document, 'script', 'dataLayer', '@settings.GoogleTagManagerContainerId');</script>
19+
<!-- End Google Tag Manager -->
1820
}

0 commit comments

Comments
 (0)