I am using Sitecore 9.3 and trying to track and display the popular search terms used on the website. However, I am not seeing the search keywords being tracked as intended in Internal search dashboard. Any ideas on what I am missing here?
public static void TrackSearch(string searchTerm)
{
if (Tracker.Current != null && Tracker.Current.IsActive)
{
var pageEventData = new PageEventData("Search", new Guid(Constants.Items.SystemSearchEvent))
{
ItemId = new Guid(Constants.Items.Search),
Data = searchTerm,
DataKey = searchTerm,
Text = searchTerm
};
var interaction = Tracker.Current.Session.Interaction;
if (interaction != null)
{
interaction.CurrentPage.Register(pageEventData);
}
}
}
Constants.Items.SystemSearchEvent
is the guid corresponding to /sitecore/system/Settings/Analytics/Page Events/Search
I am not seeing anything getting tracked here, even though the above code is getting executed successfully, and there are no errors in the logs file.
I am trying to use the following code to fetch the tracked searches, but it is not returning anything, since nothing seems to be getting tracked:
public static List<string> GetTopSearchQueries()
{
try
{
var reportingService = ApiContainer.Repositories.GetReportingService();
var reportQuery = GetReportQuery();
ReportResponse reportResponse = reportingService.RunQuery(reportQuery);
var encoder = ApiContainer.GetReportResponseEncoder();
var result = encoder.Encode(reportResponse);
if (result != null && result.Data != null && result.Data.Localization != null
&& result.Data.Localization.Fields != null && result.Data.Localization.Fields.Any())
{
var searchFields = result.Data.Localization.Fields.FirstOrDefault();
if (searchFields != null)
{
return result.Data.Localization.Fields.FirstOrDefault()
?.Translations.Select(r => r.Value).ToList();
}
}
}
catch (Exception ex)
{
Log.Info(ex.Message, ex);
}
return new List<string>();
}