Get SharePoint Web Analytics using PowerShell Programmatically

 PowerShell example on how to get SharePoint Web Analytics Data

4.4 Star
3,648 times
Add to favorites
E-mail Twitter Digg Facebook
  • Getting Same Error
    1 Posts | Last post November 10, 2016
    • Hi Ivan ,
      I Ran the script on our Development Server , its working fine there . But when i am running the same script on production server , I am getting below error .
      Exception calling "GetContext" with "1" argument(s): "<nativehr>0x80070003</nativehr><nativestack></nativestack>"
      At D:\Scripts\WebAnalytics.ps1:27 char:119
      +     $AggregationContext = [Microsoft.Office.Server.WebAnalytics.ProcessedDataRetriever.AggregationContext]::GetContext <<<< ($Context);
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException
      Cant get the Aggregation Context
      At D:\Scripts\WebAnalytics.ps1:28 char:19
      +     if (!$?){throw <<<<  "Cant get the Aggregation Context";}
          + CategoryInfo          : OperationStopped: (Cant get the Aggregation Context:String) [], RuntimeException
          + FullyQualifiedErrorId : Cant get the Aggregation Context
      I am running the script with Farm Account . 
      You told previously to make sure that the Web analytics Service Application is connected to the Web Application that the site is in.How to do that ?
      I checked following thing .
      Go to Central Admin -> Manage web applications->Click on Web Application ->Service Connections ->Web Analytics Service Application is checked or not ?
  • Custom Date
    1 Posts | Last post March 20, 2016
    • Hi Ivan,
      Could you please tell me, How to pass custom date parameter to this script. 
      I would like to get the report from Jan -01- 2016 to Jan -31 - 2016 and Feb-1-2016 to Feb-29-2016. I want to generate monthly report.
  • Unable to get the context for the script to run
    3 Posts | Last post March 11, 2015
      I've logged into my SharePoint server and tried running the script. When the code gets to line 29 to retrieve the $AggregationContext, I get an error message:
      Exception calling "GetContext" with "1" argument(s): "Object reference not set
      to an instance of an object."
      I've checked that the $Context object exists by using a write-output of the context of the web application, and it does print out the DisplayName and url of the webapp.
      I'm also running it with admin privileges and have checked that the WebAnalytics DLLs do exist in the assembly.
      Any other ideas on why it is not able to find the context?
      Thanks, Victor
    • Try running the script with the Farm Account. Also make sure that the Web analytics Service Application is connected to the Web Application that the site is in.
    • Thanks
  • Modify to run fn_WA_GetTopVisitors
    2 Posts | Last post March 05, 2015
    • I've slightly modified the script to try and get the Top Visitors report for the web application.
          # Create parameters list     
          $viewParamsList = New-Object System.Collections.Generic.List[Microsoft.Office.Server.WebAnalytics.ProcessedDataRetriever.ViewParameterValue] 
          $viewParamsList.Add((New-Object Microsoft.Office.Server.WebAnalytics.ProcessedDataRetriever.ViewParameterValue("StartDateId", (DateTimeToDateId($ReportStartDate))))); 
          $viewParamsList.Add((New-Object Microsoft.Office.Server.WebAnalytics.ProcessedDataRetriever.ViewParameterValue("EndDateId", (DateTimeToDateId($ReportEndDate))))); 
          $dataPacket = [Microsoft.Office.Server.WebAnalytics.Reporting.FrontEndDataRetriever]::QueryData($AggregationContext, $null, "fn_WA_GetTopVisitors", $viewParamsList, $null, $null, 1, 25000, $False); 
          if (!$?){throw "Unable to get the Data. Try running the script as the Farm Account. If that doesnt work, make sure that the Web Analytics Service Application is connected to the Web Application and that the Site Web Analytics reports work through the browser.";}  
          return $dataPacket.DataTable
      The script doesn't throw any errors or display any data.  The only thing I've really changed is that the method accepts the start date and end date as parameters and I'm calling fn_WA_GetTopVisitors instead of fn_WA_GetSummary.  
      Any ideas on how I can get this to work so that it returns the Top Visitors report for the web application I'm passing into it?
    • I gave up on this and found another solution that meets my needs. Here's a sample snippet from my code:
      $server = "CentralAdminUrl" 
      $webAppName = "MyWebApp" 
      $sd = 01/01/2015 
      $ed = 01/31/2015 
      $wc = New-Object System.Net.WebClient 
      $wc.Credentials =  [System.Net.CredentialCache]::DefaultCredentials 
      $url = $server+ "/_layouts/WebAnalytics/Report.aspx?t=TopVisitorsReport&l=wa&id=https%3a%2f%2f"+$id+"%2f&WebAppName="+$webAppName+"&CD=true&SD="+$sd+"&ED="+$ed+"&SHOWANALYZETAB=1&Export=1" 
      $file = "d:\Logs\$(get-date -f yyyy-MM)-WebApp-TopVisitors.csv" 
      $wc.DownloadFile($url, $file) 
      Go in Central Admin on your SharePoint server and look at the URL for the web analytics reports. You can just create a web client object in your code and use that url to get the report. 
  • How to write it in CSV File
    2 Posts | Last post October 01, 2014
    • Hi Ivan,
      Thanks for the script. I tried this and it worked fine. I want to export the report in CSV file, I tired some commands but I am not able to export the DataTable in CSV. 
      Can you help me out?
    • I used this:
      $dt = Get-WASummaryReport -Context $web -DaysToGoBack 30
      $dt | export-csv toFile.csv -NoTypeInformation
  • Sharepoint 2013 script
    2 Posts | Last post August 20, 2014
    • Do you have a similar script that works with Sharepoint 2013?
    • Hi Jimbo,
      You may try this -
  • Is there a similar script for SharePoint 2013
    2 Posts | Last post March 01, 2014
    • I would love to see a similar script for SharePoint 2013 as there's literally nothing out there at the moment.  Thanks.
    • Hi!  I created a CodePlex project with similar functionality for SharePoint 2013.  Hopefully this is useful to others!
      SharePoint 2013 Web Analytics Data Export to CSV using PowerShell
  • Eample of usage of getSortOrder method
    1 Posts | Last post January 21, 2014
    • Could you show a little example showing the usage of the getsortOrder method please.
      Thank you in advance.