Mixpanel Community Icon

Best Practices for Triggering Events in Sessions and Time Intervals

·
·

What’s the best practice if we want an event to be triggered either once per session or not sooner than X time from the previous one?

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    Hi Federico C., Please stand by as I search the Mixpanel knowledge base for an AI-powered answer. I normally take about 15 seconds or so. In case AI cannot answer your question, feel free to reach out to Mixpanel support at mixpanel.com/get-support. If you have follow-up questions for this thread, please tag me again with your question: @kapa.ai <follow-up question> 👋

    Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    To implement an event that triggers either once per session or not sooner than a specific time interval from the previous occurrence, you'll need to combine Mixpanel's session tracking capabilities with custom logic in your implementation. Here's a best practice approach: 1. Use Mixpanel's session tracking: Mixpanel automatically computes sessions based on user activity. You can configure session settings in your project to define how sessions are determined (timeout-based, event-based, or property-based) Sessions: Compute sessions based on existing event data.2. Implement custom logic: In your application code, you'll need to add logic to check: a) If the event has already been triggered in the current session b) The time elapsed since the last trigger of this event3. Use super properties: Store the timestamp of the last event trigger as a super property. This will persist between visits and allow you to check the time elapsed Best Practices.Here's a pseudo-code example of how you might implement this in JavaScript: LBKS// Assume mixpanel is already initializedLBKSfunction triggerCustomEvent() { const lastTriggerTime = mixpanel.get_property('last_custom_event_time'); const currentTime = Date.now(); const timeSinceLastTrigger = currentTime - lastTriggerTime; const minTimeBetweenTriggers = X * 1000; // X seconds in millisecondsLBKS if (!lastTriggerTime || timeSinceLastTrigger > = minTimeBetweenTriggers) { mixpanel.track('Custom Event'); mixpanel.register({ 'last_custom_event_time': currentTime }); } }LBKS// Call this function when appropriate in your app triggerCustomEvent();LBKSThis approach ensures that the event is only triggered once per session (as long as you don't reset the super property within a session) or after the specified time interval has passed.Remember to adjust the session settings in Mixpanel to match your desired session definition Session Computation Deep-Dive.By following these best practices, you'll be able to implement the desired event triggering behavior while maintaining consistent and reliable tracking in Mixpanel.

    kapa.ai is still learning and improving, please let me know how I did by reacting below.

    ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.