GT4 Design + Web

Sharing ideas (and code!)

Sharing ideas (and code!)

Sharing ideas (and code!)

At GT4 Design + Web, I'm always looking to improve the sites that I work on.

Whether it's tweaking the design or improving pagespeed, I always have at least a dozen-or-so ideas kicking around inside my head at any given time.

Unfortunately, moreoften-than-not, most of these visions of grandeur never see the light of day, which is either due to time constraints or - most commonly - because they simply don't have the same impact on an application that I had hoped.

One such example, which didn't quite live up to my expectations, was the idea that I had for helping to improve the conversion rate on blog tags-clicked to articles-viewed within our very own website.  I proposed that one way of achieving this could be to show the number of articles associated with a particular tag, as web users may be more inclined to click on tags with more posts associated with it.

Whilst I was able to put together an appropriate solution - which I will get to in a minute - I was only able to partially achieve what I had set out to do.


The updated tag section on the blog page

Tracking the number of times a tagged appeared was fairly straightforward, but since the collection of blog articles was not as large or as varied as I would have liked, I just didn't feel like this new feature would help to improve click-throughs.

Ultimately, it was due to this that the new feature was put on the back-burner and currently remains there, until (or if) it ever becomes an official feature on the site.

So, whilst this didn't necessarily work in my own circumstances, I wanted to share my solution and general workings, in-case this fits your own needs - as I think that with a big enough library, it could work quite well.

Solution

In order to achieve this, I needed to split the process of implementing the use case into a series of logical, but necessary steps:

  • First of all, I had to group similar tags together
  • Then, I needed to count the number of items in the group
  • Next, I had to store both the name and number of items in the group as an new object
  • Lastly, it made sense to order these new objects based on their count, from highest to lowest

Implementation

The basic implementation to achieve the desired outcome looked something like this:

var results = tagList.GroupBy(x => x)
          .Select(x => new
          {
              Count = x.Count(),
              Name = x.Key
           })
          .OrderByDescending(x => x.Count);

It's probably worth mentioning at this point that this obviously isn't the full code, but instead, it just focusses around the task of counting tags properly. This should be enough to get you started with implementing it within your own projects or web site!

Breaking Down the Code

Let me break this code down for you, so you can see how it compares to the steps in my solution.

I mentioned that I should first group together similar tags from the list (which is appropriately named tagList, in this instance). In order to do this, I simply group the item by x, where x represents an item in the list, like so:

tagList.GroupBy(x => x)

Then, after succesfully grouping these tags, I needed to count the number of items in the group and store this along with the name of the grouped items as a new object. Both of these tasks were accomplished in conjunction with one another, as I created a new object that takes two parameters (Count and Name), which I assigned the appropriate values to - such as the number of items in group.

.Select(x => new
{
   Count = x.Count(),
   Name = x.Key
})

Lastly, all I had to was order the groups so that that tags appear based on their count, from highest to lowest.

.OrderByDescending(x => x.Count);

And that was it... as simple as that!

Twitter Feed