Archive

Search Analytics

In SharePoint 2010 it was possible to obtain reports from web analytics programmatically using Microsoft.Office.Server.WebAnalytics.Reporting.AnalyticsReportFunction class.However, in SharePoint 2013 the Web Analytics Service Application was merged into the Search Service Application. It seems now that the old assemblies used for this (Microsoft.Office.Server.WebAnalytics.dll and Microsoft.Office.Server.WebAnalytics.UI.dll) are not available any more.

In SP 2013 UI, the reports are available at these locations, as “Usage Reports”

  • CA->Application Management->Manage Service Applications->Search Service Application->Usage Reports
  • Site Settings -> Site Administarion ->Popularity Trends -> View Usage Reports

These reports can now be obtained programmatically using SearchServiceApplication class, from Microsoft.Office.Server.Search.dll. There are two methods available for these:

Although there are more reports that can be obtained from the SSA, i will only explain these two options.

Search analytics data is kept in a SQL database – AnalyticsReportingStore. GetSearchReport is based on SQL stored procedure ar_procGetTopSearchReport and GetRollupAnalyticsItemData is based on ar_procGetAnalyticsItemData.

GetSearchReport has the following parameters:

  • reportType – int : type of report, e.g. 1 is for top search terms (top queries)
  • tenantId – guid : id of the SharePoint tenant. I haven’t been able to figure this one out yet, but without a specific setting for this in the SharePoint farm installation, it would be an Empty Guid.
  • siteId – guid : Site Collection id. Empty guid for all site collections.
  • reportDate – DateTime : The date of the report.
  • bDaily – bool : true brings the result for that day, false for the entire month
  • maxRows – uint : maximum number of results.

Here is a sample, which brings the most searched terms for the Search Service Application in a specific month.


SPSecurity.RunWithElevatedPrivileges(delegate
 {

// You can use SPContext.Current.Site.ID if you have HttpContext

using (var site = new SPSite(siteId))
     {
        var context = SPServiceContext.GetContext(site);
        var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
        var topQueries = searchProxy.GetSearchReport(1, Guid.Empty, Guid.Empty, startDate, false, maxRows);
        foreach (var query in topQueries)
          {
               //process top search term
           }
     }
  });

GetRollupAnalyticsItemData has the following parameters:

  • eventType – int : type of event, e.g. 1 for Site Usage Reports
  • tenantId – guid : same as for GetSearchReport
  • siteId – guid : site collection Id
  • scopeId – guid : sub-site id, empty guid for the entire site collection

The following code retrieves the daily and monthly usage reports of Hits and Users count:

   

SPSecurity.RunWithElevatedPrivileges(delegate
 {
    // You can use SPContext.Current.Site.ID if you have HttpContext
    using (var site = new SPSite(siteId))
       {
         var context = SPServiceContext.GetContext(site);
         var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as       SearchServiceApplicationProxy;
         var usageData= searchProxy.GetRollupAnalyticsItemData(1,Guid.Empty,site.ID,Guid.Empty);

usageData.GetHitCountForDay(date);
         usageData.GetHitCountForMonth(date);
        }
 });

Stay tuned for other posts on Search Analytics!