How to Create A Most Popular List with Laravel and Google Analytics

Here on Laravel News, I wanted to generate a list of the most popular posts for the past seven days and display the results from most popular to least popular.

To solve this problem I thought of two solutions. The first is to build my own tracking system so I could keep a count and then use it for ordering. However, that could generate a huge amount of data and it seemed like a solution that an analytics tracking service could handle.

As I was fumbling through the Google Analytics API I found a Laravel Analytics package by Spatie.be that allows you to easily retrieve data from your Google Analytics account and it seemed like the best way to solve this problem. Let’s look at how I used it to generate a list of popular posts here on Laravel News.

Installation and Setup

The installation is typical for any Laravel package, you first install the package through composer:

Once that is setup the hardest part of the process begins and it’s no fault to the package. It’s just that Google has a crazy system in place for generating credentials. It will be best to visit this section of the readme as it steps through how to set this up complete with screenshots.

Fetching the most popular

The Analytics package makes fetching the data simple and here is an example for grabbing the most popular pages over the past seven days with a limit set:

$pages = Analytics::fetchMostVisitedPages(Period::days(7), $limit);

Then it returns a Laravel Collection of the results that look something like this:

If you notice the top two results are of the home page, then the second two are of the same post, and the reason these show twice is because the titles are different. One set is from before I changed the titles on the site and one set after.

Another issue is that I don’t want the home page to be in the list at all. I’d prefer this to be limited to posts only.

Let’s work through cleaning these up as we complete the integration.

Building A Wrapper Class

Now that I am getting results and I know I’m going to need to parse them I decided to create a class that will handle the fetching and the processing.

I created a new file named app/Services/Trending.php and stubbed it out with the basic fetching code:

Now calling this should give us the same results as before and here is an example call:

$trending = app('App\Services\Trending')->week();

Next, it needs another method to parse the results. As mentioned earlier it needs to remove any duplicates, remove any pages that are not actual posts, and also remove the “ – Laravel News” suffix on titles that Google is reporting on.

Because the results from the fetchMostVisitedPages is a Collection this allows us to easily utilize all its methods and here is the finished method:

If we step through this it first rejects any items with the url of “/“, or “/blog”, or if the url starts with “/category”. These are my three primary sections that generate enough traffic where they would show in the list.

Next, it calls, ->unique('url') to prevent any duplicate url’s from passing through.

It appears to be working, but notice that instead of the desired 15 items, these results only have 11. That’s because we are originally fetching the 15 top items, then rejected any duplicates. That causes us to get less than the fifteen we wanted.

The solution I came up with is to request more than I’m going to need and then splice off the number we really want. First, change getResults to grab more than needed:

With that all working it’s time to implement it into the view and I believe this would make a great candidate for a view composer.

Creating the View Composer

If you are not familiar with View Composers here is how the documentation describes them:

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

Because this data is only going to show in the sidebar and it could be called from any controller it’s a prime candidate for a view composer.

To setup this View Composer create app/Http/ViewComposers/PopularityComposer.php and add the following class: