#!/usr/bin/env python # coding: utf-8 # In[91]: # read my matomo api token with open("matomo-token") as f: token = f.read().strip() # In[18]: url = "https://mybinder.org/matomo/index.php" # In[92]: import requests def api_request(method, **params): """Make a matomo API request""" data={ "token_auth": token, "format": "JSON", "module": "API", "method": method, "idSite": "1", } data.update(params) r = requests.post(url, data=data) try: r.raise_for_status() except requests.HTTPError as e: print(e) if r.status_code >= 500: # monthly queries seem to time out a lot # eventually, the result is cached and it responds quickly print("retrying") time.sleep(1) return api_request(method, **params) else: raise return r.json() api_request("API.getMatomoVersion") # In[70]: import pandas as pd months = {} # retry a bunch because it seems to time out the first time we request each month, # but eventually succeed # In[93]: for month in range(1, 11): if month not in months: date = f"2021-{month:02}-01" print(f"Fetching {date}") records = api_request( "UserCountry.getContinent", period="month", date=date, showColumns="label,nb_visits,sum_daily_nb_uniq_visitors", ) for record in records: record["date"] = date months[month] = records # In[102]: df.tail(10) # In[99]: from itertools import chain import altair as alt df = pd.DataFrame(chain(*months.values())) df.head() alt.Chart(df).mark_line().encode( color=alt.Color( "label", title="Continent", sort={"encoding": "y", "order": "descending"} ), x=alt.Y("date", title="Month"), y=alt.Y( "nb_visits", title="Monthly visits", scale=alt.Scale(type="log"), ), ).interactive() # And again, summing all non-NA-EU together, # ignoring "Unknown" # In[117]: gross = df[df.label != "Unknown"] gross["Gross Region"] = gross.label gross.loc[~gross.label.isin(["North America", "Europe"]), "Gross Region"] = "Rest of World" # regroup sum by gross region gross = gross.groupby(["date", "Gross Region"]).nb_visits.sum().reset_index() gross # In[118]: alt.Chart(gross).mark_line().encode( color=alt.Color( "Gross Region", sort={"encoding": "y", "order": "descending"} ), x=alt.Y("date", title="Month"), y=alt.Y( "nb_visits", title="Monthly visits", scale=alt.Scale(type="log"), ), tooltip=[ "Gross Region", "date", "nb_visits", ], ).interactive()