This has been bothering me for a while, I think the problem is this:
GA4 uses batching, i.e. a 5 second buffer to collect any events before making a network request.
GA4 also uses the Beacon API i.e. sends the events asynchronously and directly. This prevents you losing your final few events when the page unloads.
Problem 1: If the 5 seconds haven't elapsed for the batching to finish, the network request isn't sent - you lose the final few events of a session.
Problem 2: Turns out that those events aren't "lost," they're actually stored somewhere and sent when the user starts a new session! More explained below:
I've set up a custom timestamp as outlined by Simo, but I've noticed that on occasion the first event of a new session will actually come from the last event of the previous session. I've only noticed this because the custom_timestamp
sets the value on the client-side, i.e. when the event happens, whereas event_timestamp
is the batched timestamp of when the payload hits the Google servers.
Question:
That means the batched payload is being stored somewhere (but I have no idea where, this would be helpful info too). Is there ANY way to either flush out the events on unload (as you could in UA as per Simo), or delete the local storage of these events so that I don't get rouge events being sent to new sessions?