You probably know that one biggest challenge is how to get SharePoint 2010 Web Analytics data in Web Analytics Report programmatically. It’s a little stupid (I suppose) if Microsoft doesn’t provide API to access this cool feature. Although there’s an API in a method like GetUsageData(), but this thing doesn’t enough. We want more detailed and mimicking the SharePoint Web Analytics Report.
I’d rather write my own code about HttpModule or use a third party Analytics feature like Google Analytics than searching entire database for those kind of reports. But, that’s just became a nightmare if you see that the portal is not published to the Internet.
Well, after 2 days full searching (really – no sleeping at all ) in entire SharePoint 2010 Web Analytics Database, I found how to do it. By the way, accessing Database generated by SharePoint 2010 isn’t really supported by Microsoft. But I think it’s OK as long as you don’t write or update any data to them, go on, express yourself.
For my environment, my Web Analytics Reporting Database is in WebAnalytics_RPT_Service_DB. You might want to check what’s your Web Analytics Reporting Database, check it out on SharePoint Central Administration, then go to Manage Service Applications, click to your Web Analytics Service Application name.
Highlight your WA Service Application, and click properties in the ribbon.
Click Next on the dialog, and you’ll see your Reporting Database Name.
Now we might want to test for the queries to the web analytics service. Actually, I don’t have much time to wrote what could be the query for Top Pages, or any other WA Queries, (I’ll write on next post ). But if you like, you can use SQL Server Profiler, attach to your Reporting DB, and view some specific WA Reports in Site Settings, and voila, there’s the query.
Let’s see, I have one example of query, and you can use it. Here’s one of the query…
- exec sp_executesql
- N'SELECT TOP (@MaxRow) [PageId], [Frequency], [Percentage]
- FROM [dbo].[fn_WA_GetTopPages](@Start, @End, @AssetGuid, default)
- WHERE [PageId] LIKE ''%posts/post.aspx%''
- ORDER BY [Frequency] DESC',
- N'@Start int,@End int,@AssetGuid uniqueidentifier,@MaxRow int',
- @Start=20110327,@End=20110425,@AssetGuid='C629FD58-0E21-4C6F-F8E4-681352410415',@MaxRow=10
You can set the Start time in @Start with int data type, and End time in @End. And also you might want to remember, the @AssetGuid is unique and represents each object in SharePoint.I thought that @AssetGuid might be the SPSite or SPWeb GUID, but it’s not. @AssetGuid is the SPSite, SPWeb, SPWebApplication, or SPFarm object, calculated with MD5 Hash (psssst…. I tell ya…don’t tell anyone… this is according to the Microsoft’s DLL, Microsoft.Office.Server.WebAnalytics.AggregationIdGenerator inside Microsoft.Office.Server.WebAnalytics, Version=14.0.0.0).
I think that almost imposible if you don’t create custom class to create @AssetGuid which is named as Aggregation ID. After I looked deeper into Microsoft’s DLL for Web Analytics, finally ! There’s one class which in charge in creating Aggregation ID, but it’s declared as internal, so any other class like mine, can’t read that class. So, I’ve created one same as Microsoft’s.
UPDATE: guess what?! When I’m searching about those queries in google, I’ve found a detailed description and documentation about Analytics Report query, and it’s released by Microsoft since 7/13/2009. Here’s the link [MS-MAVA]: Microsoft Office SharePoint Server (MOSS) Analytics View Access Protocol Specification. You can download the PDF’s to make you more comfortable to read at. So, I don’t really need to write all detailed queries to my next post right ? Almost every query, will involve @AssetGuid (a.k.a Aggregation ID).
This is the class of mine (well, not mine actually ), and remember, involving 2 classes. This class calculates MD5 Hash for any SharePoint objects.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.Globalization;
- namespace Rdz.SharePoint.WebAnalytics
- {
- internal static class CommonMethods
- {
- internal static class Hasher
- {
- internal static byte[] ComputeHash(string input)
- {
- if (string.IsNullOrEmpty(input))
- return null;
- return MD5.Create().ComputeHash(Encoding.Unicode.GetBytes(input));
- }
- internal static string ConvertHashToString(byte[] hash)
- {
- if (hash == null)
- return null;
- StringBuilder builder = new StringBuilder("0x");
- for (int i = 0; i < hash.Length; i++)
- {
- builder.Append(hash[i].ToString("X2", CultureInfo.InvariantCulture));
- }
- return builder.ToString();
- }
- }
- }
- }
And this is the main class, AggregationIdGenerator.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Administration;
- namespace Rdz.SharePoint.WebAnalytics
- {
- public static class AggregationIdGenerator
- {
- private static Guid GetAggregationId(string id)
- {
- return new Guid(CommonMethods.Hasher.ComputeHash(id));
- }
- public static Guid GetAggregationIdForFarm()
- {
- return GetAggregationId(GetFarmId().ToString());
- }
- public static Guid GetAggregationIdForFarm(Guid farmId)
- {
- if (farmId == Guid.Empty)
- return Guid.Empty;
- return GetAggregationId(farmId.ToString());
- }
- public static Guid GetAggregationIdForServiceApplication(SPServiceApplication serviceApplication)
- {
- if (serviceApplication == null)
- throw new ArgumentNullException("serviceApplication");
- return GetAggregationId(serviceApplication.Id.ToString());
- }
- public static Guid GetAggregationIdForServiceApplication(Guid serviceApplicationId)
- {
- if (serviceApplicationId == Guid.Empty)
- return Guid.Empty;
- return GetAggregationId(serviceApplicationId.ToString());
- }
- public static Guid GetAggregationIdForSite(SPSite site)
- {
- if (site == null)
- throw new ArgumentNullException("site");
- return GetAggregationId(site.ID.ToString());
- }
- public static Guid GetAggregationIdForSite(Guid siteId)
- {
- if (siteId == Guid.Empty)
- return Guid.Empty;
- return GetAggregationId(siteId.ToString());
- }
- public static Guid GetAggregationIdForWeb(SPWeb web)
- {
- if (web == null)
- throw new ArgumentNullException("web");
- return GetAggregationId(web.Site.ID.ToString() + web.ID.ToString());
- }
- public static Guid GetAggregationIdForWeb(string webUrl)
- {
- if (webUrl == null)
- throw new ArgumentNullException("webUrl");
- using (SPSite site = new SPSite(webUrl))
- {
- if (site != null)
- using (SPWeb web = site.OpenWeb())
- {
- if (web != null)
- return GetAggregationId(site.ID.ToString() + web.ID.ToString());
- }
- }
- return Guid.Empty;
- }
- public static Guid GetAggregationIdForWeb(Guid siteId, Guid webId)
- {
- if (siteId == Guid.Empty)
- return Guid.Empty;
- if (webId == Guid.Empty)
- return Guid.Empty;
- return GetAggregationId(siteId.ToString() + webId.ToString());
- }
- public static Guid GetAggregationIdForWebApplication(SPWebApplication webApplication)
- {
- if (webApplication == null)
- throw new ArgumentNullException("webApplication");
- return GetAggregationId(webApplication.Id.ToString());
- }
- public static Guid GetAggregationIdForWebApplication(Guid webApplicationId)
- {
- if (webApplicationId == Guid.Empty)
- return Guid.Empty;
- return GetAggregationId(webApplicationId.ToString());
- }
- private static Guid GetFarmId()
- {
- SPFarm local = SPFarm.Local;
- if (local == null)
- throw new InvalidOperationException("Server not joined in farm: " + System.Net.Dns.GetHostName());
- return local.Id;
- }
- }
- }
You can play and work with those 2 classes . Sorry to Microsoft, I’ve already done it (peeking into your own class and created the same one). Hope it helps .
UPDATED:
For anyone who do have a problem when getting Aggregation ID instantly, you can read through this post about how to get it. I’ve created a little tools to get Aggregation ID using input URL. Of course this tools must be running in SharePoint box. And another post on how to query the Web Analytics Report from it’s database, you can look at this post how to do it. If after using this tools, and you’ve tried to query web analytics and no results, you should think again !
No, I mean you should look back and see what you’ve done. Are you creating the Web Analytics properly? Is the Web Analytics data is valid? I mean, you can look at the SharePoint web analytics at your SharePoint site, go to Site Settings and then click Site Web Analytics Report. If there’s no data in there, then you should look again at your Web Application configurations.
And if your web analytics looks like this,
you should be happy , because there’s no need to worry. Check again your Web Analytics Service Application Association. Is it already attached to your Web Application? You can look at your Central Administration.
Then if you do have a problem, you can (again) verify the Web Analytics Reporting Database at my post above . Hope it helps, folks………….
.
.
.
And… click +1 if you do like!
Took me time to read all the comments, but I really enjoyed the article. It proved to be very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had joy writing this article.
BalasHapusFYI, this kind of thing, it really stressed me up. After I found the solution, it's just like running on finish line at the first row. :D
BalasHapusAnyway, hope it helps (and cheers)....
Nice one. Let me try the same
BalasHapusHi there,
BalasHapusfirst of all thanks for your article and the link to the protocol specification.
Just a short question: Am I right that I can use "fn_WA_GetTopPages" to get the hits at a specific page within a given time?
The solution would be to create a class for getting the MD5 hash and then for creating an SQL query in order to use the MD5 hash to get the analytics data I want, right?
Thanks in advance,
Andreas
That's correct Andreas. Actually, that class what I've wrote above, really really similar to the Microsoft's class. Why? coz the Microsoft's class is declared as internal, so I can't add reference to that class to get AggregationId.
BalasHapusThe class is getting the MD5 Hash from SharePoint objects, then you can use the generated aggregatorid to query the toppages. Mostly of the queries described in MS-MAVA (in the link I've provided above), needs this aggregationid.
Secondly, yes, you can use "fn_WA_GetTopPages" to get hits at a specific page and date range. Yes, you can use any queries described in MS-MAVA. As long as not modifying the web analytics but queries, I think it's OK.
Sorry if my english is bad. still learning.
Hi,
BalasHapusAre you writing a custom ASP.NET /SQL application to pull these data ?
Even you can do it from PHP, desktop app, coz it's just query to get web analytics. Don't need to use SharePoint API to get this data.
BalasHapuswhat could be advantage using this other than WSS_logging?
BalasHapusWell, like what I've said before, I'm just want to get report which is really-really similar to SharePoint Web Analytic Report in every Site Collection Settings and Site Settings.
BalasHapusI actually haven't read any literature about WSS_Logging, but based on my searchs, WSS_Logging is providing basic report, usage request, feature request, ULS Logs, performance counters, timer job usage request, etc. You can look at http://sharepointmalaya.blogspot.com/2011_03_01_archive.html.
But one thing for sure, the process is really complicated. And the 'end-data' served in Reporting Analytics DB. Briefly, the process is like this, SharePoint HttpModule (to record all requests and response from http) >> WSS_UsageApplication (the data still in raw format in here, but you can still query from here with extended filters) >> Staging Analytics DB >> Reporting Analytics DB.
Hi Radityo,
BalasHapusI can only find this article when I look for Web analytical reports..Thanks for gr8 research,, I'm bit behind in catching up the technical bits in this article.. first question how and where do you place these custom classes? Web.config?!
Any help will be appreciated.
Just custom classes, DLL. Deploy those DLLs to Global Assembly Cache (C:\Windows\Assembly) by dragging it into those folder. Add your webpart/app/whatsoever reference to those 2 DLLs, and you can start using it.
BalasHapusThank you so so much..
BalasHapusHi , on the tecnical side this approach is owsome but i think we lose Microsoft support even for Reading data from that Db. Im probing Microsoft and i'll post a comment on that.
BalasHapusScoutmanPt
Yeah right, Scoutman,
BalasHapusI don't think my approach is the best. I still waiting and would prefer Microsoft SP Object Model to do more about this.
But since there's no props or method I could use, then this is the another solutions of mine. I hope Microsoft would create another approach (or updates on next Service Packs), so everyone's happy and safe.
Hi Radityo,
BalasHapusWould it be alright if you can show us an example for this? I am quite new into sharepoint and it seems that i need to do something like this. I see the 2 classes but i still don't really know how to use it programatically.
Very sorry if this request is quite noob.
Hi Radityo,
BalasHapusI'm trying to develop my own web analytics webpart within Sharepoint 2010 so your post has really helped me to understand the access to the analytics database and their functions. I did a console application in VS to test the MD5 hashing calculation but the guid obtained using the hash value doesn't match with any aggregation id stored in my web analytics database, thus I can't get any results, and I don't understand why.
Thanks for the knowledge shared in this blog and hope you could get me some idea about my problem.
EGC
Well, Anonymous (at Wednesday, December 14, 2011 3:20:00 AM GMT+07:00),
BalasHapusyou can create 2 classes on your solution and use those 2 classes to get the web analytics Aggregation ID. That's the point!
Because almost every operations in this query, involving Aggregation ID, which is returned by calculating MD5 hash of the SPSite or SPWeb GUID.
From then, you can look at MS-MAVA about detailed query that you can use on Reporting Web Analytics Database.
And, for Anonymous (at Thursday, December 15, 2011 7:16:00 PM GMT+07:00),
BalasHapusyou can look at this post, you can see my update below it.
How can we show the up down arrow and "new" icon against the page. How to identify this?
BalasHapusFor Anonymous (at Sunday, December 18, 2011 2:23:00 AM GMT+07:00), I'm a little confuse with your question. Would you mind to show me what and where is the up down arrow, and what "new" icon do you mean?
BalasHapushey..awesome post
BalasHapusm newbie in shrepoint....
could you provide me any working code or something for web analytic
Just follow this post, there's an update sometimes.
HapusBtw, if you need to publish analytics data on the site pages, in our HarePoint Analytics for SharePoint we have a special web part for this purpose:
BalasHapushttp://www.harepoint.com/Products/HarePointAnalyticsForSharepoint/AnalyticsDataSharePointPages.aspx
WBR, Alexander
thanks for sharing @agorlach... nice thing though...
HapusYou could just use AnalyticsReportFunction, like this-
BalasHapususing Microsoft.Office.Server.WebAnalytics.Reporting;
AnalyticsReportFunction ar = new AnalyticsReportFunction(); // this is public class, but no document about it.
object[,] result = ar.GetWebAnalyticsReportData(
"http://somewebsite/",
"2", // report level,
"TopPageForPageReport", // report name,
DateTime.Today.AddDays(-30.0), // start date,
DateTime.Today); // end date,
U sure? Will test it if I have a time.
HapusIt seems promising... :P
Anyway, thanks maaaan!
Very informative post.I have really enjoyed in reading let me try in programming also.Hope it works as it seems.Thanks for sharing.
BalasHapusthanks buddy....
HapusHope it helps...
Hallo Radityo,
BalasHapusMany thanks for a very useful post!
I have a problem with Web Anayltics configuration. How can i configure this so that site owner or user are not able to access it but it is still enabled. The reason is to allow third party softwares such as Control Point having access to Web Analytics but to prevent other users (esp. site owners) due to data privacy regulations.
Any feedback would be appreciated
Cheers
Billy
Billy, thanks for the comments.
HapusWell that's pretty abnormal, since Site Owners always have access to this things. Well, pretty clear from its name, Site Owners.
You could use lower level than Site Owners for that users, i.e.: Designer or Manage Hierarchy.
Another way, is to modify the settings.aspx right from the LAYOUTS folder of SharePoint server, and change the link of Web Analytics to be displayed for a user with certain permission level. But I wouldn't recommend that, since that would change the whole SharePoint Web Application.
Many thanks for your time and in particular with your useful feedback so quickly, Radityo! Very much appreciated!
HapusCheers
Billy
Sure, no problem :D
HapusAggregationContext.GetContext can be used to get the AggregationId
BalasHapusI am trying to use SP.GetUsageData returns NULL data and GetUsageBlob returns response object length = 0 . Usage timer jobs are running fine on regular intervals.
BalasHapusWhat could be the reason both methods are not returning expected results.
My environment is SharePoint 2010 and is it recommend to use GetUsageBlob as per standard practice?
I want to create report which will display "Number of visits/user on every site collection"
Any thoughts?
Great post! but how can i retreive query results data to sharepoint using visual studio??
BalasHapusHere is an example on how to get the Web Analytics summary report using PowerShell: http://gallery.technet.microsoft.com/office/Get-SharePoint-Web-19cd2137
BalasHapusThat's a great PowerShell. I should try it sometime.... :D
Hapus