gapy: Getting Pageviews with Google Analytics using Python

gapy: A concise wrapper for Google Analytics Reporting API V4.

If you want to get pageviews and other statistics with Google Analytics and you really don’t want to do it in frontend. Guess what, getting it at backend is pretty simple. Source code can be found here.
Please refer following steps:

1. Get an account

First you need a Google Analytics account. Of course you can use your current Google account as well. Portal is here:

2. Add your webpage domain to your GA account

Once you have your account, you need to add your webpage domain or IP address to your account so that it can start tracking the pages’ statistics. Guide link is here:

3. Add analytics.js to your webpage source code

Refer this link:

Remember to replace the track ID of your own webpage created in step2. After this, you can relaunch your webpage to the server.Now if you go to and login with your account, you can getevery statistic about your webpage, including number of pageviews and visitors.

4. Get statistics from backend (Python)

If you are using Flask or Django, you can look at code and try to do the same thingat your own backend. This is very common if you building your own admin page for a databasemanagement system. Based on Google Analytics Reporting API v4, it also offers JAVA and PHP API.
Please follow this guide to enable and install this API:, you need to enable API, get your key, generate a JSON key file, and refer to the tracking ID in previous steps.
You can also see the exact sample there. However in gapy, you can use one function to obtain useful pageviews data. Have a try:

analytics = initialize_analyticsreporting()
print get_pageviews_array(analytics, '6daysAgo', 'today', 'day')
print get_pageviews_array(analytics, '27daysAgo', 'today', 'week')
print get_pageviews_array(analytics, '2017-05-01', 'today', 'month')
print get_pageviews_array(analytics, '2017-01-01', 'today', 'year')
print get_pageviews_array(analytics, '2017-01-01', 'today', 'total')

Output is something like this:

# per day
[['2017-07-04', '2017-07-05', '2017-07-06', '2017-07-07', '2017-07-08', '2017-07-09', '2017-07-10'], [1, 0, 1, 1, 0, 6, 4]] # per week [['2017-24', '2017-25', '2017-26', '2017-27', '2017-28'], [0, 11, 45, 4, 10]]
# per month
[['2017-05', '2017-06', '2017-07'], [0, 46, 24]]
# per year
[['2017'], [70]]
# in total
[[''], [70]]

Pretty simple right? Then you can send the result to your application admin page. I will write another article to write about how to present the collected data on your frontend using chartjs. Cheers!