From 3f312f60f2129b26cf4fb434358ef3550b31a67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sun, 4 Aug 2024 22:41:49 +0200 Subject: [PATCH 01/11] Add Google Tag (shape, Liquid and Razor tag helpers). --- .../GoogleTag/GoogleTagLiquidParserTag.cs | 50 +++++++++++++++++++ .../GoogleTag/GoogleTagTagHelper.cs | 26 ++++++++++ .../GoogleTag/GoogleTagViewModel.cs | 20 ++++++++ .../Extensions/GoogleTag/Startup.cs | 14 ++++++ Lombiq.HelpfulExtensions/FeatureIds.cs | 1 + .../Lombiq.HelpfulExtensions.csproj | 2 +- Lombiq.HelpfulExtensions/Manifest.cs | 7 +++ .../Views/GoogleTag.cshtml | 38 ++++++++++++++ 8 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagLiquidParserTag.cs create mode 100644 Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagTagHelper.cs create mode 100644 Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagViewModel.cs create mode 100644 Lombiq.HelpfulExtensions/Extensions/GoogleTag/Startup.cs create mode 100644 Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml diff --git a/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagLiquidParserTag.cs b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagLiquidParserTag.cs new file mode 100644 index 00000000..303f7969 --- /dev/null +++ b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagLiquidParserTag.cs @@ -0,0 +1,50 @@ +using Fluid; +using Fluid.Ast; +using Fluid.Values; +using Lombiq.HelpfulLibraries.OrchardCore.Liquid; +using OrchardCore.DisplayManagement.Liquid.Tags; +using System.Collections.Generic; +using System.IO; +using System.Text.Encodings.Web; +using System.Threading.Tasks; + +namespace Lombiq.HelpfulExtensions.Extensions.GoogleTag; + +public class GoogleTagLiquidParserTag : ILiquidParserTag +{ + public async ValueTask WriteToAsync( + IReadOnlyList argumentsList, + TextWriter writer, + TextEncoder encoder, + TemplateContext context) + { + var arguments = new List + { + new(null, new LiteralExpression(new StringValue(GoogleTagViewModel.ShapeType))), + }; + + foreach (var argument in argumentsList) + { + if (argument.Name == "property_id") + { + await AddStringAsync(arguments, nameof(GoogleTagViewModel.GoogleTagPropertyId), argument, context); + } + else if (argument.Name == "cookie_domain") + { + await AddStringAsync(arguments, nameof(GoogleTagViewModel.CookieDomain), argument, context); + } + } + + return await ShapeTag.WriteToAsync(arguments, writer, encoder, context); + } + + private static async Task AddStringAsync( + List arguments, + string newName, + FilterArgument argument, + TemplateContext context) + { + var newValue = await argument.Expression.EvaluateAsync(context); + arguments.Add(new(newName, new LiteralExpression(newValue))); + } +} diff --git a/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagTagHelper.cs b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagTagHelper.cs new file mode 100644 index 00000000..366e8694 --- /dev/null +++ b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagTagHelper.cs @@ -0,0 +1,26 @@ +using Lombiq.HelpfulLibraries.OrchardCore.TagHelpers; +using Microsoft.AspNetCore.Razor.TagHelpers; +using OrchardCore.DisplayManagement; +using System.Threading.Tasks; + +namespace Lombiq.HelpfulExtensions.Extensions.GoogleTag; + +[HtmlTargetElement("google-tag")] +public class GoogleTagTagHelper : ShapeTagHelperBase +{ + [HtmlAttributeName("property-id")] + public string PropertyId { get; set; } + + [HtmlAttributeName("cookie-domain")] + public string CookieDomain { get; set; } + + public GoogleTagTagHelper(IDisplayHelper displayHelper, IShapeFactory shapeFactory) + : base(displayHelper, shapeFactory) + { + } + + protected override string ShapeType => GoogleTagViewModel.ShapeType; + + protected override ValueTask GetViewModelAsync(TagHelperContext context, TagHelperOutput output) => + ValueTask.FromResult(new GoogleTagViewModel(PropertyId, CookieDomain)); +} diff --git a/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagViewModel.cs b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagViewModel.cs new file mode 100644 index 00000000..8064e1c3 --- /dev/null +++ b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagViewModel.cs @@ -0,0 +1,20 @@ +using OrchardCore.DisplayManagement.Views; + +namespace Lombiq.HelpfulExtensions.Extensions.GoogleTag; + +public class GoogleTagViewModel : ShapeViewModel +{ + public const string ShapeType = "GoogleTag"; + + public string GoogleTagPropertyId { get; set; } + public string CookieDomain { get; set; } + + public GoogleTagViewModel() => Metadata.Type = ShapeType; + + public GoogleTagViewModel(string googleTagPropertyId, string cookieDomain) + : this() + { + GoogleTagPropertyId = googleTagPropertyId; + CookieDomain = cookieDomain; + } +} diff --git a/Lombiq.HelpfulExtensions/Extensions/GoogleTag/Startup.cs b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/Startup.cs new file mode 100644 index 00000000..2f16dd99 --- /dev/null +++ b/Lombiq.HelpfulExtensions/Extensions/GoogleTag/Startup.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using OrchardCore.Modules; + +namespace Lombiq.HelpfulExtensions.Extensions.GoogleTag; + +[Feature(FeatureIds.GoogleTag)] +public class Startup : StartupBase +{ + public override void ConfigureServices(IServiceCollection services) + { + services.AddTagHelpers(); + services.AddLiquidParserTag("google_tag"); + } +} diff --git a/Lombiq.HelpfulExtensions/FeatureIds.cs b/Lombiq.HelpfulExtensions/FeatureIds.cs index 3cd3b3ee..180370db 100644 --- a/Lombiq.HelpfulExtensions/FeatureIds.cs +++ b/Lombiq.HelpfulExtensions/FeatureIds.cs @@ -19,4 +19,5 @@ public static class FeatureIds public const string Workflows = FeatureIdPrefix + nameof(Workflows); public const string Trumbowyg = FeatureIdPrefix + nameof(Trumbowyg); public const string ResetPasswordActivity = Workflows + "." + nameof(ResetPasswordActivity); + public const string GoogleTag = FeatureIdPrefix + nameof(GoogleTag); } diff --git a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj index 1f42243f..945f7bdb 100644 --- a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj +++ b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj @@ -41,7 +41,7 @@ - + diff --git a/Lombiq.HelpfulExtensions/Manifest.cs b/Lombiq.HelpfulExtensions/Manifest.cs index 4aaf914e..265e8f3a 100644 --- a/Lombiq.HelpfulExtensions/Manifest.cs +++ b/Lombiq.HelpfulExtensions/Manifest.cs @@ -146,3 +146,10 @@ Category = "Content", Description = "Adds option for inserting code snippets in Trumbowyg editor." )] + +[assembly: Feature( + Id = GoogleTag, + Name = "Lombiq Helpful Extensions - Google Tag", + Category = "Content", + Description = "Adds a shape along with Razor and Liquid tag helpers for Google Analytics." +)] diff --git a/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml new file mode 100644 index 00000000..684c69d0 --- /dev/null +++ b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml @@ -0,0 +1,38 @@ +@model dynamic + +@using Lombiq.HelpfulLibraries.OrchardCore.Security +@using Microsoft.AspNetCore.Http.Features; +@using Microsoft.Extensions.Hosting; +@using Lombiq.HelpfulExtensions.Extensions.GoogleTag + +@inject IHostEnvironment HostEnvironment + +@{ + var trackingConsentFeature = ViewContext.HttpContext.Features.Get(); +} + +@if (HostEnvironment.IsProduction() && (trackingConsentFeature is null || trackingConsentFeature.CanTrack)) +{ + GoogleAnalyticsContentSecurityPolicyProvider.EnableForCurrentRequest(Context); + + var viewModel = Model as GoogleTagViewModel ?? new GoogleTagViewModel + { + GoogleTagPropertyId = Model.GoogleTagPropertyId, + CookieDomain = Model.CookieDomain, + }; + var cookieDomain = string.Empty; + + if (!string.IsNullOrEmpty(viewModel.CookieDomain)) + { + cookieDomain = $", {{'cookie_domain': '{viewModel.CookieDomain}' }}"; + } + + + +} From ebbdc4262f0ad71f48f2ce2ee60c072f72131c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 13 Aug 2024 11:27:18 +0200 Subject: [PATCH 02/11] Update OC previews. --- .../Lombiq.HelpfulExtensions.csproj | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj index 945f7bdb..be8b6ae0 100644 --- a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj +++ b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj @@ -35,18 +35,18 @@ - - - - - - - - - - - - + + + + + + + + + + + + From 1d44a13901ef14c7b06b1f96b326f62e7b81c14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sun, 18 Aug 2024 01:38:43 +0200 Subject: [PATCH 03/11] Update OC preview version. --- .../Lombiq.HelpfulExtensions.csproj | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj index f96fb687..f977e3c7 100644 --- a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj +++ b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj @@ -35,18 +35,18 @@ - - - - - - - - - - - - + + + + + + + + + + + + From df635443db658eff9f4ff0f194dd3e81c22b1190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sun, 18 Aug 2024 01:56:45 +0200 Subject: [PATCH 04/11] Post OC update fixup. --- Lombiq.HelpfulExtensions/Views/ContentSetPart.MemberLink.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulExtensions/Views/ContentSetPart.MemberLink.cshtml b/Lombiq.HelpfulExtensions/Views/ContentSetPart.MemberLink.cshtml index 7fc338b8..aa371ffc 100644 --- a/Lombiq.HelpfulExtensions/Views/ContentSetPart.MemberLink.cshtml +++ b/Lombiq.HelpfulExtensions/Views/ContentSetPart.MemberLink.cshtml @@ -13,7 +13,7 @@
  • @if (!string.IsNullOrEmpty(link.ContentItemId)) { - var url = Orchard.Action(controller => controller.Edit(link.ContentItemId, null)); + var url = Orchard.Action(controller => controller.Edit(link.ContentItemId)); @link.DisplayText From 8c395bb9271bef59126e4bf164220a60053a550e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 20 Aug 2024 12:32:18 +0200 Subject: [PATCH 05/11] Update OC preview version. --- .../Lombiq.HelpfulExtensions.csproj | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj index f977e3c7..f288362e 100644 --- a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj +++ b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj @@ -35,18 +35,18 @@ - - - - - - - - - - - - + + + + + + + + + + + + From 02726cf1853625f0c18779f382886ef9f2408413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 21 Aug 2024 00:47:48 +0200 Subject: [PATCH 06/11] Add Google Tag note in the readme. --- Readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Readme.md b/Readme.md index 66a39922..28b35c4b 100644 --- a/Readme.md +++ b/Readme.md @@ -222,6 +222,12 @@ builder.WhenContentTypeEditor("BlogPost").RegisterFootScript(Lombiq.HelpfulExten builder.WhenContentTypeEditor("BlogPost").RegisterStylesheet(Lombiq.HelpfulExtensions.Constants.ResourceNames.TrumbowygHighlight); ``` +### Google Tag + +Adds a shape along with Razor and Liquid tag helpers for Google Analytics, using . + +You can use `` Razor tag helper in cshtml files or the `{% google_tag property_id: "...", cookie_domain: "auto" %}` parser tag in Liquid. + ## Contributing and support Bug reports, feature requests, comments, questions, code contributions and love letters are warmly welcome. You can send them to us via GitHub issues and pull requests. Please adhere to our [open-source guidelines](https://lombiq.com/open-source-guidelines) while doing so. From b303f772feee760ea9ab692a0a1ebd9f0b4558a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Wed, 21 Aug 2024 17:30:38 +0200 Subject: [PATCH 07/11] Grammar, formatting --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 28b35c4b..625fe565 100644 --- a/Readme.md +++ b/Readme.md @@ -226,7 +226,7 @@ builder.WhenContentTypeEditor("BlogPost").RegisterStylesheet(Lombiq.HelpfulExten Adds a shape along with Razor and Liquid tag helpers for Google Analytics, using . -You can use `` Razor tag helper in cshtml files or the `{% google_tag property_id: "...", cookie_domain: "auto" %}` parser tag in Liquid. +You can use the `` Razor tag helper in _cshtml_ files or the `{% google_tag property_id: "...", cookie_domain: "auto" %}` parser tag in Liquid. ## Contributing and support From 28e4095c63e6a3f52ba1464879b1de0b0acf3c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Wed, 21 Aug 2024 17:49:35 +0200 Subject: [PATCH 08/11] MD formatting --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 625fe565..979ace5c 100644 --- a/Readme.md +++ b/Readme.md @@ -226,7 +226,7 @@ builder.WhenContentTypeEditor("BlogPost").RegisterStylesheet(Lombiq.HelpfulExten Adds a shape along with Razor and Liquid tag helpers for Google Analytics, using . -You can use the `` Razor tag helper in _cshtml_ files or the `{% google_tag property_id: "...", cookie_domain: "auto" %}` parser tag in Liquid. +You can use the `` Razor tag helper in _cshtml_ files or the `{% google_tag property_id: "...", cookie_domain: "auto" %}` parser tag in Liquid. ## Contributing and support From 3088aef84a8569d24c74f1f17317b9ce6bb59093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 21 Aug 2024 17:57:59 +0200 Subject: [PATCH 09/11] Update OC preview version. --- .../Lombiq.HelpfulExtensions.csproj | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj index f288362e..538347fd 100644 --- a/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj +++ b/Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj @@ -35,18 +35,18 @@ - - - - - - - - - - - - + + + + + + + + + + + + From e13a2c2a4d740203c6a70065cd7a1de26594bf0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 21 Aug 2024 22:22:35 +0200 Subject: [PATCH 10/11] Simplify condition. --- Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml index 684c69d0..92ba2452 100644 --- a/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml +++ b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml @@ -11,7 +11,7 @@ var trackingConsentFeature = ViewContext.HttpContext.Features.Get(); } -@if (HostEnvironment.IsProduction() && (trackingConsentFeature is null || trackingConsentFeature.CanTrack)) +@if (HostEnvironment.IsProduction() && trackingConsentFeature is not { CanTrack: false }) { GoogleAnalyticsContentSecurityPolicyProvider.EnableForCurrentRequest(Context); From a3a68e6ff2e22a1ef7e0665f06315142a5c4e7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 21 Aug 2024 23:20:17 +0200 Subject: [PATCH 11/11] Remove the at="Head" from GoogleTag. --- Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml index 92ba2452..2d592612 100644 --- a/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml +++ b/Lombiq.HelpfulExtensions/Views/GoogleTag.cshtml @@ -27,8 +27,8 @@ cookieDomain = $", {{'cookie_domain': '{viewModel.CookieDomain}' }}"; } - - +