How to Install Search Analytics For Sitecore

Go to https://github.com/deanobrien/search-analytics-for-sitecore and download the relevant ZIP file for your version of Sitecore (i.e. Sitecore 9 Compatible – Search Analytics for Sitecore Module and DacPac.zip) and unpack the contents.

Within you will find:

  1. SearchAnalytics.dacpac (database)
  2. Search Analytics for Sitecore Module-1.zip (sitecore package)

Connect the database

  1. Access your database server via SSMS
  2. Right click databases node => Deploy Data Tier Application
  3. Follow the on screen instructions and select the DACPAC file from above
  4. This will create a database called SearchAnalytics
  5. Create a user with access to read/write
  6. Add a new connection string to you your Sitecore instance called SearchAnalytics
deploy data teir

Install Sitecore Package

  1. Go to desktop
  2. Development tools
  3. Installation Wizard
  4. Upload Package
  5. Select package from above
  6. Install

Update your code to register searches and rank order

In a typical Sitecore solution, the search component will likely be a controller rendering which links to an action (similar to below).

The example below assumes you connect to your own SearchService to get standard search results. You should then concatenate the IDs of the top 20 (or less) results. Then pass that, together with current item and search term to the ITrackSearch service (which is registered as a dependency when you install the module).

Please see complete example Search Controller on GitHub

public ActionResult SiteSearchResults()
{
    var model = new RenderingModel();

    //
    // This is not intended to be a used as a component. 
    // Example of how you might integrate with Track() given a result from a similar SearchService
    //

    _term = Request["q"];
    var searchResults = _SearchService.SiteSearch(_term);
    var top20Items = searchResults.Take(20).Select(x => x.ItemId.ToString());

    if (!string.IsNullOrWhiteSpace(_term))
    {
        _trackSearch.Track(Context.Item, _term, string.Join("|", top20Items));
    }

    //
    // Add the search results to a custom view model and pass to view
    //

    return View(model);
}

Update the config file

In order for the module to detect a ‘Click Through’ from a search results page, it needs to be able to recognise one. The configuration file sitecore-patch-SearchAnalytics.config has a setting called SearchPageUrlPartial which defaults to “?q=“. If your search listings page does not contain that sequence of characters, then you should update the setting to one that matches your setup.

i.e. The URL for the page may look like this <yoursite.com>/SearchResults/?q=searchterm

Note: The module currently assumes the search query will be parameter ‘q ‘. This is hard coded at the moment, but will be moved to a similar setting as above in future. See line 46 here.

Update web.config file

The page that displays the search report makes calls to a small number of external CDN sites for style / font / script. In order to allow access to these, you need to update the content security setting in your web.config file. To do this:

  1. Look for the line beginning: <add name=”Content-Security-Policy”
  2. Find style-src and add https://maxcdn.bootstrapcdn.com immediatey after it (with space either side).
  3. Find font-src and add https://maxcdn.bootstrapcdn.com immediatey after it (with space either side).
  4. Find script-src and add https://cdn.jsdelivr.net immediatey after it (with space either side).

Note: If any of the above are not present (i.e. script-src then it will fall back to the default-src so you need to add the url immediately after that – or add the attribute)

Test that it works

After completing the steps above you should be able to navigate your site, make a search, select a result and have the data captured.

Note: The module currently uses events to register the searches, then hooks into the convertToXConnectEvent pipeline to send the information to SQL at the end of the session. Due to this, the module will only work correctly if you have a fully working XConnect solution, and you access the front end on a Standalone or ContentDelivery instance (xconnect events are not processed on ContentManagement only instances). In the future I will add a XMonly setting that send data straight to SQL.


Important: The stored procedure PopulateTempAdjustedRank is responsible for processing ranking data. In order to get accurate data, you need to schedule this to run frequently. I aim to create a scheduled task that will call this every X hours at some point in the future…. (just did not get round to adding it just yet).


Summary

Hopefully if you completed the steps above, you should be able to access the search reports in the backend of Sitecore (as described on this page Search Analytics for Sitecore XM/XP. If you manage to get this working or if you have any issues, I would appreciate your leaving a comment below.

Good Luck!

Leave a Reply

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