-
Notifications
You must be signed in to change notification settings - Fork 51
/
analytics.py
64 lines (48 loc) · 2.14 KB
/
analytics.py
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
'''
Analytics API Query
This script makes a GET request to the /searches endpoint on the Analytics REST API - https://www.algolia.com/doc/rest-api/analytics/.
To get the top 1000 searches over the last 7 days.
There is no API client for Analytics, so this script uses the Python Requests library to make the call.
'''
import requests
import csv
from os import getenv
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
# Get your Algolia Application ID, (analytics) API key, and Index name from the dashboard: https://www.algolia.com/account/api-keys
# Get your Algolia analytics domain here: https://www.algolia.com/infra/analytics
# Add these environment variables to a `.env` file:
ALGOLIA_APP_ID = getenv('ALGOLIA_APP_ID')
ALGOLIA_API_KEY = getenv('ALGOLIA_API_KEY')
ALGOLIA_INDEX_NAME = getenv('ALGOLIA_INDEX_NAME')
ANALYTICS_DOMAIN = getenv('ALGOLIA_ANALYTICS_DOMAIN')
# Initialise requests session
client = requests.Session()
# Set required session headers
client.headers = {"X-Algolia-API-Key": ALGOLIA_API_KEY, "X-Algolia-Application-Id": ALGOLIA_APP_ID}
# Set the index and limit parameters. You can also set params for startDate, endDate, orderBy and more - https://www.algolia.com/doc/rest-api/analytics/#get-top-searches
params = {'index': {ALGOLIA_INDEX_NAME}, 'limit': 1000}
# Make the call to the analytics endpoint
print('Fetching analytics data...')
try:
response = client.request("GET", f'{ANALYTICS_DOMAIN}/2/searches', params=params)
except requests.RequestException:
raise
if response.status_code != 200:
print(f'Problem with request: {response.status_code}. Exiting.')
exit()
print('Analytics data retrieved.')
content = response.json()
searches = content['searches']
print('Creating CSV file...')
# Create a csv file with the data
try:
keys = searches[0].keys()
with open(f'{ALGOLIA_INDEX_NAME}_top_1000_searches.csv', 'w') as f:
dict_writer = csv.DictWriter(f, keys, lineterminator='\n')
dict_writer.writeheader()
dict_writer.writerows(searches)
except Exception:
print('Error creating csv file.')
exit()
print(f'CSV file "{ALGOLIA_INDEX_NAME}_top_1000_searches.csv" created.')