diff --git a/src/Elastic.Markdown/BuildContext.cs b/src/Elastic.Markdown/BuildContext.cs index 5c56566b9..3a1825d7f 100644 --- a/src/Elastic.Markdown/BuildContext.cs +++ b/src/Elastic.Markdown/BuildContext.cs @@ -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; @@ -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; } @@ -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) @@ -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; + } +} diff --git a/src/Elastic.Markdown/Slices/HtmlWriter.cs b/src/Elastic.Markdown/Slices/HtmlWriter.cs index 57129b28d..0b32d2ff3 100644 --- a/src/Elastic.Markdown/Slices/HtmlWriter.cs +++ b/src/Elastic.Markdown/Slices/HtmlWriter.cs @@ -125,7 +125,7 @@ private async Task 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 diff --git a/src/Elastic.Markdown/Slices/Index.cshtml b/src/Elastic.Markdown/Slices/Index.cshtml index c8b6a4312..aca7569ff 100644 --- a/src/Elastic.Markdown/Slices/Index.cshtml +++ b/src/Elastic.Markdown/Slices/Index.cshtml @@ -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 diff --git a/src/Elastic.Markdown/Slices/Layout/_Head.cshtml b/src/Elastic.Markdown/Slices/Layout/_Head.cshtml index ad6fc3e00..2f897dcbd 100644 --- a/src/Elastic.Markdown/Slices/Layout/_Head.cshtml +++ b/src/Elastic.Markdown/Slices/Layout/_Head.cshtml @@ -14,6 +14,15 @@ } + @if (Model.GoogleTagManager.Enabled) + { + + } @await RenderPartialAsync(_Favicon.Create()) @@ -26,14 +35,4 @@ { } - @if (Model.EnableGoogleTagManager) - { - - - - } diff --git a/src/Elastic.Markdown/Slices/_Layout.cshtml b/src/Elastic.Markdown/Slices/_Layout.cshtml index 667a866e1..f1f2d9e73 100644 --- a/src/Elastic.Markdown/Slices/_Layout.cshtml +++ b/src/Elastic.Markdown/Slices/_Layout.cshtml @@ -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) { - - - + } @(await RenderPartialAsync(_Header.Create(Model)))
diff --git a/src/Elastic.Markdown/Slices/_ViewModels.cs b/src/Elastic.Markdown/Slices/_ViewModels.cs index 6ecb73499..75d294b49 100644 --- a/src/Elastic.Markdown/Slices/_ViewModels.cs +++ b/src/Elastic.Markdown/Slices/_ViewModels.cs @@ -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; @@ -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; } } @@ -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; } diff --git a/src/docs-assembler/Configuration/AssemblyConfiguration.cs b/src/docs-assembler/Configuration/AssemblyConfiguration.cs index 6acc0ea0c..383f863e4 100644 --- a/src/docs-assembler/Configuration/AssemblyConfiguration.cs +++ b/src/docs-assembler/Configuration/AssemblyConfiguration.cs @@ -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; } } diff --git a/src/docs-assembler/Navigation/AssemblerDocumentationSet.cs b/src/docs-assembler/Navigation/AssemblerDocumentationSet.cs index 5b4299796..c47aab4cd 100644 --- a/src/docs-assembler/Navigation/AssemblerDocumentationSet.cs +++ b/src/docs-assembler/Navigation/AssemblerDocumentationSet.cs @@ -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, }; diff --git a/src/docs-assembler/YamlStaticContext.cs b/src/docs-assembler/YamlStaticContext.cs index fd57edc10..7fff8253d 100644 --- a/src/docs-assembler/YamlStaticContext.cs +++ b/src/docs-assembler/YamlStaticContext.cs @@ -12,4 +12,5 @@ namespace Documentation.Assembler; [YamlSerializable(typeof(Repository))] [YamlSerializable(typeof(NarrativeRepository))] [YamlSerializable(typeof(PublishEnvironment))] +[YamlSerializable(typeof(GoogleTagManager))] public partial class YamlStaticContext; diff --git a/src/docs-assembler/assembler.yml b/src/docs-assembler/assembler.yml index 3898acbd7..2a2ebd094 100644 --- a/src/docs-assembler/assembler.yml +++ b/src/docs-assembler/assembler.yml @@ -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: