Analysis of a WordPress Analytics Plugin

Ciprian on Thursday, November 24, 2022 in WordPress

NEW! Learn JavaScript by example. Code snippets, how-to's and tutorials. Try now!

A few weeks ago, I was about to take over an abandoned analytics plugin. The deal didn’t go through, but I got the chance to look at some of the open issues and bugs raised by users. Most of them were centered around the slowness of the plugin after various periods of time. Others were about slow page speed for their visitors.

Now, all these analytics plugins for WordPress store their data locally, as in the same database as WordPress itself. After a while, depending on the amount of data saved in each row, the database will grow and become sluggish.

I have my own analytics plugin, used internally for all my clients. I have recently started to promote it on various social networks and communities, and also did a bit of work on it to optimize the codebase and its functionality.

Active Analytics

Let’s see how Active Analytics works.

Table of Contents

Data Recording

Active Analytics records every user’s page view. That is every page view coming from a real user, and not a bot, a crawler, a spider or a CRON job. The data is saved asynchronously, and it contains the bare minimum.

One thing I miss in the new Google Analytics (GA4) is the main traffic chart. I don’t need much, I need to see the evolution of my website’s traffic (unique users, impressions, sessions) over the past 30 days. And this is what ALL my clients need to see. 99% of website owners, or small business owners, DO NOT NEED the new GA4. I stopped using it a long time ago, and started recording my own data, the way I require it. And I don’t need much.

So, to reiterate, saving data is done using JavaScript, asynchronously, using native code with no dependencies. The actual code is less than 1 KB → 648 bytes gzipped.

Data Storage

The amount of data stored for each page view is paramount for a local plugin. Basically, I need to be able to build useful charts using minimal data. What I need is the traffic evolution on a daily basis, and I need to see how many unique users, page views, and sessions I had. I can then build a chart spanning over the past 30 days, 60, 90 or as much as I want (and it fits the screen).

I also need to know where my users came from, what search engines they used, or which website referred them.

The same JavaScript code above will tell me if the device is a desktop or a mobile – I don’t need more than that. Tablets nowadays are either similar to a mobile phone or to a desktop. Also, I don’t need to know if it’s Android or iPhone. Mobile is enough.

Again, the same JavaScript code above will filter out bots, crawlers, or spiders. It might miss some, either due to ancient, legacy browsers, or, who knows, a DDoS attack.

No Cookies, No Cry

How do I save sessions without cookies? All browsers nowadays use the localStorage API, so that’s where I store my data, and I also remove it when it expires (yes, it requires another localStorage value to save the expiry date). The upside is that it’s fast, and it’s private. Cookies are more susceptible to “sniffing” than localStorage items.

The localStorage read-only property of the window interface allows you to access a Storage object for the Document‘s origin; the stored data is saved across browser sessions.

localStorage is similar to sessionStorage, except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed. (localStorage data for a document loaded in a “private browsing” or “incognito” session is cleared when the last “private” tab is closed.)

MDN Web Docs

Data Retention

This is another aspect of efficiently storing data.

Do I need analytics older than 3 years? Two? One? Probably not. Lots of things change in 3 years. I pivoted twice, and I changed from a developer blog to a WordPress plugin marketplace, then back to a developer blog, then back to a WordPress plugin marketplace (Envato this time).

If I’m being honest, I only need the last 90 days. If I want historical data, I always have Google Search Console.

Dogfooding

I use my own plugin for my own websites. I use it for my clients’ websites.

Active Analytics has become a daily necessity, and the data is available at a glance with the most relevant information anyone would need.

Related posts

Leave a Reply