0

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:

  1. Current page's template ID OR the Sitecore Context Item by which the template ID can be derived
  2. Tracker
  3. 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.

1 Answer 1

0

The appropriate pipeline in this case appears to be ProcessItem:

using Sitecore.Analytics;
using Sitecore;
using System;
using Sitecore.Analytics.Pipelines.ProcessItem;
using Sitecore.Diagnostics;
using Sitecore.Analytics.Data;
using System.Collections.Generic;

namespace Client.Project.Pipelines.ProcessItem
{
    public class RegisterCustomPageEvents : ProcessItemProcessor
    {
        public override void Process(ProcessItemArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));

            foreach (TrackingField trackingParameter in (IEnumerable<TrackingField>)args.TrackingParameters)
                TrackingFieldProcessor.ProcessEvents(args.Interaction, trackingParameter);

            if (!Tracker.Enabled || Tracker.Current == null || !Tracker.Current.IsActive)
            {
                return;
            }

            FireCustomEvent(args);
        }

        private void FireCustomEvent(ProcessItemArgs args)
        {
            var myPageTemplateId = "{xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx}";

            if (args.Item == null || Context.Item.TemplateID.ToString() != myPageTemplateId)
            {
                return;
            }

            var ev = Tracker.MarketingDefinitions.PageEvents[Guid.Parse("{xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxx}")];

            if (ev != null)
            {
                var pageData = new Sitecore.Analytics.Data.PageEventData(ev.Alias, ev.Id);

                pageData.Text = "My custom event";

                var fullUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

                pageData.CustomValues.Add("Url", fullUrl);
                pageData.CustomValues.Add("PageTemplateId", args.Item.TemplateID.ToString());
                pageData.CustomValues.Add("ItemVersion", args.Item.Version.Number);

                pageData.ItemId = args.Item.ID.ToGuid();

                try
                {
                    Tracker.Current.CurrentPage.Register(pageData);
                }
                catch (Exception ex)
                {
                    Log.Error("Unable to fire custom event", ex, this);
                }
            }
        }
    }
}

Patch config:

<sitecore>
    <pipelines>
        <processItem>
            <processor type="Client.Project.Pipelines.ProcessItem.RegisterCustomPageEvents, Client.Project" />
        </processItem>
    </pipelines>
</sitecore>

4
  • I have the same requirement, but after the register page event, I am not able to see it in the ContactFacets table. Commented Oct 23, 2022 at 18:36
  • What are you expecting to see in the ContactFacets table? Perhaps this blog post will help? sitecore.marcelgruber.ca/posts/… Commented Oct 25, 2022 at 14:27
  • Thanks I will check Commented Oct 26, 2022 at 4:22
  • @MarcelGruber I have read your blog post regarding registering custom pageview event. In this siuation, does it register the custom event with default pageViewEvent as parent? or instead of default? Im really interested in preserving the original data (i.e. duration) but adding to that info about previous page. All info that info needs to be in one event for retrieval Commented Apr 27, 2023 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.