1

I am struggling with accomplishing the following scenario: I need to create a custom property for Application Insights, containing an identifier for the user (which is read from the data store at login time).

Went through several articles on the matter, but it does not seem to work.

I tried the following so far:

  1. Implemented a custom telemetry initializer and loaded it in a Sitecore pipeline. The problem here is that I can't access the session object, as it's always NULL. After some research I realized that it is triggered before the session is even created, so then I went to the second option.
  2. Tried to load the telemetry initializer in a HttpRequestProcessor, I was able to access the session, but the Initialize method is never triggered afterwards. Can't say I understand what is happening. I added the initializer as follows TelemetryConfiguration.Active.TelemetryInitializers.Add(new UserIdTelemetryInitializer([injected_service])), in the constructor of the processor, so it is only triggered once.

Does anyone have any thoughts on what exactly I am doing wrong? If you have a code example that works for you in a similar case, I would be grateful.

Let me know if you need additional information (might have been too brief about it).

EDIT

I am registering the pipeline from option 2 in a patch file as follows:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
    <pipelines>
        <httpRequestProcessed>
            <processor type="[namespace].Pipelines.ApplicationInsights.UserIdTelemetryProcessor, [namespace]" resolve="true" />
        </httpRequestProcessed>
    </pipelines>
</sitecore>
1

The Application Insights telemetry initializers are triggered before the Session_OnStart event occurs, so you need to register your custom telemetry initializer in the Session_Start method in the Global.asax.cs of your application.

Custom Telemetry Initializer

You probably already created this, but I am sharing here an example. Mine is inheriting one of the existing OOTB telemetry initializers and overrides the OnInitializeTelemetry method. In this way, you get easily access to the HttpContext object:

public class SitecoreSessionInitializer : SessionTelemetryInitializer
{
        /// <summary>Implements initialization logic.</summary>
        /// <param name="platformContext">Http context.</param>
        /// <param name="requestTelemetry">Request telemetry object associated with the current request.</param>
        /// <param name="telemetry">Telemetry item to initialize.</param>
        protected override void OnInitializeTelemetry(
            HttpContext platformContext,
            RequestTelemetry requestTelemetry,
            ITelemetry telemetry)
        {
            if (telemetry == null)
                throw new ArgumentNullException(nameof(telemetry));
            if (requestTelemetry == null)
                throw new ArgumentNullException(nameof(requestTelemetry));
            if (platformContext == null)
                throw new ArgumentNullException(nameof(platformContext));
            
            // YOUR CUSTOM LOGIC HERE
            if (platformContext.Session != null)
                requestTelemetry.Properties["CustomUserId"] = platformContext.Session["UserId"].ToString();
        }
}

In this custom telemetry initializer I am writing the value of the UserId session object in a custom dimension called CustomUserId.

Custom Telemetry Initializer Registration in Global.asax.cs

public class Global : Sitecore.Web.Application
{
        protected void Session_Start(Object sender, EventArgs e)
        {
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new SitecoreSessionInitializer());
        }
}
2
  • I know it is not a good practice to modify the Global.asax.cs file in a Sitecore installation, but just to use pipelines instead. That is why I registered a httpRequestProcessed pipeline, but still had the issues presented above. Either way, I will also try using the OOTB initializer you mentioned and be back with a response. Thanks. Jul 17 at 16:08
  • Yes, I couldn't find a pipeline that was working either. This approach worked for me instead. Jul 17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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