Most Popular/Visited Articles in Sitecore Part-2


As we already discussed about API to get Most Popular/Visited Pages in last post. Here we will discuss how to manage the data for most visited pages by scheduler and calling the above API only one time in a day. If you haven’t read my last post, please click here to read.

At the beginning we decided to go with Sitecore scheduler to execute the API for same but after digging into some posts I found that if you want to run the Scheduler on specific time then Sitecore scheduler is not the recommended way to go with. There are many posts which suggest doing customization in current code and make the scheduler to work on specific time but still I found it inconsistent. So finally, we thought to go with windows scheduler on CM server.

We have created 3 below fields for same –

  1. Include templates for Most Popular Pages – Treelist field to set the data templates for which types of pages we want to get.
  2. Most Popular Items – TreelistEx field to store items after calculating by API.
  3. Number of Results – Number field to set number of results to show on page.

Below is the API we have used to execute by scheduler and store the most visited pages –

try
            {
                //  Calling the API which is given in previous post to get the most popular pages
                var mostVisitedPages = MostPopularPageEvents.GetMostVisitedPages();
                if (mostVisitedPages.Any())
                {
                    Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase(“master”);
                    if (master != null)
                    {
                        var siteItem = master.GetItem(“Site Item ID”);
                        if (siteItem!= null)
                        {
                            int pageCounts = 5;
                            List<Item> itemsToSaveInMostPopular = new List<Item>();
                            var mostVisitedItems = mostVisitedPages.Select(i => master.GetItem(new Guid(i).ToString())).Where(i => i != null);
                            if (siteItem.FieldHasValue(Templates.MostPopularPages.Fields.NoOfItems))
                            {
                                pageCounts = int.Parse(siteItem [Templates.MostPopularPages.Fields.NoOfItems]);
                            }

                            List<Item> templatesTypes = null;
                            if (siteItem.FieldHasValue(Templates.MostPopularPages.Fields.IncludeTemplates))
                            {
                                templatesTypes = siteItem.GetMultiListValueItems(Templates.MostPopularPages.Fields.IncludeTemplates).ToList();
                            }

                            foreach (var itm in templatesTypes)
                            {
                                if (itm != null)
                                {
                                    var templateSpecificItems = mostVisitedItems.Where(i => i.TemplateID.Equals(itm.ID)) ToList();
                                    if (templateSpecificItems.Any())
                                    {
                                        itemsToSaveInMostPopular.AddRange(templateSpecificItems.Take(pageCounts));
                                    }

                                }
                            }

                            using (new Sitecore.SecurityModel.SecurityDisabler())
                            {
                                using (new EditContext(siteItem))
                                {
                                    siteItem[Templates.MostPopularPages.Fields.MostPopularItems] = string.Join("|", itemsToSaveInMostPopular.Select(i => i.ID.ToString()));
                                }
                            }

                            LogManager.Diagnostic("Most Popular " + string.Join("|", itemsToSaveInMostPopular.Select(i => i.ID.ToString())), true);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogManager.Error("Error with Most Popular Scheduler" + ex.Message, true);
            }

With the help of above API we are able to get the most popular pages for specific template types also with specific count and storing them into Most Popular Items field of Sitecore. After the execution of this API we are publishing the item programmatically at the same time if everything works perfect.

Here we also made some updates if there is no data coming from the API (Because of any exceptions) then we are not updating the field value it use old data to display.

We have created windows scheduler to run the API everyday and the above API …. getting most popular pages and filtering the results specifically for the templates which we needed. And then storing only specific number of items which we need to show on website instead of storing large number of records.

There may be many other ways to handle the same but I am sharing what I did for my implementation.

Advertisements
This entry was posted in Reporting, Sitecore, Sitecore Analytics, Sitecore Reporting 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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s