-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplausible-stats
executable file
·106 lines (85 loc) · 3.67 KB
/
plausible-stats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""
Load site data from Plausible Analytics API and print to stdout.
Input is a TSV file containing the name of the site and its Plausible domain
"""
import argparse
import datetime
import json
import os
import sys
from collections import OrderedDict
import csv
import requests
try:
PLAUSIBLE_KEY = os.environ['PLAUSIBLE_KEY']
except KeyError:
print("Please set PLAUSIBLE_KEY environment variable")
sys.exit(1)
FIELDS = ["visitors", "visits", "pageviews", "views_per_visit", "bounce_rate", "visit_duration"]
START = '2023-06-01'
def geo(args, sites: OrderedDict):
"""Load geo data"""
session = requests.session()
session.headers = {'Authorization': "Bearer " + PLAUSIBLE_KEY}
data = OrderedDict()
for short_name, domain in sites.items():
name = f"{short_name} Geo"
# parse the month of the given start date:
start = datetime.datetime.strptime(args.start, "%Y-%m-01")
# get a list of months from start to the current date:
months = [start + datetime.timedelta(days=31 * i) for i in range(0, (datetime.date.today().year - start.year) * 12 + datetime.date.today().month - start.month + 1)]
# get the data for each month:
data[name] = OrderedDict()
first_of_months = [m.strftime("%Y-%m-01") for m in months]
country_data = OrderedDict()
# in order the get all available countries we need to collect all the
for i, fom in enumerate(first_of_months):
r = session.get('https://plausible.io/api/v1/stats/breakdown', params=dict(
site_id=domain,
period='month',
property='visit:country',
date=fom
))
results = r.json()['results']
for result in results:
country_code = result['country']
visitors = result['visitors']
if country_code not in country_data:
country_data[country_code] = {fom: visitors}
else:
country_data[country_code][fom] = visitors
# reformat the results:
data[name] = [["country"] + [m.strftime("%Y-%m") for m in months]]
for country_code, visitors in country_data.items():
data[name].append([country_code] + [visitors.get(fom, 0) for fom in first_of_months])
json.dump(data, fp=sys.stdout, indent=2)
def visits(args, sites: OrderedDict):
"""Load all site data"""
session = requests.session()
session.headers = {'Authorization': "Bearer " + PLAUSIBLE_KEY}
data = OrderedDict()
for name, domain in sites.items():
r = session.get('https://plausible.io/api/v1/stats/timeseries', params=dict(
site_id=domain,
period='custom',
metrics=','.join(FIELDS),
date=args.start + "," + datetime.date.today().isoformat()
))
data[name] = [["date"] + FIELDS]
data[name].extend([[i["date"]] + [i[field] for field in FIELDS] for i in r.json()['results']])
json.dump(data, fp=sys.stdout, indent=2)
if __name__ == "__main__":
# Parse arguments
parser = argparse.ArgumentParser(description='Load site data from Plausible Analytics API and print to stdout.')
parser.add_argument('-g', '--geo', action='store_true', help="Extract geospatial data for months to date")
parser.add_argument('-s', '--start', default=START, help="Start date for data as YYYY-MM-DD")
args = parser.parse_args()
# Load SITES from TSV file passed via stdin
sites = OrderedDict()
for row in csv.reader(sys.stdin, delimiter="\t"):
sites[row[0]] = row[1]
if args.geo:
geo(args, sites)
else:
visits(args, sites)