Track search Keywords on Sitecore website


While working on Sitecore search we got a requirement where we need to store keyword s which is visitor will search in our website. First I thought to store them into Sitecore Database by creating items. But once I started looking into analytics feature I got that Sitecore analytics provides feature to store Site searches (Keywords). And also it provide more feature after storing them.

So I planned to use the analytics to store our site search keywords into analytics.

I created below function to store the keyword into sitecore analytics database  –


public bool IsActive => Tracker.Current != null && Tracker.Current.IsActive;

 public virtual void TrackSiteSearch(Item pageEventItem, string query)
        {
            Assert.ArgumentNotNull(pageEventItem, nameof(pageEventItem));
            Assert.IsNotNull(pageEventItem, $"Cannot find page event: {pageEventItem}");
            if (this.IsActive)
            {
                var pageEventData = new PageEventData("Search", [Search Event Item ID])
                {
                    ItemId = pageEventItem.ID.ToGuid(),
                    Data = query,
                    DataKey = query,
                    Text = query
                };
                var interaction = Tracker.Current.Session.Interaction;
                if (interaction != null)
                {
                    interaction.CurrentPage.Register(pageEventData);
                }
            }
        }

[Search Event Item ID] – It is the item id of the search event item created on /sitecore/system/Settings/Analytics/Page Events/Search location of Sitecore.

After creating this function you just need to call the function after getting the search results as below –

  if (!string.IsNullOrWhiteSpace(query) && results.Any())
     {
          TrackSiteSearch(ContextItem.Item, query.QueryText);
     }

This will store keyword into analytics database whenever visitor will perform search.

Where we can see the stored keywords in Sitecore analytics reports –

1. Open the ExperienceAnalytics dashboard from Launch pad as given in below screenshot

launch pad.png

2. Navigate to Behavior section and click on Internal search it will show complete report of searched keywords with all other analytics details as shown below.

 

launch pad2

 

In next post we will discuss how to show top searches keywords in site.

 

Advertisements
This entry was posted in Sitecore, Sitecore Analytics and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s