Step by steps on how to display Most Visited Sites with number of view count in SharePoint 2013 Web Analytics

Senthil Kumaresan
 
SharePoint Consultant
March 31, 2014
 
Rate this article
 
Views
71988

It’s a backward step from Microsoft in terms of Web Analytics in SharePoint 2013 by removing the out of the box SharePoint Usage and Search Analytics reports that was provided with a more intuitive GUI in SharePoint 2010. This article shows you how to display most visited sites with view count in easy steps in SharePoint 2013

As you might have known by now that web Analytics is no more a Service Application and it is part of Search Service Application in SharePoint 2013.

Now there are few excel reports named as “Popularity reports” available in each list\library\Site and Site Collection in SharePoint 2013. It shows day/Month wise hit count of each document and list items in SharePoint 2013.But these reports in reality is not more effective and don’t present data needed to identify the effective usage of the SharePoint System. Also the SharePoint 2013 lacks the Web Analytics Web part that was available in SharePoint 2010.

You can now use the Content Search Web Part that is used to generate the Usage and Search Analytics. Content Search Web Part ships along with the Enterprise Edition of SharePoint 2013. When you activate the SharePoint Enterprise Site Features of a site collection you get the Content Search web Part available under the Content Rollup group in the Add Web Part section.

I will take a a examples of a report Most Visited Sites in SharePoint 2013 and explain how you can achieve using the Content Search Web Part which can be generated for a Site Collection, Web Application.

Note: You cannot generate any User Specific reports like “Top Visitors” or “Top Contributors” using the analytics component in SharePoint 2013 as it does not store any user specific data.

I will take a simple example of how to show “Most Visited Sites” in your Site Collection.

Most Visited Sites(With Content Search Web Part):

Make sure you have activated the “Cross Site Collection Publishing” feature activated in Site Collection Features for Content Roll Up section to appear in the Add Web Parts the To add a Content Search Web Part to your page.

Configure the content search web part using the below parameters

Edit the Content Search Web Part and Provide the below values for the properties of the Web part

1. Select “Change Query” Button. In the Basics Tab for Query window, Select the “Switch to Advanced Mode”.

Basics TabValue
QueryLocal SharePoint Results
QueryTextUse the below query text for Most Visited Sites:

 

(contentclass:STS_Web AND Path:www.contoso.com/sites/contoso*)

 

 

 

Use the below query for Most Visited Documents

 

(IsDocument:"True" AND path:http://www.contoso.com/sites/contoso*)

 

Note: The hit count of documents is recorded in Analytics component when the documents are opened using Office Web Apps in a Web Page.

 

 

 

Use the below query for Most Visited Pages:

 

(Path:http://www.contoso.com* AND (FileExtension:aspx or FileExtension:html)) (contentclass:STS_ListItem OR IsDocument:True)

 

 

 

Note: The above query only includes Pages content from libraries in the site and excludes any page that are available in the root folder of the site.

Sorting TabValue
Sort ByViewsRecent Descending (Orders the result set by ViewCount for last 14 days )

(Or)

ViewsLifeTime Descending (Orders the result set by ViewCount from the time the Analytics Component was enabled in the farm)

Click ok to Save the Query.

The above configuration will have the content search web part display the results for the Most Visited Sites, but it does not show you the no. of View Counts.

How do you display the total no. of Views for each Site ?

 

You can modify the Display Templates for Content Search Web Part to show the View Count. The total no. of Views will be stored in the Managed Properties ViewsRecent or ViewsLifeTime.

You can add managed properties to your item level display template and show the value of the managed property in the display templates which will show you the count in web part.

Note : Download the sample display template with ViewRecent managed property from the bottom of the post

Deploy the Item Template to _catalogs/MasterPages/Display Templates/Content Web Parts folder. A Corresponding JS file will be automatically created in the folder.

The new item level display template can then be used by configuring the web part settings to use it.

You don’t have an Enterprise Edition then how do you generate these reports without Content Search Web Part?

SharePoint 2013 exposes a set of methods via the Microsoft.Office.Server.Analytics Namespace that is used to generate reports programmatically using Server Side Object Model.

There is no programmatic way to query analytics data using Client Side Object Model as it requires RunWithElevatedPrivileges to read data from Analytics Database.

Below I have provided a Code snippet that can be used to generate the Most Visited Sites programmatically. I have used the method GetHitCountForMonth to generate hit count for the last 30 days. You can also query per day by using GetHitCountForDay method.

 Dictionary<string, int> viewCounts = new Dictionary<string, int>();
 SPSecurity.RunWithElevatedPrivileges(delegate
 {
     using (var site = new SPSite("http://www.contoso.com/Sites/Test"))
     {
         using (SPWeb oWebsite = new SPSite("http://www.contoso.com/Sites/Test").OpenWeb())
         {
             SPWebCollection collWebsite = oWebsite.Webs;
 
             foreach (SPWeb subSite in collWebsite)
             {
                 try
                 {
 
                     var context = SPServiceContext.GetContext(site);
                     var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
                     var usageData = searchProxy.GetRollupAnalyticsItemData((int)StandardEventTypeId.View, Guid.Empty, site.ID, subSite.ID);//1 represents "View Count" Event Type
 
 
                     if (usageData.GetHitCountForMonth(DateTime.Now) != null)
                     {
                         viewCounts.Add(subSite.Title, usageData.GetHitCountForMonth(DateTime.Now));
                     }
                     else
                     {
                         viewCounts.Add(subSite.Title, 0);
                     }
                     subSite.Close();
                     return viewCounts;
                 }
                 catch (Exception er)
                 {
                     throw new Exception("An error has occurred. Please contact your farm administrator");
                 }
             }
         }
 
     }
 
 });

You can also use the Microsoft.Office.Server.Search.Analytics.UsageAnalytics Namespace that can be used to generate item level reports such as “Most Visited Documents”. Below I have provided a snippet that can be used to identify view count for each item in a Pages library

 Dictionary<string, int> viewCounts = new Dictionary<string, int>();
 SPSecurity.RunWithElevatedPrivileges(delegate
 {
     using (var site = new SPSite("http://www.contoso.com/sites/test"))
     {
         using (SPWeb oWebsite = new SPSite("http://www.contoso.com/sites/test").OpenWeb())
         {
 
             try
             {
                 var context = SPServiceContext.GetContext(site);
                 var searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
                 UsageAnalytics obj = new UsageAnalytics(site);
 
                 SPList list = oWebsite.Lists["Pages"];
                 SPQuery query = new SPQuery();
 
                 foreach (SPListItem item in list.Items)
                 {
                     var usageData = obj.GetAnalyticsItemData((int)StandardEventTypeId.View, item);
                     if (usageData.GetHitCountForMonth(DateTime.Now) != null)
                     {
                         viewCounts.Add(item["Title"].ToString(), usageData.GetHitCountForMonth(DateTime.Now));
                     }
                     else
                     {
                         viewCounts.Add(item["Title"].ToString(), 0);
                     }
                 }
                 return viewCounts;
             }
             catch (Exception er)
             {
                 throw new Exception("An error has occurred. Please contact your farm administrator");
             }
         }
     }
 
 });

Download display template with View Recent Property

Category : SharePoint

Author Info

Senthil Kumaresan
 
SharePoint Consultant
 
Rate this article
 
SharePoint Architect – Consulting on SharePoint Infrastructure, IT Governance and Apps Development. ...read more
 

Leave a comment