All questions

Where exactly do croct.identify and croct.anonymize go in a React login flow?

Asked by Sean Gallagher on

SGSean Gallagher

Wiring Croct into a JWT-based login flow and I want to get the sequencing right the first time. Our auth state lives in a context provider. Questions:

  1. Should identify fire when the login request succeeds, or when the auth context actually updates?
  2. Does anonymize go in the logout handler or somewhere else?
  3. Most important: if a user browses anonymously for a week and then signs in, is that pre-login history lost or does it carry over to the identified profile

Would rather ask now than untangle bad attribution later.

Was this helpful?

1 answer

MKMarta KowalczykAccepted answer

Straightforward sequencing:

  1. Call croct.identify('<userId>') as soon as the session is established, i.e. when your login succeeds and you know who the user is. Whether that is in the request handler or an effect watching the auth context does not matter much; what matters is that it fires once per sign-in with the final user ID. Note the ID must be a string, the SDK rejects anything else with "Invalid user ID".
  2. croct.anonymize() goes in the logout handler, right where you clear your own auth state.
  3. Get the instance via useCroct inside components, or use the plug instance directly outside React.

On your third question, which is the important one: nothing is lost. Identity resolution links the anonymous profile to the identified one when you call identify, so the week of pre-login browsing carries over to the signed-in user.

One thing not to do: do not call identify on every page load with the same ID "just in case". Once per sign-in is enough, the token persists.

Was this helpful?
SGSean Gallagher

Clear and complete, thanks. Put identify in the auth effect and anonymize in logout, and I can see the anonymous history on the identified profile now.

Still have questions?