Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix identify users in analytics #5335

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/strong-olives-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Users are now properly anonymously identified
4 changes: 0 additions & 4 deletions src/auth/hooks/useAuthProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApolloClient, ApolloError } from "@apollo/client";
import { IMessageContext } from "@dashboard/components/messages";
import { useAnalytics } from "@dashboard/components/ProductAnalytics/useAnalytics";
import { DEMO_MODE } from "@dashboard/config";
import { AccountErrorCode, useUserDetailsQuery } from "@dashboard/graphql";
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
Expand Down Expand Up @@ -38,7 +37,6 @@ type AuthErrorCodes = `${AccountErrorCode}`;

export function useAuthProvider({ intl, notify, apolloClient }: UseAuthProviderOpts): UserContext {
const { login, getExternalAuthUrl, getExternalAccessToken, logout } = useAuth();
const analytics = useAnalytics();
const navigate = useNavigator();
const { authenticated, authenticating, user } = useAuthState();
const [requestedExternalPluginId] = useLocalStorage("requestedExternalPluginId", null);
Expand Down Expand Up @@ -85,8 +83,6 @@ export function useAuthProvider({ intl, notify, apolloClient }: UseAuthProviderO
}
};
const handleLogout = async () => {
analytics.reset();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: how it will work now? We won't reset user if they are logging out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use our user_id to identify users so we rely on an auto-generated ID by Posthog, so when we run reset on logout and user login again, a new ID was generated.
For now, we will have one ID per device and only change the domain and email domain by setting user properties.


const returnTo = urlJoin(window.location.origin, getAppMountUriForRedirect());
const result = await logout({
input: JSON.stringify({
Expand Down
43 changes: 31 additions & 12 deletions src/components/ProductAnalytics/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
import { usePostHog } from "posthog-js/react";

interface UserProperties {
domain: string;
email_domain: string;
}

interface Analytics {
initialize: (details: Record<string, any>) => void;
reset: () => void;
initialize: (userProperties: UserProperties) => void;
trackEvent: (event: string, properties?: Record<string, any>) => void;
}

export function useAnalytics(): Analytics {
const posthog = usePostHog();

function initialize(details: Record<string, any>) {
// According to docs, posthog can be briefly undefined
const [lastUserProperties, setLastUserProperties] = useLocalStorage<UserProperties>(
"analyticsUserProperties",
{
domain: "",
email_domain: "",
},
);

function initialize(userProperties: UserProperties) {
if (!posthog) return;

const id = posthog.get_distinct_id();
if (!hasUserPropertiesChanged(userProperties, lastUserProperties)) return;

posthog.identify(id, details);
}
const id = posthog.get_distinct_id();

function reset() {
if (!posthog) return;
posthog.identify(id, userProperties);

posthog.reset();
setLastUserProperties(userProperties);
}

function trackEvent(event: string, properties?: Record<string, any>) {
Expand All @@ -30,5 +39,15 @@ export function useAnalytics(): Analytics {
posthog.capture(event, properties);
}

return { initialize, reset, trackEvent };
return { trackEvent, initialize };
}

function hasUserPropertiesChanged(
userProperties: UserProperties,
lastInitializedUserProperties: UserProperties,
) {
return (
userProperties.domain !== lastInitializedUserProperties.domain ||
userProperties.email_domain !== lastInitializedUserProperties.email_domain
);
}
Loading