Tracking a Page Event from ASHX Handler

1.3k views Asked by At

Im currently trying to track a PageEvent within a ASHX Handler. My code basically looks like this:

public class GetProductPdf : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (!Tracker.IsActive)
        { 
            Tracker.Initialize();
            Tracker.StartTracking();
        }

        //Track PageEvent here...
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

The Tracker is always inactive and Tracker.Current == null. On method call "Tracker.StartTracking();" the following Exception is thrown:

[InvalidOperationException: Tracker.Current is not initialized]
Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking.Process(PipelineArgs args) +317
(Object , Object[] ) +83
Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +445
 Project.Web.Handler.PdfCreation.GetProductPdf.ProcessRequest(HttpContext context) in d:\Project\Website\Handler\PdfCreation\GetProductPdf.ashx.cs:69
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +913
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

I tried all possible solutions suggested here.

When doing the same in a mvc controller the Tracker is active and Tracker.Current != null.

Does anyone has an idea, what could cause this or are there any other suggestions for a solution?

Thanks in advance.

1

There are 1 answers

3
Jonathan Robbins On

I am not certain that your Ashx Handler can be executed within the necessary Sitecore Context so that Tacker.Current will not be valid nor can be started via Tracker.StartTracking(). Someone might be able to confirm but I have another solution you can try which works for me.

As nice as it would be for the Ashx Handler to register the Event for you, instead you can fire a JavaScript function on the link to the file. So that when the link is clicked the JavaScript makes a web request to a MVC Controller and the controller registers the event for you.

I have implemented this myself using WebApi Controllers. Data Attributes were on the a tag, JavaScript posted those attributes to the controller, the controller used those attributes to determine which Event to register and the description to use on the Event.

<asp:HyperLink runat="server" data-goalid="{08030449-A811-428B-95F0-59FCD42B8DEB}" data-goaldescription="Product 0112 brochure">
[System.Web.Mvc.HttpPost]
public JsonResult RegisterGoal(string goalId, string goalDescription)
{
    Item eventItem = Sitecore.Context.Database.GetItem(goalId);
    var goal = new PageEventItem(eventItem);

    var eventData = Tracker.Current.PreviousPage.Register(goal);
    eventData.Data = goal["Description"] + " " + goalDescription;
    Tracker.Current.Interaction.AcceptModifications();

    return Json(new PageEventRequestResult()
    {
        Success = true,
        Message = "Successfully registered goal",
    });
}

It works really well. The only downside is having to add it to the various links that lead to the files you want to track.

I wrote a blog about tracking various interactions on a site and registering Sitecore Events / Goals you might want to look at, scroll down to the 'Storing custom data in xDB' section.