How to get Search Analytics Reports programmatically in SharePoint 2013

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!

Advertisements
13 comments
  1. Leo said:

    Hy,
    I am currently trying to get the data for the “Abondoned Queries” Report. But I´m not lucky with it. GetSearchReport seems to deliver ONLY the top Queries (with querytext and querycount).

    Do you have any suggestions how to get the abondoned queries with their percentage?

    thx

    • Radu Tut said:

      Hi Leo,

      have you tried using different values for parameter eventtype?
      Unfortunately, there isn’t much documentation regarding search reports out there. My suggestion would be to check the SearchServiceApplication class, different methods it exposes (like GetSearchReport and GetRollupAnalyticsItemData) with different parameter calls. You can try it in Powershell quite easily and fast. Then you could compare the different results you get in Powershell with what you get from SharePoint User Interface (excel files).

      If you want to have a deeper look on how they work, you can have a look in SQL Server, on the Search Analytics database. These methods, all they do is to call stored procedures from SQL server. Maybe you can figure out what parameters you need to get the abandoned queries report.
      Also please consider that for top queries for example, only terms that have been searched at least three times are taken into consideration. Such a limit may exist on abandoned queries as well.

      I hope this helps. Let me know how it goes.

      Best Regards,

      radu.

    • Hello,

      I haven’t tried to obtain this report .. you could have a look at the methods available on the SearchServiceApplication class, and you can also have a look (advanced) in SQL Server, at the Analytics DB and the stored procedures that are called by the SP API.

  2. harsh said:

    Error 4 The type ‘Microsoft.Office.Server.Search.Analytics.AnalyticsItemData’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.Office.Server.Search.Applications, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’.

    I get this error msg

    • You must reference the assembly from your visual studio solution

  3. harsh said:

    Hey radu,
    I get this error message
    Error 3 The type ‘Microsoft.Office.Server.Search.Analytics.AnalyticsItemData’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.Office.Server.Search.Applications, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’.

    • You must reference the assembly from your visual studio solution

  4. suchithra j said:

    Hi Radu,

    This information is very useful.

    GetRollupAnalyticsItemData has the following parameters:
    eventType – int : type of event, e.g. 1 for Site Usage Reports

    i wanted to know is der any way that i can get page views in sp2013,
    will GetSearchReport report gives page view ?

    • Hello,

      I haven’t tried to obtain this report .. you could have a look at the methods available on the SearchServiceApplication class, and you can also have a look (advanced) in SQL Server, at the Analytics DB and the stored procedures that are called by the SP API. If it is possible – that would be the way to find out how.

  5. Important to note that the DateTime value you use should be UTC. If not, you will, at some time during the day (depending on your offset) get a “Specified argument was out of the range of valid values” error when trying to get the furthest back data (example: -14 days for Day) if you use Local time.

  6. Here are the event IDs.

    Search Reports Events

    Invalid = 0
    First = 1
    TopQuery = 1
    FailedQuery = 2
    NoResultsQuery = 3
    BestBetUsage = 4
    RuleFired = 5
    RuleFiredPromotedResultClicked = 6
    Last = 7
    RuleFiredResultClicked = 7

    Rollup Data Events

    Invalid = 0
    First = 1
    View = 1
    RecommendationView = 2
    RecommendationClick = 3
    Last = 4
    Search = 4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: