Skip to content

Commit

Permalink
Merge pull request #22 from Kentico/fix/opt-out-of-rendering
Browse files Browse the repository at this point in the history
Fix/opt out of rendering
  • Loading branch information
michalJakubis authored May 20, 2024
2 parents c7bcb96 + da03f93 commit 6d4549c
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ dotnet add package Kentico.Xperience.TagManager
- Top of the body - inserts a script immediately after the opening body tag.
- Bottom of the body - inserts a script right before the closing body tag.
- Fill in a consent if required.
- Select whether you want to display tags in the Xperience administration preview or Page Builder, or both. This option defaults to None.
5. During rendering the livesite page, the Tag manager module automatically adds custom code snippets with accepted consents to defined locations.
6. To dynamically update the rendered code snippets, for example if a consent is accepted, call javascript function `window.xperience.tagManager.updateCodeSnippets()`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class CodeSnippetConfigurationModel
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Code { get; set; }

[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetLocationsExtensions.FormComponentOptions)]
[RadioGroupComponent(Label = "Tag location", Order = 5, Options = CodeSnippetExtensions.LocationFormComponentOptions)]
[VisibleIfEqualTo(nameof(TagType), CustomSnippetFactory.TAG_TYPE_NAME)]
public string? Location { get; set; }

Expand All @@ -38,6 +38,9 @@ internal class CodeSnippetConfigurationModel
[ObjectIdSelectorComponent(objectType: ConsentInfo.OBJECT_TYPE, Label = "Consent", Order = 6, Placeholder = "No consent needed")]
public IEnumerable<int> ConsentIDs { get; set; } = [];

[DropDownComponent(Label = "Kentico administration Display Mode", Options = CodeSnippetExtensions.DisplayModeFormComponentOptions, Order = 7)]
public string DisplayMode { get; set; } = "None";

public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
{
info.ChannelCodeSnippetItemChannelId = ChannelIDs.FirstOrDefault();
Expand All @@ -47,13 +50,6 @@ public void MapToChannelCodeSnippetInfo(ChannelCodeSnippetItemInfo info)
info.ChannelCodeSnippetItemName = Name;
info.ChannelCodeSnippetItemIdentifier = TagIdentifier;
info.ChannelCodeSnippetItemCode = Code;
info.ChannelCodeSnippetAdministrationDisplayMode = DisplayMode;
}
}

internal static class CodeSnippetLocationsExtensions
{
public const string FormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
$"{nameof(CodeSnippetLocations.HeadBottom)};Insert at the bottom of the head\r\n" +
$"{nameof(CodeSnippetLocations.BodyTop)};Insert at the top of the body\r\n" +
$"{nameof(CodeSnippetLocations.BodyBottom)};Insert at the bottom of the body\r\n";
}
16 changes: 16 additions & 0 deletions src/Kentico.Xperience.TagManager/Admin/CodeSnippetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Kentico.Xperience.TagManager.Rendering;

namespace Kentico.Xperience.TagManager.Admin;

internal static class CodeSnippetExtensions
{
public const string LocationFormComponentOptions = $"{nameof(CodeSnippetLocations.HeadTop)};Insert at the top of the head\r\n" +
$"{nameof(CodeSnippetLocations.HeadBottom)};Insert at the bottom of the head\r\n" +
$"{nameof(CodeSnippetLocations.BodyTop)};Insert at the top of the body\r\n" +
$"{nameof(CodeSnippetLocations.BodyBottom)};Insert at the bottom of the body\r\n";

public const string DisplayModeFormComponentOptions = $"{nameof(CodeSnippetAdministrationDisplayMode.None)};Do not display in Administration\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PreviewOnly)};Display in the Preview view mode only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.PageBuilderOnly)};Display in the Page Builder only\r\n" +
$"{nameof(CodeSnippetAdministrationDisplayMode.Both)}Display in the Preview view mode and the Page Builder";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CMS.Helpers;
using CMS.ContentEngine;
using CMS.DataProtection;

using Kentico.Xperience.TagManager.Admin;

[assembly: RegisterObjectType(typeof(ChannelCodeSnippetItemInfo), ChannelCodeSnippetItemInfo.OBJECT_TYPE)]
Expand Down Expand Up @@ -120,7 +121,7 @@ public virtual string ChannelCodeSnippetItemLocation


/// <summary>
/// Channel code snippet GTMID.
/// Channel code snippet third party identifier.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetItemIdentifier
Expand All @@ -140,6 +141,16 @@ public virtual string ChannelCodeSnippetItemCode
set => SetValue(nameof(ChannelCodeSnippetItemCode), value, String.Empty);
}

/// <summary>
/// Channel code snippet administration display mode.
/// </summary>
[DatabaseField]
public virtual string ChannelCodeSnippetAdministrationDisplayMode
{
get => ValidationHelper.GetString(GetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode)), String.Empty);
set => SetValue(nameof(ChannelCodeSnippetAdministrationDisplayMode), value, String.Empty);
}


/// <summary>
/// Channel code snippet consent ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ private static void InstallChannelCodeSnippetClass(ResourceInfo resourceInfo)
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetAdministrationDisplayMode),
Visible = false,
Precision = 0,
Size = 200,
DataType = FieldDataType.Text,
Enabled = true,
AllowEmpty = true
};
formInfo.AddFormItem(formItem);

formItem = new FormFieldInfo
{
Name = nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override CodeSnippetConfigurationModel Model
ChannelIDs = [info.ChannelCodeSnippetItemChannelId],
Name = info.ChannelCodeSnippetItemName,
Code = info.ChannelCodeSnippetItemCode,
DisplayMode = info.ChannelCodeSnippetAdministrationDisplayMode,
TagType = info.ChannelCodeSnippetItemType,
ConsentIDs = info.ChannelCodeSnippetItemConsentId == 0 ? [] : [info.ChannelCodeSnippetItemConsentId],
TagIdentifier = info.ChannelCodeSnippetItemIdentifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kentico.Xperience.TagManager.Rendering;

public enum CodeSnippetAdministrationDisplayMode
{
None = default,
PreviewOnly,
PageBuilderOnly,
Both
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class CodeSnippetDto
public int ID { get; init; }
public string? Code { get; init; }
public CodeSnippetLocations Location { get; init; }
public CodeSnippetAdministrationDisplayMode DisplayMode { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using CMS.ContactManagement;

using Kentico.Content.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
Expand All @@ -18,14 +23,17 @@ internal class CodeSnippetTagHelperComponent : TagHelperComponent
private readonly IChannelCodeSnippetsService codeSnippetsContext;
private readonly IUrlHelperFactory urlHelperFactory;
private readonly IFileVersionProvider fileVersionProvider;
private readonly IHttpContextAccessor httpContextAccessor;

public CodeSnippetTagHelperComponent(
IChannelCodeSnippetsService codeSnippetsContext,
IUrlHelperFactory urlHelperFactory,
IFileVersionProvider fileVersionProvider)
IFileVersionProvider fileVersionProvider,
IHttpContextAccessor httpContextAccessor)
{
this.codeSnippetsContext = codeSnippetsContext;
this.urlHelperFactory = urlHelperFactory;
this.httpContextAccessor = httpContextAccessor;
this.fileVersionProvider = fileVersionProvider;
}

Expand All @@ -40,42 +48,92 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
var contact = ContactManagementContext.CurrentContact;

var codeSnippets = await codeSnippetsContext.GetConsentedCodeSnippets(contact);

if (string.Equals(context.TagName, HeadTag, StringComparison.OrdinalIgnoreCase))
{
ProcessHead(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessHead(output, codeSnippets, httpContextAccessor.HttpContext);
}

if (string.Equals(context.TagName, BodyTag, StringComparison.OrdinalIgnoreCase))
{
ProcessBody(output, await codeSnippetsContext.GetConsentedCodeSnippets(contact));
ProcessBody(output, codeSnippets, httpContextAccessor.HttpContext);
}
}

private static void ProcessHead(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var headTopSnippets = codeSnippets[CodeSnippetLocations.HeadTop];
var headBottomSnippets = codeSnippets[CodeSnippetLocations.HeadBottom];

if (isEditMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
headTopSnippets = headTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

headBottomSnippets = headBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in headTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.HeadBottom])
foreach (var codeSnippet in headBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
}

private void ProcessBody(
TagHelperOutput output,
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets)
ILookup<CodeSnippetLocations, CodeSnippetDto> codeSnippets,
HttpContext? httpContext)
{
foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyTop])
bool isEditMode = httpContext.Kentico().PageBuilder().EditMode;
bool isPreviewMode = httpContext.Kentico().Preview().Enabled;

var bodyTopSnippets = codeSnippets[CodeSnippetLocations.BodyTop];
var bodyBottomSnippets = codeSnippets[CodeSnippetLocations.BodyBottom];

if (isEditMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PageBuilderOnly);
}
else if (isPreviewMode)
{
bodyTopSnippets = bodyTopSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);

bodyBottomSnippets = bodyBottomSnippets.Where(x => x.DisplayMode is CodeSnippetAdministrationDisplayMode.Both or
CodeSnippetAdministrationDisplayMode.PreviewOnly);
}

foreach (var codeSnippet in bodyTopSnippets)
{
output.PreContent.AppendHtml(codeSnippet.Code);
}

foreach (var codeSnippet in codeSnippets[CodeSnippetLocations.BodyBottom])
foreach (var codeSnippet in bodyBottomSnippets)
{
output.PostContent.AppendHtml(codeSnippet.Code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class DefaultChannelCodeSnippetsService : IChannelCodeSnippetsService
{
private readonly IConsentAgreementService consentAgreementService;
private readonly IWebsiteChannelContext channelContext;

private readonly IChannelCodeSnippetItemInfoProvider codeSnippetInfoProvider;
private readonly IProgressiveCache cache;

Expand Down Expand Up @@ -70,6 +71,7 @@ async Task<ILookup<CodeSnippetLocations, CodeSnippetDto>> GetCodeSnippetsInterna
.Columns(nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemLocation),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemCode),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemConsentId),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetAdministrationDisplayMode),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemIdentifier),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemID),
nameof(ChannelCodeSnippetItemInfo.ChannelCodeSnippetItemType),
Expand Down Expand Up @@ -99,13 +101,19 @@ private static IEnumerable<CodeSnippetDto> CreateCodeSnippet(ChannelCodeSnippetI

var tags = new List<CodeSnippetDto>();

if (!Enum.TryParse(snippetInfo.ChannelCodeSnippetAdministrationDisplayMode, out CodeSnippetAdministrationDisplayMode displayMode))
{
displayMode = CodeSnippetAdministrationDisplayMode.None;
}

if (snippetSettings.TagTypeName != CustomSnippetFactory.TAG_TYPE_NAME)
{
tags.AddRange(snippetFactory.CreateCodeSnippets(snippetInfo.ChannelCodeSnippetItemIdentifier).Select(x => new CodeSnippetDto
{
Location = x.Location,
Code = x.Code,
ID = snippetInfo.ChannelCodeSnippetItemID
ID = snippetInfo.ChannelCodeSnippetItemID,
DisplayMode = displayMode
}));
}
else
Expand All @@ -117,6 +125,7 @@ private static IEnumerable<CodeSnippetDto> CreateCodeSnippet(ChannelCodeSnippetI
Location = Enum.TryParse(snippetInfo.ChannelCodeSnippetItemLocation, out CodeSnippetLocations location)
? location
: throw new InvalidOperationException("Invalid Channel Tag Location."),
DisplayMode = displayMode
});

tags.Add(tag);
Expand All @@ -130,7 +139,8 @@ private static CodeSnippetDto AdjustCustomCodeSnippet(CodeSnippetDto codeSnippet
{
Code = codeSnippet.Code != null ? AddSnippetIds(codeSnippet.ID, codeSnippet.Code!) : codeSnippet.Code,
ID = codeSnippet.ID,
Location = codeSnippet.Location
Location = codeSnippet.Location,
DisplayMode = codeSnippet.DisplayMode
};

private static string AddSnippetIds(int codeSnippetId, string codeSnippet) =>
Expand Down

0 comments on commit 6d4549c

Please sign in to comment.