SharePoint - Popularity Trends Not Working As Expected

Problem Description

Popularity Trends Not Working as Expected in SharePoint 2013 On-Premise Environment.
If all the reports are showing data but you are not getting accurate results, the below steps will help you to resolve the issue. And if you're getting 0 results, it's pretty much the same troubleshooting steps. 
A few things to check first from below:
  • First make sure your SharePoint Farm is up to date.  Check Manage Servers in Farm from Central Admin and make sure all the servers say No Action Required.  If the SarePoint Server says Upgrade Available please make sure you install the latest CU and run PSConfig Wizard in all the SharePoint Servers.
  • Make sure Search Service Application is configured properly in your Central Admin and that it is connected to your problematic web application by checking from the service connections in Central Admin.
  • And also make sure your crawl is running successfully
  • Because Usage Analytics is part of Search Servcie Application from SharePoint 2013 Onwards, check that the Usage and Health Data service is up and running.
  • Make sure proper events are selected (Analytics Usage, Feature Use, and Page Requests) in the Usage and Health data services.
  • Check that the Usage Health data service is connected with the web applciation.
  • Make sure Microsoft SharePoint Foundation Usage Data Import and Microsoft SharePoint Foundation Usage Data Processing timers jobs are running.
  • Check the SharePoint ULS Logs to check proper logs files are being generated.
  • Check for the Event Viewer logs as well.

Troubleshooting steps to resolve the issue

After running the first script from below it returns with no results. We found out the Analytic Timer Jobs on both the Publishing and Consuming servers did not exist. And then we used the second, third and fourth scripts to create the Analytics Timer Job manually along with the job definition types, and then we registered the Analytics Timer Job. All these steps are done by running  the second, third and fourth scripts.
We also ran couple of other scripts to check the value of Analytics Timer Job and then we cleared Analytics information from Search and Usage and performed the usage import timer job as well. Upon running all those scripts from below give it at least a day to see the results from the reports.
Step 1
Check if the search settings were configured properly on the site and also the search center. 
Run the following commands as below:
  1. $ssa = Get-SPEnterpriseSearchServiceApplication  
  2. $site = Get-SPSite <site url>  
  3. $result = $ssa.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$site.ID,[System.Guid]::Empty)  
  4. $filterdate = Get-Date "yyyy-mm-dd"  
  5. $result 
If the above commands return no results then move on to Step 2. 
Step 2
After further troubleshooting, we found out that the Analytic Timer Jobs on both the Publishing and Consuming servers did not exist.
We created them manually using the following  PowerShell script,
  1. $ssa = Get-SPEnterpriseSearchServiceApplication  
  3. $assembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")  
  5. $bindingflags = [System.Reflection.BindingFlags]::NonPublic, [System.Reflection.BindingFlags]::Static  
Step 3
Analytics job definition types,
  1. $jobtypes = @("Microsoft.Office.Server.Search.Analytics.UsageAnalyticsJobDefinition",  "Microsoft.Office.Server.Search.Analytics.AnalyticsJobDefinition")  
Step 4
Register analytics timer jobs,
  1. foreach ($jobtype in $jobtypes)  
  3.     {  
  5.         $type = $assembly.GetType($jobtype)  
  7.         $registerJob = $type.GetMethod("RegisterJob"$bindingflags)  
  9.         $registerJob.Invoke($type$ssa)  
  11.   }   
We also ran the following commands as below,
Step 5
Check values,
  1. $tj= Get-SPTimerJob -Type Microsoft.Office.Server.Search.Analytics.AnalyticsJobDefinition   
  2. $sa = $tj.GetAnalysis("Microsoft.Office.Server.Search.Analytics.SearchAnalyticsJob")  
  3. $sa   
Step 6
After this, clear Analytics information for Search and Usage,
  1. $ua = Get-SPTimerJob -Type Microsoft.Office.Server.Search.Analytics.UsageAnalyticsJobDefinition  
  2. $ua.DisableTimerJobSchedule()  
  3. $ua.StopAnalysis()  
  4. $ua.ClearAnalysis()  
  5. $ua.EnableTimerJobSchedule()   
Step 7
Force Usage Analytics to run, it cannot return null,
  1. $tj = Get-SPTimerJob -Type Microsoft.Office.Server.Search.Analytics.AnalyticsjobDefinition  
  2. $sa = $tj.GetAnalysis([Microsoft.Office.Server.Search.Analytics.SearchAnalyticsJob])  
  3. $sa.StopAnalysis()  
  4. $sa.ClearAnalysis()  
  5. $sa.StartAnalysis()   
  6. $sa.GetAnalysisInfo()   
  8. $ua = Get-SPTimerJob -Type Microsoft.Office.Server.Search.Analytics.UsageAnalyticsJobDefinition  
  9. $ua.DisableTimerJobSchedule()  
  10. $ua.StartAnalysis()  
  11. $ua.EnableTimerJobSchedule()   
Step 8
Run the usage import timer job as well,
  1. $tj = Get-SPTimerJob -Identity ("job-usage-log-file-import")  
  2. $tj.RunNow()    
Not exactly sure what caused this issue, I'm guessing patching might be the culprit.
Just FYI - Upon running all those commands from the top make sure you give it  24 hours to display your results and then everything will be working as expected. :)