mixpanel-js icon indicating copy to clipboard operation
mixpanel-js copied to clipboard

track's callback called before request were sent

Open Pavel-Liteiniy opened this issue 1 year ago • 3 comments

Hello! : )

I'm not sure that this is correct behavior

  1. initializing mixpanel with persistence: 'localStorage'
    mixpanel.init(token, {
      persistence: 'localStorage',
      persistence_name: 'mixpanel',
    })
  1. Trying to track event mixpanel.track('my event', undefined (response) => {...handling error response...}) and expecting to get error if smth went wrong
export function track(
    event_name: string,
    properties?: Dict,
    optionsOrCallback?: RequestOptions | Callback,
    callback?: Callback, <-- I'm want to handle error response here (for example { "error": "token, invalid format", "status": 0 })
): void;

If I track event without setting RequestOptions['send_immediately'] equals to true then my callback is called before request to mixpanel has been finished here:

image

Pavel-Liteiniy avatar Jul 16 '24 15:07 Pavel-Liteiniy

Yes, in the default mode, track requests are batched and sent over the network at regular intervals. The track() callback is executed when the event is enqueued for transfer, while the actual network-sending mechanism is separate and occurs on a timer. We don't currently expose any callback from the network side of the tracking code.

tdumitrescu avatar Jul 16 '24 15:07 tdumitrescu

Yes, in the default mode, track requests are batched and sent over the network at regular intervals. The track() callback is executed when the event is enqueued for transfer, while the actual network-sending mechanism is separate and occurs on a timer. We don't currently expose any callback from the network side of the tracking code.

@tdumitrescu if send_immediately: true is passed in the options does the callback execute when the event gets sent (not enqueued)? Also, are there any docs that talk more about this? Thanks

josuemy avatar Nov 17 '24 22:11 josuemy

@josuemy yes, with send_immediately there is no enqueueing, and the callback should fire when the network request returns. I don't know of any docs around this atm.

tdumitrescu avatar Nov 18 '24 00:11 tdumitrescu