Uniting Server-Side and Client-Side Tracking for Anonymous Users
I have the following use case:
I currently send `page` and `track` events to Segment for logged out users. When such a user then attempts to use our "Forgot Password" tool, I want to send a new `track` event along with the users email address as a property if the email was not found in our system. I'd like to do this on the server-side to avoid exposing this information to the client.
I'd like to do so in such a way that the client-side events and sever-side events are unified under the same anonymous user and then can be later associated with an authenticated user once that person has logged in.
The approach I'm considering is the following:
- Set a cookie with a session ID for anonymous users.
- Use that session ID on both the client and server as the `anonymous_id` when sending events to Segment.
- On authentication, call `identify` client-side and specify the session ID as the `anonymous_id`.
This seems like it should work but I'm not sure if manually specifying the `anonymous_id` on the client (step #3) is possible based on the docs. It seems that the `anonymous_id` is pulled automatically from local storage?
Anyone have experience with this approach? Are there any gotchas I'm not thinking of?
You wouldn't need to create a sessionID for what you're trying to do.
If you want to unify the events from a user in the client with the call you send from your servers to identify a user, you can do this using the existing anonymousId. When you send the email from the user to your servers, you can also retrieve the anonymousId from the client and send it to your servers. When you trigger the identify call from your servers, you can include both the email in the properties and the anonymousId together with the userId. You can follow these docs on how to retrieve the anonymousId: https://segment.com/docs/sources/website/analytics.js/#retrieving-the-anonymous-id
If this was a user that was already identified but logged out which means you had to reset their id, you can recover their anonymousId before resetting the user and then re-assign the anonymousId once they are logged out. Please follow these docs on how to set the anonymousId: https://segment.com/docs/sources/website/analytics.js/#setting-the-anonymous-id .