How to set up your top stories widget without hitting site speed

The ‘Top Stories’ or ‘Trending articles’ widget  is ubiquitous on digital publishing websites because it’s a great way to increase the reach and engagement of popular content. 

Real-time traffic data can be used to automate these widgets, but without the right setup, it can slow down your site.

Here’s how a custom WordPress integration using WP-Cron gets around that. 

What automation options do publishers have for promoting content?

Google Analytics is the usual source of truth for content popularity data. And it’s possible to automate content promotion by simply connecting your widget to the Google Analytics API on every page load. But if you’re publishing a high volume of articles, this will slow down your site on uncached pages. 

READ MORE: Fast & Easy: 7 Ways to Boost Website Performance

More often, publishers opt for a plugin like Jetpack’s Top Posts and Pages. This is a reasonable solution if you have more basic content promotion needs. It allows you to display your most liked posts, or up to ten posts that have received the most traffic in the past 48 hours. 

Jetpack Top Post and Pages plugin for digital publishers
Jet Pack ‘Top Posts & Pages’

However, like most out-of-the-box tools, your customisation options are limited. Because results are selected purely on popularity, it’s possible for old and out-of-date content to surface in your feed  if it’s re-shared to a large audience or for some reason goes viral.

READ MORE: Ad tech: Should you build  a custom integration or use an ad management plugin?

How to automate the ‘Top Stories’ widget using Google Analytics API and cron 

For faster automation it’s better to use Cron.

Cron is the most efficient way to automate repetitive or scheduled tasks and serves as a ‘helper code’ working in tandem with Google Analytics’ existing code without impact site speed.

Here’s how we build a custom Google Analytics integration using Cron:

1. Use the Google PHP Client Library to access the Google Analytics API.

Using a client library reduces the amount of code you need to write and is more accurate than making raw requests.

Google recently launched a new ‘next generation’ version of Google Analytics (GA4), which uses events instead of session-based data. If you’re still using Universal Analytics, you have until 1st July 2023 to migrate to GA4. 

These are the libraries we’re currently using for digital publishing clients:

Google Analytics Data for PHP

Google APIs Client Library for PHP

2. Determine clear and precise parameters for your Top Stories widget.

For example, do you want to promote stories with the highest traffic regardless of publication date? Or do you want to restrict the selection to stories published in the last 7 days, 24 hours, 3 hours etc?

3. Set up a cron task

Pass the URLs of all published content through the API library at your desired cadence. Google has rate limits and the shortest interval available is 60 seconds. However,  we typically advise publishers to schedule cron runs for hourly, 30 or 15 minute intervals.   

4. Power your content with real-time traffic data

Use this Google Analytics automation throughout your owned media to increase the reach and exposure of popular content.

Examples of data-powered content promotion

While this method of automation requires some custom development work, the payoff of a more lightweight, faster site is well worth the extra effort. 

The Code Company has  used the Cron method of Google Analytics integration to engineer automated content promotion solutions for numerous digital publishers. 

Data-driven newsletters

Aside from basic ‘trending stories’ widgets, publishers can use popularity data to determine which stories feature in emails.

Our team developed a custom admin panel using Cron that collates the top 20 most viewed articles, ordered by popularity.  The editor can then easily select which 7 articles to publish in the newsletter by clicking the ‘Add Article’ button.

The newsletter can then be sent directly from WordPress to their email service provider (ESP).

Many use popularity metrics, but Google Analytics can also be engineered to promote content based on activity metrics such as comments, shares or conversions.

Final thoughts on data integration

Building internal data products is crucial to democratising access to analytics

The Benefits and Risks of Media Data Democratisation

Powering widgets with real-time data is a great way to efficiently automate your promotion efforts. But it shouldn’t come at the cost of your site performance.

Given the limitations of ‘Top Posts’ plugins, a custom integration with the Google Analytics API and Cron is your best option.

Anthony Thorne

Anthony is a full stack WordPress engineer at The Code Company. He works with clients on projects and retainer engagements. Anthony has worked on large Chargify and other payment related API services.