Search Analytics: Creating the Sitecore Module

Search Analytics dashboard

In this post, we will look at how we surface data into a useful format and create a search report. Then how to allow users to access it from various places within the Sitecore user interface, such as the launchpad, context menu and the content editor.

Routing to our Search Report

The search report is created using a standard MVC approach. With a SearchReportController, an action that optionally accepts the parameters id, searchTerm and display. Then an action returns a specific view:

(/sitecore/shell/client/Applications/SearchReport/Index.cshtml

Requests are directed to our controller action by mapping a custom route in RegisterCustomRoute.cs like this:

RouteTable.Routes.MapRoute("SearchReport", "sitecore/shell/sitecore/client/applications/searchreport/", new { controller = "SearchReport", action = "SearchReport" });

The processor that deals with these routes is added to the initialize pipeline, via our sitecore patch file (sitecore-patch-SearchAnalytics.config).

With these changes, anyone who accesses the link will then be shown our search report.

<our-domain>/sitecore/shell/sitecore/client/applications/searchreport/

If an itemId is supplied, the report will be focused on information about that specific page. If omitted a general report will be presented. Using this approach, we can call the same report from the launchpad, context menu and also the top ribbon. The relevant information will be presented depending on the parameters provided.

Note: the assets for the report are housed in a folder called SearchReport in this location: /sitecore/shell/sitecore/client/applications/ together with other internal applications

Adding a Sitecore Command

We need to create a custom command to link the context menu and the ribbon buttons to our report.

To do this, we create a new class and inherit from Sitecore.Shell.Framework.Commands.command. We override the Execute method and access the context to get the ID of the item being viewed or clicked. We retrieve the search report route from settings and combine it with the ID to form the URL. This is then opened in a new modal window, using Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog().

The last thing we need to then do is register the command in config (using the patch file mentioned previously):

<commands>
    <command name="searchreport:item" type="DeanOBrien.Feature.SearchAnalytics.Commands.SearchReportCommand,DeanOBrien.Feature.SearchAnalytics"/>
</commands>

We can then call the command from a variety of places within the Sitecore back end to access the report.

Hooking it up to the Sitecore UI

When ever you need to add or edit the functionality of the Sitecore user interface, you need to do this via the core database. You can access this via Sitecore Desktop => Select Core in the bottom right hand corner.

Context Menu

The first thing we want to add is an item to the context menu, so that if a user right clicks an item in the content editor, they will be given an option to select the search report.

To do this, add a new Menu Item called SearchReport in this location:

 /sitecore/content/Applications/Content Editor/Context Menues/Default

Update display name and icon accordingly, then in the Message field add searchreport:item(id=$Target) which effectively instructs Sitecore to call our custom command and pass the selected item as the context item.

Top ribbon

In order to add a new button in the ribbon, we need to first create a new ‘chunk’. Do this by adding a Chunk in this location:

/sitecore/content/Applications/Content Editor/Ribbons/Chunks/

together with an appropriate Header (this is the text at bottom of chunk) and ID.

Then add a ‘Large Menu Combo Button’ as a child of the above chunk and give it appropriate icon, header (text directly below icon) and tooltip. The Click field connects the button to our command ‘searchreport:item(id=$Target)’ .

Finally, you can add the new chunk to an existing ‘Strip’. In our example, we add to the Review strip by adding a Reference item to this location and point back to the chunk we created earlier.

/sitecore/content/Applications/Content Editor/Ribbons/Strips/Review/

Launchpad Button

The last thing we need to connect is the launchpad button. Unlike the two links above, this button will link through to a general report for all searches, so there is no need to pass any parameters (to begin). To create this add a ‘LaunchPad-Button’ item to this location and give it appropriate text and icon. The ‘Link’ field points to the route we configured earlier.

/sitecore/client/Applications/Launchpad/PageSettings/Buttons/Tools/

The Search Report UI

The report itself uses some HTML/CSS from existing Sitecore pages to contain the report and to bring in a similar look and feel. Then some Bootstrap elements to layout the page and provide basic functionality.

After the page initially loads, a variety of API calls are made to bring back the relevant information from the database. The routes to these APIs are configured in the same way as the main report, pointing to actions in the same SearchReportController. The data from the API calls is mapped to the relevant charts using Chart.js javascript plugin, or to search results using a combination of JQuery and Mustache.js HTML templating.

The API calls map through the SqlSearchStore, which in turn call a collection of stored procedure within the SearchAnalytics database. By enlarge, each procedure groups data from one of the single tables we used when collecting the data. However, two procedures (GetRankingSummaryForTerm & GetSearchTermsByEngagement) take data from a temporary table TempAdjustedRank.

This table is populated using the PopulateTempAdjustedRank procedure, which needs to be run periodically to ensure the data is up-to-date. This procedure groups information from the ClickThroughs table, to find which items have the highest engagement for each term. This data is then ranked for each term and compared against the original ranking data, to see how items rankings based on engagement compare (leading to the red and green up/down arrows).

Summary

In this post, we looked at how the data we collected and stored in the database, is resurfaced and presented back to the user as a Search Report. We looked at routing custom paths to MVC controller actions, creating custom Sitecore commands and hooking up these different bits of functionality to buttons within the Sitecore user interface via the core database.

This post is the last in a series that has looked at the Search Analytics Module for Sitecore.

Leave a Reply

Your email address will not be published. Required fields are marked *