Skip to content
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
33 changes: 32 additions & 1 deletion src/Elastic.Markdown/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.Web;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO;
using Elastic.Markdown.IO.Configuration;
Expand Down Expand Up @@ -34,7 +36,7 @@ public record BuildContext
// This property is used to determine if the site should be indexed by search engines
public bool AllowIndexing { get; init; }

public bool EnableGoogleTagManager { get; init; }
public GoogleTagManagerConfiguration GoogleTagManager { get; init; }

// This property is used for the canonical URL
public Uri? CanonicalBaseUrl { get; init; }
Expand Down Expand Up @@ -82,6 +84,10 @@ public BuildContext(

Git = gitCheckoutInformation ?? GitCheckoutInformation.Create(DocumentationCheckoutDirectory, ReadFileSystem);
Configuration = new ConfigurationFile(this);
GoogleTagManager = new GoogleTagManagerConfiguration
{
Enabled = false
};
}

private (IDirectoryInfo, IFileInfo) FindDocsFolderFromRoot(IDirectoryInfo rootPath)
Expand Down Expand Up @@ -110,3 +116,28 @@ from folder in knownFolders
}

}

public record GoogleTagManagerConfiguration
{
public bool Enabled { get; init; }
[MemberNotNullWhen(returnValue: true, nameof(Enabled))]
public string? Id { get; init; }
public string? Auth { get; init; }
public string? Preview { get; init; }
public string? CookiesWin { get; init; }

public string QueryString()
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
if (Auth is not null)
queryString.Add("gtm_auth", Auth);

if (Preview is not null)
queryString.Add("gtm_preview", Preview);

if (CookiesWin is not null)
queryString.Add("gtm_cookies_win", CookiesWin);

return queryString.Count > 0 ? $"&{queryString}" : string.Empty;
}
}
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/Slices/HtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
GithubEditUrl = editUrl,
AllowIndexing = DocumentationSet.Build.AllowIndexing && !markdown.Hidden,
CanonicalBaseUrl = DocumentationSet.Build.CanonicalBaseUrl,
EnableGoogleTagManager = DocumentationSet.Build.EnableGoogleTagManager,
GoogleTagManager = DocumentationSet.Build.GoogleTagManager,
Features = DocumentationSet.Configuration.Features,
StaticFileContentHashProvider = StaticFileContentHashProvider,
ReportIssueUrl = reportUrl
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/Slices/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
GithubEditUrl = Model.GithubEditUrl,
AllowIndexing = Model.AllowIndexing,
CanonicalBaseUrl = Model.CanonicalBaseUrl,
EnableGoogleTagManager = Model.EnableGoogleTagManager,
GoogleTagManager = Model.GoogleTagManager,
Features = Model.Features,
StaticFileContentHashProvider = Model.StaticFileContentHashProvider,
ReportIssueUrl = Model.ReportIssueUrl
Expand Down
19 changes: 9 additions & 10 deletions src/Elastic.Markdown/Slices/Layout/_Head.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
<link rel="canonical" href="@Model.CanonicalUrl" />
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@if (Model.GoogleTagManager.Enabled)
{
<script>(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+'@(new HtmlString(Model.GoogleTagManager.QueryString()))';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','@Model.GoogleTagManager.Id');
</script>
}
@await RenderPartialAsync(_Favicon.Create())
<meta name="robots" content="@(Model.AllowIndexing ? "index, follow" : "noindex, nofollow")">
<meta name="htmx-config" content='{"selfRequestsOnly": true}'>
Expand All @@ -26,14 +35,4 @@
{
<meta property="og:url" content="@Model.CanonicalUrl" />
}
@if (Model.EnableGoogleTagManager)
{
<!-- Google Tag Manager -->
<script>(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+ '&gtm_auth=nPocPUG0wiH68jsVeyRSxA&gtm_preview=env-507&gtm_cookies_win=x';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-KNJMG2M');</script>
<!-- End Google Tag Manager -->
}
</head>
13 changes: 3 additions & 10 deletions src/Elastic.Markdown/Slices/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
class="group/body text-ink has-[#primary-nav-hamburger:checked]:overflow-hidden"
hx-ext="preload, head-support"
data-root-path="@Model.Link("/")">
@if (Model.EnableGoogleTagManager)
@if (Model.GoogleTagManager.Enabled)
{
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KNJMG2M&gtm_auth=nPocPUG0wiH68jsVeyRSxA&gtm_preview=env-507&gtm_cookies_win=x"
height="0"
width="0"
style="display:none;visibility:hidden">
</iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=@(Model.GoogleTagManager.Id)@(new HtmlString(Model.GoogleTagManager.QueryString()))"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
}
@(await RenderPartialAsync(_Header.Create(Model)))
<div id="main-container" class="flex flex-col items-center border-t-1 border-grey-20">
Expand Down
8 changes: 5 additions & 3 deletions src/Elastic.Markdown/Slices/_ViewModels.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Markdown.IO;
using Elastic.Markdown.IO.Configuration;
using Elastic.Markdown.IO.Navigation;
Expand Down Expand Up @@ -29,7 +30,9 @@ public class IndexViewModel
public required ApplicableTo? Applies { get; init; }
public required bool AllowIndexing { get; init; }
public required Uri? CanonicalBaseUrl { get; init; }
public required bool EnableGoogleTagManager { get; init; }

public required GoogleTagManagerConfiguration GoogleTagManager { get; init; }

public required FeatureFlags Features { get; init; }
public required StaticFileContentHashProvider StaticFileContentHashProvider { get; init; }
}
Expand All @@ -54,8 +57,7 @@ public class LayoutViewModel
public required string? ReportIssueUrl { get; init; }
public required bool AllowIndexing { get; init; }
public required Uri? CanonicalBaseUrl { get; init; }
public required bool EnableGoogleTagManager { get; init; }

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

Expand Down
31 changes: 29 additions & 2 deletions src/docs-assembler/Configuration/AssemblyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ public record PublishEnvironment
[YamlMember(Alias = "allow_indexing")]
public bool AllowIndexing { get; set; }

[YamlMember(Alias = "enable_google_tag_manager")]
public bool? EnableGoogleTagManager { get; set; }
[YamlMember(Alias = "google_tag_manager")]
public GoogleTagManager GoogleTagManager { get; set; } = new();
}

public record GoogleTagManager
{
[YamlMember(Alias = "enabled")]
public bool Enabled { get; set; }

private string _id = string.Empty;
[YamlMember(Alias = "id")]
public string Id
{
get => _id;
set
{
if (Enabled && string.IsNullOrEmpty(value))
throw new ArgumentException("Id is required when Enabled is true.");
_id = value;
}
}
[YamlMember(Alias = "auth")]
public string? Auth { get; set; }

[YamlMember(Alias = "preview")]
public string? Preview { get; set; }

[YamlMember(Alias = "cookies_win")]
public string? CookiesWin { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ TableOfContentsTreeCollector treeCollector
UrlPathPrefix = env.PathPrefix,
Force = true,
AllowIndexing = env.AllowIndexing,
EnableGoogleTagManager = env.EnableGoogleTagManager ?? false,
GoogleTagManager = new GoogleTagManagerConfiguration
{
Enabled = env.GoogleTagManager.Enabled,
Id = env.GoogleTagManager.Id,
Auth = env.GoogleTagManager.Auth,
Preview = env.GoogleTagManager.Preview,
CookiesWin = env.GoogleTagManager.CookiesWin
},
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.
SkipMetadata = true,
};
Expand Down
1 change: 1 addition & 0 deletions src/docs-assembler/YamlStaticContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ namespace Documentation.Assembler;
[YamlSerializable(typeof(Repository))]
[YamlSerializable(typeof(NarrativeRepository))]
[YamlSerializable(typeof(PublishEnvironment))]
[YamlSerializable(typeof(GoogleTagManager))]
public partial class YamlStaticContext;
11 changes: 9 additions & 2 deletions src/docs-assembler/assembler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ environments:
uri: https://www.elastic.co
path_prefix: docs
allow_indexing: false
enable_google_tag_manager: true
google_tag_manager:
enabled: true
id: GTM-KNJMG2M
staging:
uri: https://staging-website.elastic.co
path_prefix: docs
enable_google_tag_manager: true
google_tag_manager:
enabled: true
id: GTM-KNJMG2M
auth: nPocPUG0wiH68jsVeyRSxA
preview: env-507
cookies_win: x
preview:
uri: https://docs-v3-preview.elastic.dev
path_prefix:
Expand Down
Loading