Tracking
Introduction
Tracking is the process of storing statistical data about queries that visitors to the site have searched for, as well as the hits that they have clicked on.
The purpose of tracking is to help the content administrator to gain a deeper insight into the efficiency of the search functionality and to identify areas of improvement and optimization to better serve vistors with relevant content.
Automatic tracking
The recommended and standard way to enable tracking on your site is to call Track() on the search query.
Example
SearchClient.Instance.UnifiedSearchFor(searchQuery).Track().GetResult()
Track() ensures that the required tracking information is added to the URLs of the search hits. JavaScript is automatically injected in the footer of the page that handles the tracking of the hits.
When using Unified Search, Track() enables tracking of both the query and the hits. When using Track() on a non-Unified Search, it only enables tracking of the query. Refer to custom tracking below for information on how to achieve hit tracking for a non-Unified Search.
Note: The client side scripts used for tracking depends on having set the EPiServer.Framework.Web.RenderingTags in the page templates:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<%@ Import Namespace="EPiServer.Framework.Web" %>
<%@ Import Namespace="EPiServer.Framework.Web.Mvc.Html" %>
<html>
<head runat="server">
...
<%= Html.RequiredClientResources(RenderingTags.Header) %>
</head>
<body>
...
<%= Html.RequiredClientResources(RenderingTags.Footer)%>
</body>
</html>
Custom tracking
You can utilise the StatisticsClient to implement a custom tracking mechanism. The StatisticsClient offers two methods: TrackQuery() and TrackHit().
Access the StatisticsClient as follows:
SearchClient.Instance.Statistics().TrackQuery(...)
SearchClient.Instance.Statistics().TrackHit(...)
TrackQuery(), as the name suggests, is used to track the user query. It returns a TrackQueryResult with the TrackId.
The TrackId returned from TrackQuery() should be used when you want to track hits through the existing Javascript. The existing Javascript referred to here is explained below under automatic tracking.
You would typically implement this scenario when you are using a non-Unified Search, and have to manually enable tracking of hits.
This requires you to supply the required tracking information as querystring parameters in the hit URL.
The complete list of tracking querystring parameters is as follows:
- _t_id: TrackId, returned from client.Statistics().TrackQuery(...)
- _t_q: The search query string
- _t_tags: Tags for categorization of the collected data. Normally contains site and language tags.
- _t_ip: Client IP address
- _t_hit.id: The expected format for a hit id (hitId argument to StatisticsClient.TrackHit) is the type name used in the index and the ID of the document in the index separated by a slash. Example: "EPiServer_Templates_Alloy_Models_Pages_ProductPage/_cb1b190b-ec66-4426-83fb24546e24136c_en"
When you have the SearchHit<T> objects (from GetResult) available, you can simply combine the SearchHit.Type and SearchHit.Id properties for the appropriate search hit.
If you instead need to construct this value based on only the object that was indexed, it could be done as follows: client.Conventions.TypeNameConvention.GetTypeName(myObj.GetType()) + "/" + client.Conventions.IdConvention.GetId(myObj) - _t_hit.pos: The ordinal position the hit was in.
TrackHit() is used to track a search hit. Stated in other words, it is a way to achieve click tracking. It takes a query and hit ID as parameters. The format of the hit id parameter is the same as described for the _t_hit.id querystring parameter above.
Last updated: Dec 15, 2014