Customizing Web Analytics in SharePoint

From quite some time, I was trying to find ways to customize the SharePoint 2010 Web Analytics to capture the usage of Internal SharePoint solutions.

With SharePoint Web Analytics,  Users will be able to find the following three categories of reports out of the box from SP2010 (More Details) but I wanted to find out the customization details of this functionality so I can capture the data my way and create my own reports.

Traffic Reports:

  • Number of Page Views
  • Top Referrers
  • Top Visitors
  • Number of Daily Unique Visitors, Top Destinations, Top Browsers, etc.

Search Reports

  • Top Queries
  • Number of Queries 
  • Failed Queries
  • Best Bet Usage, Search keywords, etc.

Inventory reports:

  • Total disk drive space usage
  • Number of Site Collections 
  • Top Site Product Versions, Top Site Languages, etc.

The web analytics data comes from the request usage & It implements a usage receiver. From reflector we can see that SPUsageReceiver is an abstract class and we can write our own solution to override this class. We can create an usage receiver so that we can check every incoming request and store the statistics that we would like to. The receiver is going to be called when Usage Import job is executed.

image

Custom Usage Receiver

    1: class CustomSPRequestUsageReceiver : SPUsageReceiver    
    2: {         
    3:     public override void UsageImported(SPUsageReceiverProperties usageProperties)         
    4:     {                 
    5:         while (usageProperties.UsageEntries.MoveNext())                 
    6:         {                     
    7:             SPRequestUsageEntry entry = usageProperties.UsageEntries.Current as SPRequestUsageEntry;                    
    8:             
    9:             // You may write your custom logic here to access the entries per the custom requirement                 
   10:         }         
   11:     }     
   12: } 
  

You can register your custom usage receiver like below,

    1: SPRequestUsageDefinition definition = SPRequestUsageDefinition.Local; 
    2:  
    3: definition.Enabled = true; 
    4:  
    5: definition.EnableReceivers = true; 
    6:  
    7: definition.Receivers.Add(typeof(CustomSPRequestUsageReceiver)); 
    8:  
    9: definition.Update();