SharePoint 2013/2016 Usage Reports fail to show the correct number of clicks from anonymous access

Symptom

When using a Search Center with anonymous access, click statistics do not compute correctly for anonymous access but registers other user's accesses properly.

ULS logs will show entries like this:

w3wp.exe SharePoint Foundation CSOM ajwqj Medium Request does not have SPBasePermissions.UseRemoteAPIs permission. Need to check it when each API is accessed

Repro Steps

  • Set up web application for anonymous users:
  • From CA / Application Management, select the web Application and click on Authentication Providers, then the default Zone
  • From the Edit Authentication form, select Enable Anonymous Access and click Save
  • Navigate to the site collection / Site Settings / Site Permissions and click the Anonymous Access button on the ribbon
  • Configure anonymous access for the Entire Web Site
  • Close the browser and start a browser anonymous

NOTE: When you set up anonymous access for the site, you will need the Require Use Remote Interfaces Permission checked

Results

When using the Search Center as anonymous, the clicks will not be computed.

Expected Results

Anonymous and non-anonymous clicks should be computed.

Cause

The permission UseRemoteAPIs is somehow disabled on Search Center web site.

Solution

Run the following script:

[ps]
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$webUrl = 'https://portal.contoso.com/searchcenter' # please replace this url by your search center web URL
try
{
$web = Get-SPWeb $webUrl
if($web.AnonymousPermMask64.ToString().Contains('UseRemoteAPIs'))
{
Write-Host "Your web '$($web.Url)' is all set. No change needed";
Write-Host "Effective rights for anonymous: $($web.AnonymousPermMask64)";
} else
{
Write-Host "Your web '$($web.Url)' does not include the appropriate rights to anonymous"
Write-Host "Current Effective rights for anonymous: $($web.AnonymousPermMask64)";
Write-Host "Adding the appropriate permission";
$web.AnonymousPermMask64 = $web.AnonymousPermMask64.ToString() + ", UseRemoteAPIs";
$web.Update();
Write-Host "New effective rights for anonymous: $($web.AnonymousPermMask64)";
}
$web.Dispose();
} catch
{
Write-Error "Error: Fix could not be applied"
Write-Error $Error[0]

}

[/ps]