Skip to content

Commit

Permalink
[geocat] Add Google Analytics Tag to html pages, only in production
Browse files Browse the repository at this point in the history
switch google analytics 4 to google tag manager
  • Loading branch information
Benoît Regamey authored and cmangeat committed May 23, 2024
1 parent 2b5e582 commit f12ab50
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
27 changes: 27 additions & 0 deletions web/src/main/webapp/xslt/base-layout-cssjs-loader.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,31 @@
}]);
</script>
</xsl:template>

<!-- Google Tag Manager -->
<xsl:template name="ga4-load-head">
<xsl:choose>
<xsl:when test="$isProd">
<script>
<![CDATA[
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-NBPF5L9');
]]>
</script>
</xsl:when>
</xsl:choose>
</xsl:template>

<!-- Google Tag Manager (noscript) -->
<xsl:template name="ga4-load-body">
<xsl:choose>
<xsl:when test="$isProd">
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NBPF5L9"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
14 changes: 10 additions & 4 deletions web/src/main/webapp/xslt/base-layout.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<xsl:variable name="nodeName"
select="util:getNodeName('', $lang, true())"/>

<xsl:variable name="htmlHeadTitle"
<xsl:variable name="gaHtmlHeadTitle"
select="if ($discoveryServiceRecordUuid != '')
then util:getIndexField(
$lang,
Expand All @@ -58,7 +58,7 @@
else $nodeName"/>


<xsl:variable name="htmlHeadDescription"
<xsl:variable name="gaHtmlHeadDescription"
select="if ($discoveryServiceRecordUuid != '')
then util:getIndexField(
$lang,
Expand All @@ -69,12 +69,15 @@
then substring-after($nodeName, '|')
else $nodeName"/>

<title><xsl:value-of select="$htmlHeadTitle"/></title>
<xsl:variable name="actualHtmlHeadDescription" select="if ($isProd) then $gaHtmlHeadDescription else ''"/>
<xsl:variable name="actualHtmlHeadTitle" select="if ($isProd) then $gaHtmlHeadTitle else util:getNodeName('', $lang, true())"/>

<title><xsl:value-of select="$actualHtmlHeadTitle"/></title>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>

<meta name="description" content="{$htmlHeadDescription}"/>
<meta name="description" content="{$actualHtmlHeadDescription}"/>
<meta name="keywords" content=""/>


Expand All @@ -86,6 +89,7 @@
title="{concat($env/system/site/name, ' - ', $env/system/site/organization)}"/>

<xsl:call-template name="css-load"/>
<xsl:call-template name="ga4-load-head"/>
</head>


Expand All @@ -95,6 +99,8 @@
-->
<body data-ng-controller="GnCatController" data-ng-class="[isHeaderFixed ? 'gn-header-fixed' : 'gn-header-relative', isLogoInHeader ? 'gn-logo-in-header' : 'gn-logo-in-navbar', isFooterEnabled ? 'gn-show-footer' : 'gn-hide-footer']">

<xsl:call-template name="ga4-load-body"/>

<div data-gn-alert-manager=""></div>

<xsl:choose>
Expand Down
5 changes: 5 additions & 0 deletions web/src/main/webapp/xslt/common/base-variables.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@


<xsl:variable name="metadataUserFeedbackEnabled" select="$envSystem/localrating/enable = 'advanced'" />

<xsl:variable name="isDev" select="contains($env/system/site/name, '[dev]')" />
<xsl:variable name="isInt" select="contains($env/system/site/name, '[int]')" />
<xsl:variable name="isProd" select="not($isDev) and not($isInt)" />

</xsl:stylesheet>
2 changes: 2 additions & 0 deletions web/src/main/webapp/xslt/common/render-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@
title="{$title}"/>

<xsl:call-template name="css-load-nojs"/>
<xsl:call-template name="ga4-load-head"/>

</head>

<body class="gn-nojs {$cssClass}">
<xsl:call-template name="ga4-load-body"/>
<div class="gn-full">
<xsl:call-template name="header"/>
<div class="container" role="main">
Expand Down

0 comments on commit f12ab50

Please sign in to comment.