-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-stats.py
More file actions
executable file
·79 lines (73 loc) · 3.03 KB
/
Copy pathemail-stats.py
File metadata and controls
executable file
·79 lines (73 loc) · 3.03 KB
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
#!python3
import os
import requests
from datetime import datetime, date
from tabulate import tabulate
import argparse
def main():
parser = argparse.ArgumentParser(
description ='Generate Email statistics report',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-t', "--token",
dest = 'anApiToken',
action = 'store',
required = False,
default = None,
help = 'Action Network API token (also can be set in the AN_API_TOKEN environment variable).'
)
parser.add_argument(
'-a', "--abbreviate",
dest = 'abbreviateTitle',
action = 'store_true',
required = False,
default = False,
help = 'Whether to abbreviate Title to 50 characters.'
)
args = parser.parse_args()
if args.anApiToken:
anApiToken = args.anApiToken
else:
anApiToken = os.getenv('AN_API_TOKEN')
if not anApiToken:
print("Please set the AN_API_TOKEN environment variable or pass it with the -t option.")
exit(1)
headers = {
"OSDI-API-Token": anApiToken
}
API_BASE_URI = "https://actionnetwork.org/api/v2"
# Loop through all the messages and find those with a status of "sent"
uri = "/messages"
moreData = True
messageCnt = 0
print(f'-- Sent Email statistics as of {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n')
messageStats = []
while moreData:
try:
response = requests.get(f"{API_BASE_URI}{uri}", headers=headers)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print("Error calling Action Network API: {}".format(e))
exit(1)
data = response.json()
for message in data['_embedded']['osdi:messages']:
if message['status'] == 'sent':
messageCnt += 1
messageSentDate = datetime.strptime(message['sent_start_date'], '%Y-%m-%dT%H:%M:%SZ')
subject = message['subject'] if not args.abbreviateTitle else (message['subject'][:50] + ("..." if len(message['subject']) > 50 else ""))
numTargets = message['total_targeted']
statistics = message['statistics']
verifiedOpens = statistics['verified_opens'] if 'verified_opens' in statistics else 0
machineOpened = statistics['machine_opened'] if 'verified_opens' in statistics else 0
clicks = statistics['clicked'] if 'clicked' in statistics else 0
unsubscribed = statistics['unsubscribed'] if 'unsubscribed' in statistics else 0
messageStats.append((subject, messageSentDate, numTargets, verifiedOpens + machineOpened, verifiedOpens, machineOpened, clicks, unsubscribed))
if data['page'] >= data['total_pages']:
moreData = False
else:
uri = f"/messages?page={data['page'] + 1}"
print(tabulate(messageStats, headers=["Subject", "Date Sent", "# of Targets", "Total Opened", "Verified Opens", "Machine Opened", "Clicks", "Unsubscribed"]))
print("\n-- Total sent messages: " + str(messageCnt))
if __name__ == "__main__":
main()