Skip to content

Commit

Permalink
feat: anon id to alias
Browse files Browse the repository at this point in the history
  • Loading branch information
scopsy committed Jul 5, 2024
1 parent 8493f92 commit 3e83976
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
24 changes: 21 additions & 3 deletions apps/web/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ export function useAuth() {
}, [navigate, queryClient, segment]);

const redirectTo = useCallback(
({ url, redirectURL, origin }: { url: string; redirectURL?: string; origin?: string }) => {
({
url,
redirectURL,
origin,
anonymousId,
}: {
url: string;
redirectURL?: string;
origin?: string;
anonymousId?: string | null;
}) => {
const finalURL = new URL(url, window.location.origin);

if (redirectURL) {
Expand All @@ -127,6 +137,10 @@ export function useAuth() {
finalURL.searchParams.append('origin', origin);
}

if (anonymousId) {
finalURL.searchParams.append('anonymous_id', anonymousId);
}

// Note: Do not use react-router-dom. The version we have doesn't do instant cross origin redirects.
window.location.replace(finalURL.href);
},
Expand All @@ -139,8 +153,12 @@ export function useAuth() {
);

const redirectToSignUp = useCallback(
({ redirectURL, origin }: { redirectURL?: string; origin?: string } = {}) =>
redirectTo({ url: ROUTES.AUTH_SIGNUP, redirectURL, origin }),
({
redirectURL,
origin,
anonymousId,
}: { redirectURL?: string; origin?: string; anonymousId?: string | null } = {}) =>
redirectTo({ url: ROUTES.AUTH_SIGNUP, redirectURL, origin, anonymousId }),
[redirectTo]
);

Expand Down
9 changes: 9 additions & 0 deletions apps/web/src/pages/auth/components/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { useAcceptInvite } from './useAcceptInvite';
import { PasswordRequirementPopover } from './PasswordRequirementPopover';
import { ROUTES } from '../../../constants/routes';
import { OAuth } from './OAuth';
import { useSegment } from '../../../components/providers/SegmentProvider';
import { useStudioState } from '../../../studio/hooks';

type SignUpFormProps = {
invitationToken?: string;
Expand All @@ -37,6 +39,8 @@ export function SignUpForm({ invitationToken, email }: SignUpFormProps) {
const { isLoading: isAcceptInviteLoading, acceptInvite } = useAcceptInvite();
const { params, isFromVercel } = useVercelParams();
const loginLink = isFromVercel ? `${ROUTES.AUTH_LOGIN}?${params.toString()}` : ROUTES.AUTH_LOGIN;
const segment = useSegment();
const state = useStudioState();

const { isLoading, mutateAsync, isError, error } = useMutation<
{ token: string },
Expand All @@ -52,6 +56,7 @@ export function SignUpForm({ invitationToken, email }: SignUpFormProps) {
const onSubmit = async (data) => {
const parsedSearchParams = new URLSearchParams(location.search);
const origin = parsedSearchParams.get('origin');
const anonymousId = parsedSearchParams.get('anonymous_id');

const [firstName, lastName] = data?.fullName.trim().split(' ');
const itemData = {
Expand All @@ -66,6 +71,10 @@ export function SignUpForm({ invitationToken, email }: SignUpFormProps) {
const token = (response as any).token;
await login(token);

if (state?.anonymousId && anonymousId) {
segment.alias(anonymousId, (response as any).user?.id);
}

if (invitationToken) {
const updatedToken = await acceptInvite(invitationToken);
if (updatedToken) {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/studio/LocalStudioAuthenticator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function LocalStudioAuthenticator() {
// TODO: Refactor this to a smaller size function
useEffect(() => {
const parsedSearchParams = new URLSearchParams(location.search);
const anonymousId = parsedSearchParams.get('anonymous_id');

// Get the redirect URL of the Local Studio server
const redirectURL = parsedSearchParams.get('redirect_url');
Expand Down Expand Up @@ -67,7 +68,7 @@ export function LocalStudioAuthenticator() {
*/
// currentURL.searchParams.append('studio_path_hint', ROUTES.STUDIO_ONBOARDING);

return redirectToSignUp({ redirectURL: currentURL.href, origin: 'cli' });
return redirectToSignUp({ redirectURL: currentURL.href, origin: 'cli', anonymousId });
}

return;
Expand Down Expand Up @@ -104,7 +105,6 @@ export function LocalStudioAuthenticator() {

const localBridgeURL = buildBridgeURL(parsedApplicationOrigin.origin, tunnelPath);
const tunnelBridgeURL = buildBridgeURL(tunnelOrigin, tunnelPath);
const anonymousId = parsedSearchParams.get('anonymous_id');

// TODO: Add apiKeys to the IEnvironment interface as they exist in the response
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/utils/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ export class SegmentService {
this._segment?.identify(user?._id);
}

alias(anonymousId: string, userId: string) {
if (!this.isSegmentEnabled()) {
return;
}

if (this._mixpanelEnabled) {
mixpanel.alias(userId, anonymousId);
}

this._segment?.alias(userId, anonymousId);
}

setAnonymousId(anonymousId: string) {
if (!this.isSegmentEnabled() || !anonymousId) {
return;
Expand Down
8 changes: 7 additions & 1 deletion libs/application-generic/src/services/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export class AnalyticsService {
}
}

track(name: string, userId: string, data: Record<string, unknown> = {}) {
track(
name: string,
userId: string,
data: Record<string, unknown> = {},
anonymousId?: string
) {
if (this.segmentEnabled) {
Logger.log(
'Tracking event: ' + name,
Expand All @@ -123,6 +128,7 @@ export class AnalyticsService {

try {
this.segment.track({
anonymousId: userId,
userId: userId,
event: name,
properties: data,
Expand Down

0 comments on commit 3e83976

Please sign in to comment.