Skip to content

Commit e0fdbe8

Browse files
authored
Add google tag manager (#808)
* Add google tag manager * Add fallback noscript
1 parent 513ce72 commit e0fdbe8

File tree

10 files changed

+44
-5
lines changed

10 files changed

+44
-5
lines changed

src/Elastic.Markdown/Assets/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import {$, $$} from "select-dom"
1414
import { UAParser } from 'ua-parser-js';
1515
const { getOS } = new UAParser();
1616

17+
document.addEventListener('htmx:pushedIntoHistory', function(event) {
18+
window.dataLayer = window.dataLayer || [];
19+
window.dataLayer.push({
20+
event: "virtualPageview",
21+
page_path: event.detail.path,
22+
});
23+
});
1724

1825
// Don't remove style tags because they are used by the elastic global nav.
1926
document.addEventListener('htmx:removingHeadElement', function(event) {

src/Elastic.Markdown/BuildContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public record BuildContext
3535
// This property is used to determine if the site should be indexed by search engines
3636
public bool AllowIndexing { get; init; }
3737

38+
public bool EnableGoogleTagManager { get; init; }
39+
3840
// This property is used for the canonical URL
3941
public Uri? CanonicalBaseUrl { get; init; }
4042

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
116116
GithubEditUrl = editUrl,
117117
AllowIndexing = DocumentationSet.Build.AllowIndexing && !markdown.Hidden,
118118
CanonicalBaseUrl = DocumentationSet.Build.CanonicalBaseUrl,
119+
EnableGoogleTagManager = DocumentationSet.Build.EnableGoogleTagManager,
119120
Features = DocumentationSet.Configuration.Features,
120121
StaticFileContentHashProvider = StaticFileContentHashProvider
121122
});

src/Elastic.Markdown/Slices/Index.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
GithubEditUrl = Model.GithubEditUrl,
1717
AllowIndexing = Model.AllowIndexing,
1818
CanonicalBaseUrl = Model.CanonicalBaseUrl,
19+
EnableGoogleTagManager = Model.EnableGoogleTagManager,
1920
Features = Model.Features,
2021
StaticFileContentHashProvider = Model.StaticFileContentHashProvider
2122
};

src/Elastic.Markdown/Slices/Layout/_Head.cshtml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
{
2727
<meta property="og:url" content="@Model.CanonicalUrl" />
2828
}
29+
@if (Model.EnableGoogleTagManager)
30+
{
31+
<!-- Google Tag Manager -->
32+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
33+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
34+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
35+
'https://www.googletagmanager.com/gtm.js?id='+i+dl+ '&gtm_auth=nPocPUG0wiH68jsVeyRSxA&gtm_preview=env-507&gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
36+
})(window,document,'script','dataLayer','GTM-KNJMG2M');</script>
37+
<!-- End Google Tag Manager -->
38+
}
2939
</head>

src/Elastic.Markdown/Slices/_Layout.cshtml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
<!DOCTYPE html>
44
<html lang="en" class="h-screen">
55
@await RenderPartialAsync(_Head.Create(Model))
6-
<body
7-
class="group/body text-ink has-[#primary-nav-hamburger:checked]:overflow-hidden"
6+
<body
7+
class="group/body text-ink has-[#primary-nav-hamburger:checked]:overflow-hidden"
88
hx-ext="preload, head-support"
9-
data-root-path="@Model.Link("/")"
10-
>
9+
data-root-path="@Model.Link("/")">
10+
@if (Model.EnableGoogleTagManager)
11+
{
12+
<!-- Google Tag Manager (noscript) -->
13+
<noscript>
14+
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KNJMG2M&gtm_auth=nPocPUG0wiH68jsVeyRSxA&gtm_preview=env-507&gtm_cookies_win=x"
15+
height="0"
16+
width="0"
17+
style="display:none;visibility:hidden">
18+
</iframe>
19+
</noscript>
20+
<!-- End Google Tag Manager (noscript) -->
21+
}
1122
@(await RenderPartialAsync(_Header.Create(Model)))
1223
<div id="main-container" class="flex flex-col items-center px-6 border-t-1 border-grey-20">
1324
@functions {
@@ -35,7 +46,6 @@
3546
</div>
3647
}
3748
}
38-
3949
@switch (Model.CurrentDocument.YamlFrontMatter?.Layout)
4050
{
4151
case LayoutName.NotFound:

src/Elastic.Markdown/Slices/_ViewModels.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class IndexViewModel
2727
public required ApplicableTo? Applies { get; init; }
2828
public required bool AllowIndexing { get; init; }
2929
public required Uri? CanonicalBaseUrl { get; init; }
30+
public required bool EnableGoogleTagManager { get; init; }
3031
public required FeatureFlags Features { get; init; }
3132
public required StaticFileContentHashProvider StaticFileContentHashProvider { get; init; }
3233
}
@@ -50,6 +51,7 @@ public class LayoutViewModel
5051
public required string? GithubEditUrl { get; init; }
5152
public required bool AllowIndexing { get; init; }
5253
public required Uri? CanonicalBaseUrl { get; init; }
54+
public required bool EnableGoogleTagManager { get; init; }
5355

5456
public string? CanonicalUrl => CanonicalBaseUrl is not null ? new Uri(CanonicalBaseUrl, CurrentDocument.Url).ToString() : null;
5557
public required FeatureFlags Features { get; init; }

src/docs-assembler/Building/AssemblerBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private async Task BuildAsync(Checkout checkout, PublishEnvironment environment,
7070
UrlPathPrefix = environment.PathPrefix,
7171
Force = false,
7272
AllowIndexing = environment.AllowIndexing,
73+
EnableGoogleTagManager = environment.EnableGoogleTagManager ?? false,
7374
CanonicalBaseUrl = new Uri("https://www.elastic.co"), // Always use the production URL. In case a page is leaked to a search engine, it should point to the production site.
7475
SkipMetadata = true
7576
};

src/docs-assembler/Configuration/AssemblyConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,7 @@ public record PublishEnvironment
8585

8686
[YamlMember(Alias = "allow_indexing")]
8787
public bool AllowIndexing { get; set; }
88+
89+
[YamlMember(Alias = "enable_google_tag_manager")]
90+
public bool? EnableGoogleTagManager { get; set; }
8891
}

src/docs-assembler/assembler.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ environments:
33
uri: https://www.elastic.co
44
path_prefix: docs
55
allow_indexing: false
6+
enable_google_tag_manager: true
67
staging:
78
uri: https://staging-website.elastic.co
89
path_prefix: docs
10+
enable_google_tag_manager: true
911
preview:
1012
uri: https://docs-v3-preview.elastic.dev
1113
path_prefix:

0 commit comments

Comments
 (0)