mixpanel / mixpanel-js

Official Mixpanel JavaScript Client Library
https://mixpanel.com/help/reference/javascript
Other
867 stars 309 forks source link

Cannot add event properties to Page View event when track_pageview: true in SPA #415

Open anniegiang-octave opened 4 months ago

anniegiang-octave commented 4 months ago

I have a Next.js app. I am tracking the Page View event using track_pageview: true in mixpanel.init, as well as a custom useEffect hook that listens for route changes and tracks using mixpanel.track_pageview() (since Next.js is SPA).

The event props are added on route changes when the props are added to mixpanel.track_pageview(customProps). But the props are not added to the Page View event that's fired from track_pageview: true when the app first loads.

Is there a way to add event props to the Page View event that it is fired from track_pageview: true?

"next": "13.5.6",
"mixpanel-browser": "2.48.1",
"react": "18.2.0",
chiyc commented 4 months ago

Thanks for sharing your use case. It sounds like it may be easier here to fire the initial Page View event manually with the customProps right after SDK initialization instead of using track_pageview: true. Would that be possible in this situation?

mixpanel.init({ track_pageview: false, /* other options */ })
mixpanel.track_pageview(customProps)

I have a few other questions about the customProps. Where do the values in customProps come from? Are they known constant values ahead of time or are they only known once the page loads? Do they change between your calls to mixpanel.track_pageview(customProps)?