How to get analytics reports programmatically in SharePoint 2013?
In SharePoint 2010 it was possible to obtain data from web analytics programmatically using Microsoft.Office.Server.WebAnalytics.Reporting.AnalyticsReportFunction class.
How can you get search reports programmatically in SharePoint 2013? Are there new assemblies or classes that can be used to obtain them?
In my scenario, i am mostly interested in the most searched terms at web application or service application level.
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.
They are available in the user interface as Excel reports, in CA->Application Management->Manage Service Applications->Search Service Application->Usage Reports , but I am interested to obtain these reports programmatically.
Is this scenario still supported in SP 2013?
January 15th, 2013 3:20pm
Hi Efren,
I have just tried it out and it works fine :).
Thank you very much, that was exactly what i was looking for.
For others looking for the same issue, here is a PS script to test it out.
$searchApp = Get-SPEnterpriseSearchServiceApplication
$searchApp.GetSearchReport(1,[System.Guid]::Empty,[System.Guid]::Empty,[System.DateTime]::Now,$false,1000)
This will show Top Queries for current month for the entire Search Service Application.
-
Proposed as answer by
James 29
Friday, January 25, 2013 12:56 PM
January 20th, 2013 4:57pm
How can it possible using Object model?
Any sample available?
January 21st, 2013 9:11am
Hi Jason,
Yes, sure.
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
}
}
});
Please let me know if this was fine for you.
For the two Guid parameters it would require the tenantId - I am not sure what this is, and the siteId.
Passing Guid.Empty in these parameters retrieves all top search terms.
Does anyone know what should be passed for the parameter Guid tenantId?
-
Proposed as answer by
Jayakumar Jayabal
Friday, January 25, 2013 9:37 AM
January 21st, 2013 3:33pm
Yes MrRaduTut, it works fine. It shows search terms only.
I need to get Usage Reports which is maintains under Site Settings -> Site Administarion ->Popularity Trends -> View Usage Reports.
Is this possible using above code? It contains daily and monthly reports of Hits and users count.

January 22nd, 2013 6:15am
In order to get the Usage Reports for a site collection, you can use this PS Script:
$searchApp = Get-SPEnterpriseSearchServiceApplication
$site = Get-SPSite "{SiteUrl}"
$result = $searchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$site.ID,[System.Guid]::Empty)
$result
#for a specific date or for a specific month
$date = Get-Date "2013-01-18"
$result.GetHitCountForDay($date)
$result.GetHitCountForMonth($date)
Or using object model
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);
// process it
}
});
-
Proposed as answer by
Jayakumar Jayabal
Wednesday, January 23, 2013 9:28 AM
January 22nd, 2013 2:51pm
You can use the scopeId parameter, and pass the subsite id .
$searchApp = Get-SPEnterpriseSearchServiceApplication
$site = Get-SPSite
http://server/sitecollection/
$web = Get-SPWeb
http://server/sitecollection/MySite
$usageData = $searchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$site.ID,$web.ID)
-
Proposed as answer by
Jayakumar Jayabal
Friday, January 25, 2013 9:38 AM
January 23rd, 2013 4:12pm
Awesome :)
Thank you for your reply. It works good.
One Query:
In SharePoint 2010 web analytical reports, it can be possible to know
(i) Top Pages (page URLs)
(ii) Top Visitors (user name)
But in SharePoint 2013, i cn't find those things.
Is this be able to get above informations in SharePoint 2013 using object model for the site?


-
Edited by
Jayakumar Jayabal
Thursday, January 24, 2013 12:39 PM
-
Proposed as answer by
James 29
Friday, January 25, 2013 12:56 PM
January 24th, 2013 12:03pm
How to get the usage report for the specific date range? Do you have any idea?
June 6th, 2013 3:04pm
I have a client requirement to display the no. of hits on a specific page in SharePoint 2013. Can I retrieve the no. of hits on a specific page by using any of these methods? If yes, please provide details.
-
Edited by
Bhavan Thejpal
Friday, August 02, 2013 11:48 AM
August 2nd, 2013 11:28am
Hi Guys,
My client has same requirement(SharePoint 2010 as well as SharePoint 2013), he want to show data as below.
UserName Hitcount(Today)
Suyog 500
XYZ 2000
which classes should I used, I am trying it with Spauditcollection however it is very time consuming. is anybody done this ?
October 14th, 2013 2:14pm
try this
using (SPSite site = new SPSite(MainBUPageUrl))//open root
{
using (SPWeb web = site.OpenWeb())
{
AnalyticsReportFunction AnalyticReport = new AnalyticsReportFunction(); //create object of analyticsReport
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());//this is used only
in timer job if not used then GetWebAnalyticsReportData will not work
object[,] result = null;
web.AllowUnsafeUpdates = true;
result = AnalyticReport.GetWebAnalyticsReportData(SiteCollurl, "2", ReportType.TopDestinationForSiteReport.ToString(),
DateTime.Today.AddDays(-3.0), DateTime.Today); //this gives top visitor for provided site collection by providing time duration(start and end date),report type and url
SPList list = web.Lists.TryGetList("AppreciationsList");//list
for (int counter = 0; counter < 10; counter++) //show top ten pages
{
Item = list.AddItem();
s_count = result[rcount, 1].ToString(); //code
to get top vistor usage count
Username = result[rcount++, 0].ToString();//get
top vistor name
user = null;
try
{
user = web.EnsureUser(Username);//check
user available?
}
catch (Exception)
{
counter--;
continue;
}
IfExist = checkUserExistence(user);//call the
function // IfExist = false; //this flag is used to check existence of user in insightMember group
if (IfExist == true)//if user is not in the insight2
group member list
{
Item["Appreciated Person"]
= user;
Item["Title"] = s_count;
Item.Update();
}
else //if user is in the insight2 group member
list
{
counter--;
continue;
}
}
web.AllowUnsafeUpdates = false;
}
}
October 22nd, 2013 7:20am
Hi varsha,
Thanks for reply , However I using WSS_LOGGING database.
We can fire the SQL query and get the desired data. TopUserCount view gives desired data
--This is the user count who are accesign SharePoint
SELECT [UserName]
,sum([RequestCount]) as Frequency
FROM [WSS_Logging].[dbo].[TopUserRequestCount] where LogTime between DATEADD(DD,-1,GETDATE()) and GETDATE() group by [UserName] order by [UserName]
October 22nd, 2013 7:56am
For Performance Perspective its always better to use API than database queries
-
Edited by
varshaPatil
Wednesday, October 23, 2013 6:03 AM
October 23rd, 2013 6:01am
Previously Microsoft was not encourage not to use WSS_LOGGING.
However now WSS_LOGGING is fully querable, we can query on database.
and about the performance I think database queries will give you data fast rather than API, because API will again need to call Database so API will overhead.
If we want to show data on webparts then we can go with the API, and suppose we want to show data on reports it is always good to use Database queries.
October 23rd, 2013 7:14am
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'. &n
February 9th, 2014 4:28pm
Did you got solution for this,
i have same requirement to show no of page views in SP2013.
if you found solution do let me know.
March 4th, 2014 5:57am
My client do not want it through programming, SO I did it using sql queries ans SSIS.
However for programming you can use spaudit class.
Detailed explanation is on below URL. This might help you.
http://spuser.blogspot.com/2011/10/spaudit-how-to-read-from-audit-log.html
March 4th, 2014 8:19am
You can refer to my blog (http://mukundkoirala.blogspot.com/2014/03/share-point-2013-site-collection-hit.html )for this .
You just need to make a little change.
Instead of subsite id (web.id ) you need to pass page id in the function GetAllWebsinSiteCollection
Make change in this line:
var result = searchApplicationProxy.GetRollupAnalyticsItemData(1, Guid.Empty,oSite.ID,web.ID);
March 11th, 2014 9:27am
Thank you for the reply,
If i pass page guid i getting an error "Object reference not set to an instance of an object"
Below is code snippet i using in my webpart
SPListItemCollection PagesColl = pagesLibrary.GetItems();
foreach (SPListItem item in PagesColl)
{
SPServiceContext context = SPServiceContext.GetContext(spSite);
SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
AnalyticsItemData usageData = searchProxy.GetRollupAnalyticsItemData(1, Guid.Empty, spSite.ID, item.UniqueId);
}
Please do let me know if any corrections i need to do in my code
March 12th, 2014 6:35am
Hi,
How to Get / Save the user search key value (with refiners ) in sharepoint 2013.
Thanks,
Santhosh
April 16th, 2014 12:33pm
I have written a PS script for the same. have a look.
https://sp13usagemonthlystats.codeplex.com/
August 27th, 2015 12:50am