Skip to content

Instantly share code, notes, and snippets.

@alasvant
Last active February 23, 2019 13:03
Show Gist options
  • Save alasvant/865401a9902289de14c078ee946f7eee to your computer and use it in GitHub Desktop.
Save alasvant/865401a9902289de14c078ee946f7eee to your computer and use it in GitHub Desktop.
Sample Google Tag Manager Episerver Forms element block.
using AlloyWithFind.Business.SelectionFactories;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Forms.Core;
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.EditView;
using EPiServer.Shell.ObjectEditing;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace AlloyWithFind.Models.FormElements
{
/// <summary>
/// Google Tag Manager category and form name element block.
/// </summary>
[ContentType(DisplayName = "TagManagerElementBlock", GUID = "2fdc67f3-cdb1-4fab-9f00-bf22cf5565ae", GroupName = ConstantsFormsUI.FormElementGroup, Description = "Contains Tag Manager category and form name for a form submission event.", Order = 100)]
public class TagManagerElementBlock : ElementBlockBase, IViewModeInvisibleElement, IElementRequireClientResources // NOTE! IElementRequireClientResources is in Episerver internal namespace!
{
// hide label field from editors
[ScaffoldColumn(false)]
public override string Label { get => base.Label; set => base.Label = value; }
// hide description (aka tooltip) field from editors
[ScaffoldColumn(false)]
public override string Description { get => base.Description; set => base.Description = value; }
[CultureSpecific(true)]
[Display(Name = "FormName", Description = "Enter the Google Tag Manager form name here.", GroupName = SystemTabNames.Content, Order = 10)]
public virtual string FormName { get; set; }
[SelectOne(SelectionFactoryType = typeof(GtmCategorySelectionFactory))]
[CultureSpecific(false)]
[Display(Name = "CategoryName", Description = "Google Tag Manager category name.", GroupName = SystemTabNames.Content, Order = 20)]
public virtual string CategoryName { get; set; }
// this is shown to editors
public override string EditViewFriendlyTitle => $"GTM submission, Form name: '{FormName}', Category name: '{CategoryName}'";
public IEnumerable<Tuple<string, string>> GetExtraResources()
{
return new List<Tuple<string, string>>
{
// injected to the page, contains the GTM javascript to push to datalayer
new Tuple<string, string>("script", "/Static/js/gtm-submission.js?v=1.0")
};
}
public override bool IsElementValidToSubmit()
{
// documentation says that this means can the data be stored as submitted data
// no this doesn't need to be saved
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment