---
title: "Anonymous activity lost at login: engineering solution"
description: "Preserve the device identity at login so Amplitude joins anonymous and signed-in events into one profile."
product: data
token_estimate: 501
---
# Anonymous activity lost at login: engineering solution

> For AI agents: a documentation index is available at [/docs/llms.txt](/docs/llms.txt). Append `.md` to any page URL for markdown, or send `Accept: text/markdown`.

Preserve the anonymous device ID when you assign the stable User ID at login. Amplitude uses that relationship to join the earlier anonymous activity with the known profile. Data Assistant can't repair a relationship that was never sent, so the fix belongs in the login flow. Start from the findings in the [audit](https://amplitude.com/docs/data/data-assistant-user-solutions/login-identity-merge-audit).

## Update the login flow

1. Keep one initialized Analytics SDK instance across the anonymous and authenticated parts of the session.
2. Don't regenerate or replace the device ID during login.
3. After authentication succeeds, set the canonical User ID through the SDK before you track the next event.
4. If you use the Identify API instead of a client SDK, send an identification object that contains the existing `device_id` and the newly known `user_id`. Follow the [Identify API](https://amplitude.com/docs/apis/analytics/identify) request requirements.
5. Keep sending the same canonical User ID on later authenticated events and on other devices.

> **Warning:** Don't change one established User ID into another
>
> Changing a non-null `user_id` creates another user. This workflow joins an anonymous device to a known User ID. It doesn't merge two established User IDs.

## Validate the fix

Repeat the [audit](https://amplitude.com/docs/data/data-assistant-user-solutions/login-identity-merge-audit). Check the raw event immediately before login and the first event after login. They should share the same `device_id`, and the event after login should carry the canonical `user_id`.

Refer to [Track unique users](https://amplitude.com/docs/data/sources/instrument-track-unique-users#user-id-follows-anonymous-events) for the identity sequence Amplitude expects.

