W
Wasp-lang•4mo ago
haarismian

What is the best place to initialize tracking on the backend

What is the best place to initialize tracking on the backend
6 Replies
kapa.ai
kapa.ai•4mo ago
Hi @haarismian. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The provided knowledge sources do not specify where to initialize tracking on the backend in Wasp. However, Wasp does support background jobs for tasks that need extra time to process, which might be relevant depending on your specific use case. You can learn more about this feature in the Wasp documentation (https://wasp-lang.dev/docs/advanced/jobs). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @haarismian, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
haarismian
haarismianOP•4mo ago
@kapa.ai what is the best place to initialize analytics client on the backend, for further context I am using posthog, and it must be initialized using the following code, what is the best place to do so, right before making the posthog capture call?: import { PostHog } from 'posthog-node' const client = new PostHog( <API_KEY>, { host: 'https://us.i.posthog.com' } ) await client.shutdown()
kapa.ai
kapa.ai•4mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The provided knowledge sources do not specify where to initialize the PostHog client on the backend in Wasp. However, it's generally a good practice to initialize analytics clients in the setup phase of your application, so they are ready to use when you need to capture events. In Wasp, you can use the setupFn function to run any code you want before everything else on the client side. This might be a good place to initialize your PostHog client. Here's an example of a setup function from the Wasp documentation (https://wasp-lang.dev/docs/project/client-config#setup-function):
export default async function mySetupFunction() {
let count = 1
setInterval(
() => console.log(`You have been online for ${count++} hours.`),
1000 * 60 * 60
)
}
export default async function mySetupFunction() {
let count = 1
setInterval(
() => console.log(`You have been online for ${count++} hours.`),
1000 * 60 * 60
)
}
You can replace the console.log call with your PostHog client initialization code. However, please note that this is a client-side setup function, and it might not be suitable for your use case if you need to initialize the PostHog client on the server side. For server-side initialization, you might want to consider doing it in the main file of your server application, or in a separate module that exports the initialized client, so you can import it wherever you need to use it. Unfortunately, the provided knowledge sources do not include specific instructions or examples for server-side setup in Wasp. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @haarismian, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Filip
Filip•4mo ago
The bot almost got it right. You can do this in the server setup function. That thing gets executed as soon as the server starts running: https://wasp-lang.dev/docs/project/server-config @haarismian Does this answer your question? 🙂
haarismian
haarismianOP•4mo ago
@sodic yes it does thank you!
Filip
Filip•4mo ago
Glad I could help!
Want results from more Discord servers?
Add your server