I am trying to build a basic tag management solution in SiteCore.
I have created a folder called Tag Management under Templates. Under the Tag Management folder I created a template called Google Analytics. This tag has a few attributes that are used as parameters for the tag.
If I create a content item that inherits this template, I see the attribute fields.
What I need to know, is where -- as best practice -- would I would write my code that generates the script tag. I looked in the SiteCore source project, and do not see any folders for Template code.
UPDATE: Based on the Feedback and this Url: http://andyuzick.arke.com/2013/02/as-web-marketers-great-deal-of-our.html , I have implemented a new class library with the following:
Settings.cs
namespace TagManagement
{
public class Settings
{
public const string DEFAULT_GLOBAL_TAG_FOLDER = "/sitecore/content/Global/TagManagement";
public static string GlobalTagFolder
{
get
{
return Sitecore.Configuration.Settings.GetSetting("TagManagement.GlobalTagFolder", DEFAULT_GLOBAL_TAG_FOLDER);
}
}
}
}
WebControl.cs
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using System;
using System.Text;
using System.Web.UI;
namespace TagManagement
{
public class TagManagmentControl: Sitecore.Web.UI.WebControl
{
System.Web.UI.WebControls.Literal container;
public string TagItem { get; set; }
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
container = new System.Web.UI.WebControls.Literal();
}
protected override void CreateChildControls()
{
Assert.IsNotNullOrEmpty(TagItem, "tag item");
Item item = Sitecore.Context.Database.GetItem(TagItem);
StringBuilder tagToOutput = new StringBuilder();
string templateName = item.TemplateName;
switch (templateName)
{
case "Google Analytics":
tagToOutput.AppendLine("<script>");
tagToOutput.AppendLine(" (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
tagToOutput.AppendLine(" (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
tagToOutput.AppendLine(" m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
tagToOutput.AppendLine(" })(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
tagToOutput.AppendLine(" ga('create', '" + item.Fields["Tracking ID"].Value + "', '" + item.Fields["Domain"].Value + "');");
if (item.Fields["Enable Demographics and Interest Reports"].Value == "1")
{
tagToOutput.AppendLine(" ga('require', 'displayfeatures');");
}
tagToOutput.AppendLine(" ga('send', 'pageview');");
tagToOutput.AppendLine("</script>");
tagToOutput.AppendLine();
break;
case "HTML Tracking Tag":
tagToOutput.AppendLine(item.Fields["Markup"].Value);
break;
}
container.Text = tagToOutput.ToString();
}
protected override void DoRender(HtmlTextWriter output)
{
EnsureChildControls();
container.RenderControl(output);
}
protected override string GetCachingID()
{
return this.GetType().FullName;
}
}
}
PipelineProcessor.cs
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Layouts;
using Sitecore.Pipelines.InsertRenderings;
namespace TagManagement
{
public class InsertTags
{
public void Process(InsertRenderingsArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (Sitecore.Context.Site.Name == "shell")
{
return;
}
Item globalTagFolder = Sitecore.Context.Database.GetItem(Settings.GlobalTagFolder);
Profiler.StartOperation("Tag Management: Adding Tags...");
foreach (Item globalTagItem in globalTagFolder.Children)
{
TagManagement.TagManagmentControl control = new TagManagement.TagManagmentControl();
if (control != null)
{
control.TagItem = globalTagItem.ID.ToGuid().ToString();
control.Cacheable = true;
control.VaryByData = true;
RenderingReference reference = new RenderingReference(control);
reference.AddToFormIfUnused = true;
args.Renderings.Add(reference);
Tracer.Info(string.Concat("Tag Management: Added: '", globalTagItem.Name, "'"));
}
}
Profiler.EndOperation();
}
}
}
I would appreciate any constructive feedback from the SiteCore experts in the room!