HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Tracking data interceptor

This topic describes the Tracking Data Interceptor and how to extend TrackingDataInterceptor for a site.

ITrackingDataInterceptor is an interceptor that can modify tracking data before sending to Profile Store.

Tracking data workflow

  1. A user accesses the website.
  2. Create user tracking data.
  3. Data is accumulated through TrackingDataInterceptor  and ordered by SortOrder. The tracking data may be modified in the TrackingDataInterceptor.
  4. Tracking data is sent to the Tracking API.
1353

Customize the interceptor

Prerequisites

See Using Tracking.Core API with Profile Store.

Customize the tracking data interceptor

  1. Implement ITrackingDataInterceptor to customize the tracking data interceptor. ITrackingDataInterceptor instances are executed following the SortOrder property.

    The following example uses a QuickSilver site that tracks data values (Browser, Market, Currency, and so on).

[ServiceConfiguration(ServiceType = typeof(ITrackingDataInterceptor), Lifecycle = ServiceInstanceScope.Singleton)]
public class CustomTrackingDataInterceptor : ITrackingDataInterceptor
  {
    private readonly ICurrentMarket _currentMarket;
    private readonly ICurrencyService _currencyService;
    public int SortOrder => 300;
    public CustomTrackingDataInterceptor(ICurrentMarket currentMarket, ICurrencyService currencyService)
      {
        _currentMarket = currentMarket;
        _currencyService = currencyService;
      }
    public void Intercept(TrackingData trackingData)
      {
        if (trackingData == null || trackingData.Payload == null)
          {
            return;
          }
        if (trackingData.User == null)
          {
            trackingData.User = new UserData();
          }
        if (trackingData.User.Info == null)
          {
              trackingData.User.Info = new Dictionary<string, string>();
          }
        // Add market information to tracking data
        TryAddUserInformation(trackingData.User.Info, "Market", _currentMarket.GetCurrentMarket().MarketName);
        // Add currency information to tracking data
        TryAddUserInformation(trackingData.User.Info, "Currency", _currencyService.GetCurrentCurrency().CurrencyCode);
      }
      private void TryAddUserInformation(IDictionary<string, string> UserInfo, string key, string value)
        {
          if (!UserInfo.ContainsKey(key))
            {
              UserInfo.Add(key, value);
            }
        }
  }
  1. Compile the site and browse the site on a browser.
  2. Look into the tracking value; you have the browser, market, and currency information in it. Using Profile APIs to query the tracking data, you get data similar to the following. 
{
  "items" : [
              {
                "TrackId"       : null,
                "DeviceId"      : "4247d93c-5279-4014-a1f4-88a5cda03a9b",
                "EventType"     : "home",
                "EventTime"     : "2018-09-24T03:58:43.5035992Z",
                "Value"         : "Visited the start page.",
                "Scope"         : "cdc1ffdc-32ec-46b2-8d38-0f16965e2a02",
                "CountryCode"   : "Localhost",
                "PageUri"       : "http://myQuicksilver/",
                "PageTitle"     : "Start",
                "RemoteAddress" : "::1",
                "Payload"       : {
                                    "channel"     : "web",
                                    "type"        : "home",
                                    "ip"          : "::1",
                                    "session"     : "new",
                                    "cuid"        : "new",
                                    "site"        : "ChangeThis",
                                    "clientToken" : "ChangeThis",
                                    "lang"        : "en",
                                    "currentURI"  : "http://myQuicksilver/",
                                    "previousURI" : "",
                                    "userAgent"   : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
                                  },
                "User"          : {
                                    "Name"  : null,
                                    "Email" : null,
                                    "Info"  : {
                                                "Market"   : "Sweden"
                                                "Currency" : "SEK"
                                              }
                                  }
              }
            ],
  "total" : 1,
  "count" : 1
}

What’s Next