Chapter 02
Identity and sessions
Twig records each filter selection as a stay_filter_selected event. Its destination_type property shows which destination was chosen. This tells the team what happened, but not who was behind the interaction.
PostHog adds two identifiers that help connect the event to the visitor and visit that produced it:
Group events into one visit
Put yourself in the shoes of an anonymous Twig visitor. Select Forest, then Coast, and notice how the distinct_id and $session_id remain the same:

Adiron-shack
Adirondacks, New York
4 guests · 2 bedrooms · 2 baths
$355 / night USD
anonymous browser
The matching IDs tell the Twig team that both selections happened during one session and both events came from the same anonymous identity.
For this visit, PostHog counts two events associated with the same anonymous identity and session. The identity and session help the team determine which visitor and visit the events belong to, while the event count records how many distinct actions occurred.
There's a problem with anonymous IDs, though: anon_7f3 identifies activity from this browser, but it doesn't show who was using it. If two people share the browser, they share an anonymous ID, so the Twig team has no idea which one generated the activity.
Same browser, new session
The next day, an anonymous browser can return and select a new filter: City.
By default, PostHog starts a new session after 30 minutes without activity. The anonymous ID stored in the browser remains, but the session ID changes.
Twig has now recorded three selections from one anonymous identity across two sessions:
distinct_id: anon_7f3connects the events to the same browser identity.$session_id: session_b2places the City selection in a different visit.
The anonymous ID does not automatically follow someone to another browser, phone, or computer. Clearing browser storage also removes that connection.
This presents a problem for the Twig engineers: they still can't claim that anon_7f3 represents a person or understand their session history.
Identify visitors
Twig offers accounts so visitors can edit their profiles, save and view their booked stays, and update account settings. Select the bookmark on Adiron-shack, then sign in to Twig's demo account for Edgar Hogg:

Adiron-shack
Adirondacks, New York
4 guests · 2 bedrooms · 2 baths
$355 / night USD
anonymous browser
When Edgar signs in, Twig calls posthog.identify() with user_812, his account ID from Twig's database – not an ID created by PostHog.
PostHog links his anonymous activity to his person profile. Earlier events keep their original distinct_id: anon_7f3, but activity before and after sign-in appears together. Separate visits remain separate sessions.
On his phone, Edgar starts with a different anonymous ID. Signing in with the same account ID connects that device's activity to his profile too. Without signing in, it stays anonymous.
Connect browser and server activity
Suppose Edgar clicks Book stay in his browser and Twig's server completes the booking. The engineers want to connect both actions to Edgar.
In the browser, identify() tells PostHog which account to use for subsequent events. On the server, the engineers pass that account ID with each event.
Here, user_812 represents the ID from Edgar's signed-in account – not a value to hard-code. Both events arrive with the same distinct_id, connecting them to the same person.
Reset the identity at sign-out
Now suppose Edgar signs out and a second person uses Twig in the same browser. Open the profile menu and select Log out:
Saved stays
Keep the places you want to come back to.
signed-in browser
When Edgar signs out, Twig tells the PostHog tracking code in the browser to forget his identity. reset() replaces Edgar's identified distinct_id with a new anonymous ID and starts a new session. Edgar's person profile and earlier events stay unchanged.
Without reset(), the second person's browsing could be attached to Edgar's account. If the second person later signs in, Twig may also attempt to connect two different accounts incorrectly.
The safe sequence is:
- Identify the account after sign-in.
- Capture that account's activity.
- Reset PostHog at sign-out.
- Keep later events anonymous until another account signs in.
See the visit behind an event
To see context around a click, like what led to the event or what the user did next, Twig opens the event's Session Replay – a playback of what happened in the browser during that visit.
PostHog uses the same $session_id already attached to the event to find the matching replay. For the City click, session_b2 connects the click to its recorded visit. Because the event’s distinct_id: anon_7f3 is now linked to user_812, PostHog can also connect that visit to Edgar.
- Browse staysPageview
- event
- $pageview
- pathname
- /
- distinct_id
- anon_7f3
- $session_id
- session_b2
- City filterClick
- Le Nid ChicPageview
- Log inPageview
- Identify EdgarIdentity
- Le Nid ChicPageview
- Save stayClick
This creates one connected trail:
Event → visit → person
Now Twig's engineers can connect an event to a person. Next, they use those events to see how many people use a feature and how many finish a booking.











