Calculating the mean average age of your users from Google Analytics demographic data (using Python)

12 January 2020

If you use Google Analytics, you may be aware that you can enable it to collect demographics and interests data on your users - or at least on a subset of them. The demographics data includes 'age of user', which is broken down by age group from '18-24' to '65+' (ages below 18 are not included in the data). For example:

Age data from Google Analytics

This data allows you to compare the different behaviours of the various cohorts. For example, do your younger users spend longer on your site than your older users? Do they use different marketing channels or complete different events?

However what the data does not tell you is the average age of your user (by which I mean the mean). This can be useful to know, especially when communicating to non-technical or non-data-savvy stakeholders. I work in publishing, and telling a journalist that there are more users in the 35-44 age group than any other is perhaps less helpful than telling them that their average reader is 42 years old.

(To find the age report, go to Audience > Demographics > Age in the Google Analytics reports menu, or just tap in 'Age' into the search box at the top of GA.)

Keeping it mean

So is it possible to use the Google Analytics age demographics data to calculate a mean average? Yes, albeit with an awful lot of caveats.

Let's take the first age group, 18-24. The users in this age group could all be at the bottom end of the range (18), all at the top end (24), or have any other distribution in between these two extremes. So the mean age of this cohort could equally be anywhere between 18 and 24. It is impossible to say for certain, as Google Analytics does not provide any further granularity.

What if we assumed that the mean age of this cohort was at the exact mid-point of the range - namely, 21? And did the same for all the other cohorts? We could then come up with a mean average for the entire set of users.

Well, almost. The final age range is '65+', which means it has no upper bound and hence no mid-point. What is the average age of users in this category? It's hard to say as it very much depends on the nature of your site. I would expect that a pure-play B2B site could skew quite closely to 65 as retired users would have little reason to visit. A consumer site targeting older users would be very different.

But if you were to decide on a mean age for this cohort, you could then go ahead and calculate a very provisional mean age for your site users as a whole (excluding minors).

Out of curiosity, I did this for one of the sites I work on (assuming an average age for the 65+ cohort of 70). The results were certainly interesting. The cohort with the most users was '25-34', but the mean average across the site was 44. This suggests a long tail of older users and could encourage the journalists not to focus quite so much on their millennial readers.

It's also worth noting that the various age ranges don't all cover the same number of years. The 18-24 bracket covers seven years whereas the other all cover 10 years, with the exception of 65+. Working out a mean across the entire data set is perhaps some way to compensate for this.

Working it out in Python

I've started learning Python recently, so took the opportunity to write a small script that calculates the mean average for you. You will still need to look up and enter the relevant figures from Google Analytics, of course, but Python will do the maths.

I've embedded the script below - just hit the 'play' button then follow the prompts in the black box. If for any reason this isn't working for you, then trying going to where it is hosted on and hitting the 'Run' button in the top menu of that page.

And please look kindly on my somewhat basic Python! Oh, feel free to change any of the variables, such as the mean age of users in the '65+' bracket (currently set to 70).

Notify of
Inline Feedbacks
View all comments
James Clark
Hi! I'm James Clark and this is my website. I publish all my guides completely free, so if you find them useful then please:
Copyright © James Clark
What do you think? Leave a commentx
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram