Adding GA events tracking
Hi,
Is there a way to use the existing Google Analytics integration done through the Cookie Consent function to track specific event ? How can I do that ?
6 Replies
Hi @Glide Orchestra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Wohooo @Glide Orchestra, you just became a Waspeteer level 1!
----------------------
I'm sorry, but the knowledge sources provided do not contain information about integrating Google Analytics event tracking with Wasp, nor do they mention a "Cookie Consent function" related to Google Analytics or event tracking.
The available sources discuss how to set up Google OAuth for authentication in Wasp, and there are some community discussions about injecting Google Analytics scripts into Astro-based blogs (as used in the OpenSaas template), but they do not cover tracking specific events or integrating with a cookie consent mechanism for analytics purposes.
If you need to track specific events with Google Analytics in a Wasp app, the knowledge sources do not provide explicit guidance on how to do this. You may need to refer to Google Analytics documentation or general React/JavaScript event tracking practices for more information. If you have more details or a specific context (such as the OpenSaas template or Astro blog), please clarify, and I can check if there is any relevant information in the sources.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @Glide Orchestra, 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?
Hey @Glide Orchestra , could you provide more info about what kind of events you want to track? Do you want to get some custom events in Google Analytics?
Hey @NEROX, i try to track specific actions that a visitor performs on the app. I was thinking using a function of this kind to send GA events :
And I modified Config.ts (where GA is initialized) like this :
But this is not working, the window.gtag does not seems to be correctly initialized...
As I see, the issue is in your
gtag
function. Change this:
To this:
And define it BEFORE calling it (move outside script.onload
).
---
Better approach: Don't modify Config.ts
at all - OpenSaaS already handles GA initialization. Just check if gtag
exists before tracking:
The Cookie Consent Modal loads GA asynchronously when users accept cookies. Your tracking function just needs to check if gtag
is available before using it.
Make sure you have REACT_APP_GOOGLE_ANALYTICS_ID
set in your .env.client
file.
Let me know If this works for you!