Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
34 replies
Rhys

Where the heck do you collect data for analytics?

Attached is an excalidraw screenshot to hopefully explain this better, I'm struggling to figure out where to put analytics to be collected

In my application, there are a lot of sources that data can come in from, lets take a user changing their consent for example.

This event can come in from:

Discord Bot:
- Clicking a button on a solved message
- Agreeing to server post rules
- Agreeing to server rules
- Using a /consent command
- Clicking a button on the manage account menu
- Disabling Indexing of their messages

All of these call a common function in the Discord bot package which is just a wrapper for an api call to update the consent status

Web:
- Clicking a consent button on the web

Both the Discord bot and the website then call the api which is just a tRPC endpoint, the data they pass in is the source (what the cause of it was, i.e using the /consent command) and the new consent state along with the target user and server id

That api then calls my updateUserServerSettings function which just prepares the settings for being written and then writes it to prisma

The trouble with this, is because this can come in from all these different sources I'm not sure where to put the analytics collection event.

Lets just say I put it on the updateUserServerSettings function, I'm loosing the consent source, meta data about the server, the channel that it was in, etc. I could pass all that data in but then that becomes a bit crazy to do

If I put it in the api, it's a similar issue of losing data although I at least get the consent source now, however if I ever directly call updateUserServerSettings I miss this analytics event

If I put it in the Discord bot, since I'm still in that environment I'm able to collect a bunch of helpful data along with the analytics event such as applied tags, channel name, number of messages, etc, but then I need to make sure to also collect this analytics event on the web client, or duplicate collection on the api and db for redundancy

What's the best approach for collecting analytics from a variety of sources like this? Along with that, lets say I use the last approach where on each level there is an analytics capture event, would I want each one of those to be its own event name or would i want to use the same eventname list "update_user_consent" for all of them?

If anyone has recommended resources on this I'd love to check those out, I don't think this problem is so much a code architecture issue as it is just being new to analytics collecting and not knowing what is recommended

Thanks!


Just to add a bit more clarity, here is what the execution flow would look like:


Discord Bot:

User uses /consent
Bot handles this event and calls updateConsent()
updateConsent() creates a tRPC call and calls setConsentStatus

tRPC (API)
setConsentStatus() is called
setConsentStatus() validates permissions then calls updateUserServerSettings() in the db


DB
updateUserServerSettings() is called, parses the input, then returns the result of the Prisma update

I’m trying to figure out where in this flow of events I should put analytics, or each package shoudl get its own analytics call with all the possible data at each stage
image.png
Was this page helpful?