0

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.

enter image description here

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>();
        }
0

We have done this using PageEvents by passing the search query as parameter. Here is the code that works and registers the search events

public static bool TriggerSearchEventByQuery(string query)
    {
        if (!Tracker.IsActive)
        {
            Tracker.StartTracking();
        }

        if (!Tracker.IsActive || Tracker.Current.CurrentPage == null)
        {
            Sitecore.Diagnostics.Log.Warn($"Tracker current page is null", "xDBHelper");
            return false;
        }
        try
        {
            var searchEvent = Tracker.MarketingDefinitions.PageEvents[AnalyticsIds.SearchEvent.Guid];
            if (searchEvent == null)
            {
                Sitecore.Diagnostics.Log.Warn($"Search event not found", "xDBHelper");
                return false;
            }
            Tracker.Current.CurrentPage.Register(new PageEventData(searchEvent.Alias, searchEvent.Id)
            {
                Data = query
            });
            return true;
        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Warn($"Failed to trigger search event: {ex}", "xDBHelper");
        }
        return false;
    }

Hope this helps!

2
  • Thanks Mahesh, Does this show up somewhere in Sitecore console? Also, I see that the search term alone is being tracked. Does this mean we need to count occurences each time to display the top / popular search terms? Was there any performance impact on that? Would you have the code used to fetch the popular search terms?
    – Durga Bose
    Sep 21 at 21:31
  • Yes, it will register as internal search in your dashboard. Sitecore will display the results based on number times the keyword is searched. You don’t have to do anything manually after executing this code. Sep 21 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.