Posted on:
Categories: PowerShell;SharePoint
Description:

​For purposes of estimating usage and site adoption, SharePoint 2013 web analytics can provide simple page hit statistics for every site within your farm. Making readable and useful data reports of this information can come down to some simple average mathmatics. My goal was to provide easily readable output in a universal format that is intuitive and flexible. From this data, further processes can be developed to ensure availability and stability of consistently high traffic sites.

Below is a script that will output page hit data for all sites collecting this information in your farm.

The script omits any url that contains typically named MySite sites and site collections, so if you would like to include those that line can be simply adjusted.

 

$Folder = "C:\_tools\output"

$Date = Get-Date -format "d-M-yyyy_HHmmss"

$OutFileAllResults = $folder + "\Z_WebAnalyticsAll_" + $date + ".csv"

$OutFileNoData = $folder + "\Z_WebAnalytics_NoData_" + $date + ".csv"

$OutFileTop25Pct = $folder + "\Z_WebAnalytics_Top25Pct_" + $date + ".csv"

Add-PSSnapin microsoft.sharepoint.powershell -ea SilentlyContinue

$SearchApp = Get-SPEnterpriseSearchServiceApplication

# Edit the below to include or exclude any sites with keyword urls.

$Sites = Get-SPSite -Limit All | ? {!$_.url.Contains("my") -and !$_.Url.Contains("personal")}

$DataTable = @()

$DataTableNoData = @()

# ------------------------------------WRITE CSV HEADER ----------------------------------------

[string]$headerString = ""

for($i=-1; $i -ge -13; $i--)

{

    if ($i -eq -1){$headerString += "Site"}

    else {$headerString += "," + ((Get-Date).AddDays($i)).ToString("dd/MM/yyyy")}

}

$headerString | Out-File -LiteralPath $OutFileAllResults -Append

# ----------------------------------------------------------------------------

# ---------------------------------------COLLECT ANALYTICS DATA INTO ARRAYS -------------------------------------

# Array format [average hits:0, max hits:1, CSV string:2]

ForEach ($Site in $Sites)

{

    ForEach($Web in $Site.AllWebs)

    {

    [int]$webMaxHits = 0;

    [int]$webHitsAverageIncrement = 0;

    [string]$csvOutputLineItemString = ""

        $UsageData = $SearchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$Site.ID,$Web.ID)

        if ($UsageData -ne $null)

        {

            for($i=-1; $i -ge -13; $i--)

            {

                $thisDaysHitCount = $UsageData.GetHitCountForDay((Get-Date).AddDays($i))

                $webHitsAverageIncrement += $thisDaysHitCount


                if ($thisDaysHitCount -ge $webMaxHits)

                {$webMaxHits = $thisDaysHitCount}


                if ($i -eq -1)

                {$csvOutputLineItemString += $Web.Url}

                else

                {$csvOutputLineItemString += "," + ($UsageData.GetHitCountForDay((Get-Date).AddDays($i)))}

            }

        $webHitsAverageIncrement = [Math]::Truncate($webHitsAverageIncrement/13)

        # append to all data array

        $DataTable += ,@(($webHitsAverageIncrement),$webMaxHits,$csvOutputLineItemString)

        }

        else

        {

        # append to nodata array

        $DataTableNoData += ,@(-1,-1,$Web.Url)

        }

    }

}

# -----------------------------------------------------------------------------------------------------------------

## Sort the Array for Descending Averages

$DataTable = $DataTable | Sort-Object @{Expression={$_[0]};Ascending=$false}

##-------------------------------------------------OUTPUT DATA TO CSV ----------------------------------------------------------------

# output all data

[int]$withValuesCount = 0;

for($i=0; $i -lt $DataTable.Count; $i++)

{

    ($DataTable[$i][2]) | Out-File -LiteralPath $OutFileAllResults -Append

    if($DataTable[$i][1 -ne 0]){$withValuesCount += 1;}

}

## output no data

for($i=0; $i -lt $DataTableNoData.Count; $i++){($DataTableNoData[$i][2]) | Out-File -LiteralPath $OutFileNoData -Append}

## output top 25% with values if top 25% is less than 20 lines

if ([Math]::Truncate($withValuesCount * .25) -gt 20)

{

    for($i=0; $i -lt [Math]::Truncate($withValuesCount * .25); $i++){($DataTable[$i][2]) | Out-File -LiteralPath $OutFileTop25Pct -Append}

}

else

{

    for($i=0; $i -lt 20; $i++){($DataTable[$i][2]) | Out-File -LiteralPath $OutFileTop25Pct -Append}

}

# -----------------------------------------------------------------------------------------------------------------