SharePoint Developer Support Team Blog

The Official blog of the SharePoint Developer Support Team

HOW TO: Retrieve Web Analytics report data using API

HOW TO: Retrieve Web Analytics report data using API

  • Comments 10
  • Likes

This blog post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team.

In a scenario that you want to render Web Analytics report data to your favorite format, you need to retrieve the report data. There’s no document on how to retrieve SharePoint built-in report data. Some online articles are suggesting retrieving data directly from Web Analytics reporting database. It’s not recommended. Even with this approach, often customer faces issue of getting AggregationId which is required to retrieve data from database.

Here’s the sample code that can be used to retrieve Web Analytics report data.

using Microsoft.Office.Server.WebAnalytics.Reporting;
 
AnalyticsReportFunction ar = new AnalyticsReportFunction();  
object[,] result = ar.GetWebAnalyticsReportData(
                "http://yoursiteurl/",
                "2",                            // Report level,
                "TopPageForPageReport",         // Report name, 
                DateTime.Today.AddDays(-30.0),  // Start date,
                DateTime.Today);                // End date,

In the above, the Report level parameter is shown below:

image

And the Report name parameter is shown below:

image

Here’s what is returned

image

And here’s what you see from UI (WA report)

image 

Comments
  • Nice post, I'll need to try this out!

  • your post is very good .

    i have a question, how to get FAST Search Connector Crawl Log  or History?

    however the  CrawlLog Class methods is obsoleted , please help if you have any answer .

  • Nice post

  • Nice Post. Do you know the client side API for Usage Analytic report data in SharePoint 2013 ?  I have a requirement to show the page views in custom developed apps. let me know your views if you can.

  • Hi raam,

    Web Analytics has been changed in SharePoint 2013 significantly. Please check out the followings articles for details. Haven't got chance to research how you can bring those limited reports in an app.

    technet.microsoft.com/.../ff607742.aspx

    technet.microsoft.com/.../jj219554.aspx

  • nice post,
    i need to get same in sharepoint2013,if u have code pls share.
    i know this feature is removed in Sp2013,it is merged with search service application.
    but i m not knowing how to get in SP2013.
    i tried below linked post but no use
    http://social.technet.microsoft.com/Forums/sharepoint/en-US/06e00842-6517-443f-866f-f91eae396f17/how-get-toppageforpagereport-programmatically-in-sharepoint-2013?forum=sharepointsearch

  • Here's an example for SharePoint 2013:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.Office.Server.Search.Administration;
    using Microsoft.Office.Server.Search.Analytics;
    ...
    SPSite site = new SPSite("http://yourserver/sites/testsite/");
    SPUserToken userToken = site.UserToken;

    SPSecurity.RunWithElevatedPrivileges(delegate
    {
    using (SPSite site1 = new SPSite(site.ID, userToken))
    {
    SPServiceContext context = SPServiceContext.GetContext(site1);
    SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
    AnalyticsItemData usageData = searchProxy.GetRollupAnalyticsItemData((int)SearchReportTypeId.TopQuery, Guid.Empty, Guid.Empty, Guid.Empty);

    int dailyHits = usageData.GetHitCountForDay(DateTime.Today);
    int monthlyHits = usageData.GetHitCountForMonth(DateTime.Today);
    Console.WriteLine(String.Format("Report Daily Hits={0}; Monthly Hits={1}", dailyHits, monthlyHits));

    uint maxRows = 1000;
    DateTime startDate = new DateTime(2014, 1, 24);
    IList searchResults = searchProxy.GetSearchReport((int)SearchReportTypeId.TopQuery, Guid.Empty, Guid.Empty, startDate.Date, true, maxRows);
    if (searchResults.Count > 0)
    {
    foreach (QueryReportData rptData in searchResults)
    {
    int hitsCnt = rptData.Count;
    string qry = rptData.Query;
    Guid scope = rptData.Scope;
    Console.WriteLine(String.Format("Report Count={0}; Query={1}; Scope={2}", hitsCnt, qry, scope.ToString()));
    }
    }
    }
    });
    ...

    SearchReportTypeId enumeration details are here:
    http://msdn.microsoft.com/en-us/library/office/microsoft.office.server.search.analytics.searchreporttypeid.aspx

  • Unfortunately, wasn't able to get this working with SummaryReport on 2010. Always got the "viewName" error. Anyone had success?

  • I also get the "viewName" error when accessing the SummaryReport. I'm tried the fixes described above

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment