-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagLiquidParserTag.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Completion> WriteToAsync( | ||
IReadOnlyList<FilterArgument> argumentsList, | ||
TextWriter writer, | ||
TextEncoder encoder, | ||
TemplateContext context) | ||
{ | ||
var arguments = new List<FilterArgument> | ||
{ | ||
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<FilterArgument> arguments, | ||
string newName, | ||
FilterArgument argument, | ||
TemplateContext context) | ||
{ | ||
var newValue = await argument.Expression.EvaluateAsync(context); | ||
arguments.Add(new(newName, new LiteralExpression(newValue))); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagTagHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GoogleTagViewModel> | ||
{ | ||
[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<GoogleTagViewModel> GetViewModelAsync(TagHelperContext context, TagHelperOutput output) => | ||
ValueTask.FromResult(new GoogleTagViewModel(PropertyId, CookieDomain)); | ||
} |
20 changes: 20 additions & 0 deletions
20
Lombiq.HelpfulExtensions/Extensions/GoogleTag/GoogleTagViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GoogleTagTagHelper>(); | ||
services.AddLiquidParserTag<GoogleTagLiquidParserTag>("google_tag"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ITrackingConsentFeature>(); | ||
} | ||
|
||
@if (HostEnvironment.IsProduction() && trackingConsentFeature is not { CanTrack: false }) | ||
{ | ||
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}' }}"; | ||
} | ||
|
||
<script async src="https://www.googletagmanager.com/gtag/js?id=@viewModel.GoogleTagPropertyId"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { dataLayer.push(arguments); } | ||
gtag('js', new Date()); | ||
gtag('config', '@viewModel.GoogleTagPropertyId'@Html.Raw(cookieDomain)) | ||
</script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters