Tying Amplitude events sent from back-end to user sessions

1.7k Views Asked by At

When tracking website with Amplitude if some events are sent from the backend, how can we tie those events to specific user sessions?

Thank you

1

There are 1 best solutions below

0
On

As someone working in Amplitude, you need to sent session-id together with your server-side event to tie them into the same session.

I presume you use the HTTP API endpoint to sent events from the backend. It has a field called session_id that is just a Unix timestamp when the session starts. Because you obviously need to have the same session-id between the front end and backend you can get the session id from client-side calling

amplitude.getInstance()._sessionId; and then pass that into your backend. This is session-id is created when our SDK starts in your site.

Now you can pass this value with your backend events. Events with same session id within the same user or device id will be tied to the same session. Session id doesn't need to be globally unique.

If you are already tracking sessions in your app somehow, another much easier way to get unified sessions is to use any custom session identifier. You can send whatever session identifier you have into amplitude as user property and then go to Amplitude UI and select to use custom session property. This property could literally anything. See more here: https://help.amplitude.com/hc/en-us/articles/115002323627-Tracking-Sessions#h_d6df9d70-48fa-44cb-8907-7c3c652b007fc