Skip to content

Commit e801d72

Browse files
committed
Add PostHog
1 parent 25607d7 commit e801d72

6 files changed

Lines changed: 60 additions & 1 deletion

File tree

.template.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ WOLFRAM_ALPHA_APP_ID=
1515
SENTRY_DSN=
1616
SENTRY_AUTH_TOKEN=
1717
SENTRY_ORG=
18+
POSTHOG_API_KEY=
19+
POSTHOG_HOST=

configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def get(self, key: str) -> Any:
120120
Setting(
121121
'disco_max_file_size_in_mib', description='Maksymalny rozmiar pliku utworu disco', unit='MiB', default_value=16
122122
),
123+
Setting('posthog_api_key', description='Klucz API PostHog', optional=True),
124+
Setting('posthog_host', description='Host PostHog', default_value='https://us.i.posthog.com'),
123125
)
124126

125127
configuration = Configuration(SETTINGS)

docker-compose.dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ services:
5757
- CLICKHOUSE_USER=somsiad
5858
- CLICKHOUSE_PASSWORD=somsiad
5959
- CLICKHOUSE_DATABASE
60+
- POSTHOG_API_KEY
61+
- POSTHOG_HOST
6062
- REDIS_URL=redis://redis:6379/
6163
web:
6264
build: web

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ services:
3232
- CLICKHOUSE_USER
3333
- CLICKHOUSE_PASSWORD
3434
- CLICKHOUSE_DATABASE
35+
- POSTHOG_API_KEY
36+
- POSTHOG_HOST
3537
- REDIS_URL=redis://redis:6379/
3638
web:
3739
build: web

plugins/commands.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import discord
1515
from core import Invocation, is_user_opted_out_of_data_processing
1616
import datetime as dt
17+
import posthog
1718
from somsiad import Somsiad, SomsiadMixin
1819

1920
from discord.ext import commands
@@ -41,6 +42,21 @@ async def on_command(self, ctx: commands.Context):
4142
)
4243
session.add(invocation)
4344

45+
# Track command invocation in PostHog
46+
posthog.capture(
47+
distinct_id=str(ctx.author.id),
48+
event='command_invoked',
49+
properties={
50+
'command': ctx.command.qualified_name,
51+
'root_command': ctx.command.root_parent or ctx.command.qualified_name,
52+
'prefix': ctx.prefix,
53+
'server_id': ctx.guild.id if ctx.guild else None,
54+
'server_name': ctx.guild.name if ctx.guild else None,
55+
'channel_id': ctx.channel.id,
56+
'is_dm': ctx.guild is None,
57+
}
58+
)
59+
4460
@commands.Cog.listener()
4561
async def on_command_completion(self, ctx: commands.Context):
4662
with data.session(commit=True) as session:
@@ -50,6 +66,19 @@ async def on_command_completion(self, ctx: commands.Context):
5066
if invocation is not None:
5167
invocation.exited_at = dt.datetime.now()
5268

69+
# Track command completion in PostHog
70+
duration_ms = (invocation.exited_at - invocation.created_at).total_seconds() * 1000
71+
posthog.capture(
72+
distinct_id=str(ctx.author.id),
73+
event='command_completed',
74+
properties={
75+
'command': ctx.command.qualified_name,
76+
'root_command': ctx.command.root_parent or ctx.command.qualified_name,
77+
'duration_ms': duration_ms,
78+
'server_id': ctx.guild.id if ctx.guild else None,
79+
}
80+
)
81+
5382
@commands.Cog.listener()
5483
async def on_command_error(self, ctx: commands.Context, error: commands.CommandError):
5584
with data.session(commit=True) as session:
@@ -58,9 +87,23 @@ async def on_command_error(self, ctx: commands.Context, error: commands.CommandE
5887
invocation = session.query(Invocation).get(ctx.message.id)
5988
if invocation is not None:
6089
invocation.exited_at = dt.datetime.now()
61-
invocation.error = text_snippet(
90+
error_message = text_snippet(
6291
str(error).replace('Command raised an exception: ', ''), Invocation.MAX_ERROR_LENGTH
6392
)
93+
invocation.error = error_message
94+
95+
# Track command error in PostHog
96+
posthog.capture(
97+
distinct_id=str(ctx.author.id),
98+
event='command_error',
99+
properties={
100+
'command': ctx.command.qualified_name if ctx.command else 'unknown',
101+
'root_command': ctx.command.root_parent or ctx.command.qualified_name if ctx.command else 'unknown',
102+
'error_type': type(error).__name__,
103+
'error_message': error_message,
104+
'server_id': ctx.guild.id if ctx.guild else None,
105+
}
106+
)
64107

65108

66109
async def setup(bot: Somsiad):

somsiad.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import aiohttp
4040
import discord
41+
import posthog
4142
import psutil
4243
import sentry_sdk
4344
import aiochclient
@@ -211,6 +212,13 @@ def __init__(self):
211212
self.system_channel = None
212213
self.public_channel = None
213214

215+
# Initialize PostHog if configured
216+
if configuration.get('posthog_api_key'):
217+
posthog.api_key = configuration['posthog_api_key']
218+
posthog.host = configuration['posthog_host']
219+
else:
220+
posthog.disabled = True
221+
214222
async def on_ready(self):
215223
psutil.cpu_percent()
216224
localize()

0 commit comments

Comments
 (0)