I have a case where I want to fire a custom page event but only for a specific page template. This means that whichever pipeline I fire the event from, I need access to:
- Current page's template ID OR the Sitecore Context Item by which the template ID can be derived
- Tracker
- Full URL of the request
At a high level, this is the code I am looking to fire:
using Sitecore;
using Sitecore.Pipelines.HttpRequest;
using System;
using Sitecore.Analytics;
namespace Client.Project.Pipelines.httpRequestBegin
{
public class FirePageEvents : HttpRequestProcessor
{
public override void Process(HttpRequestArgs args)
{
if (Context.Item == null || Context.Item.TemplateID.ToString() != myPageTemplateID)
{
return;
}
if (!Tracker.Enabled || Tracker.Current == null || !Tracker.Current.IsActive)
{
return;
}
var ev = Tracker.MarketingDefinitions.PageEvents[Guid.Parse("{xxxxxxxxxxxxxxxx}")];
if (ev == null)
{
return;
}
var pageData = new Sitecore.Analytics.Data.PageEventData(ev.Alias, ev.Id);
pageData.Text = "Custom event which fires on page load";
pageData.ItemId = Context.Item.ID.ToGuid();
pageData.CustomValues.Add("PageTemplateId", Context.Item.TemplateID.ToString());
pageData.CustomValues.Add("Url", args.Request.Url.AbsoluteUri);
try
{
Tracker.Current.CurrentPage.Register(pageData);
}
catch(Exception ex)
{
// Handle
}
}
}
}
From which pipeline is the best place to trigger the custom page view event? This would certainly be project specific, but this requirement seems general enough to be useful to the community.