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

Add pageview tracking #5370

Merged
merged 15 commits into from
Jan 24, 2025
5 changes: 5 additions & 0 deletions .changeset/hip-horses-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Now you can see pageviews in the posthog.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
IS_CLOUD_INSTANCE: true
POSTHOG_KEY: ${{ secrets.POSTHOG_KEY }}
POSTHOG_HOST: ${{ secrets.POSTHOG_HOST }}
POSTHOG_EXCLUDED_DOMAINS: ${{ vars.EXCLUDED_DOMAINS }}
POSTHOG_EXCLUDED_DOMAINS: ${{ vars.POSTHOG_EXCLUDED_DOMAINS }}
ONBOARDING_USER_JOINED_DATE_THRESHOLD: ${{ vars.STAGING_ONBOARDING_USER_JOINED_DATE_THRESHOLD }}
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-automation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ jobs:
STATIC_URL: /
IS_CLOUD_INSTANCE: true
ONBOARDING_USER_JOINED_DATE_THRESHOLD: ${{ vars.STAGING_ONBOARDING_USER_JOINED_DATE_THRESHOLD }}

run: npm run build

- name: Configure AWS credentials
Expand Down
1 change: 1 addition & 0 deletions src/components/ProductAnalytics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const useConfig = () => {
capture_pageview: false,
autocapture: false,
advanced_disable_decide: true,
cookie_expiration: 30, // 30 days,
loaded: posthog => {
if (process.env.NODE_ENV === "development") posthog.debug();
},
Expand Down
19 changes: 9 additions & 10 deletions src/components/ProductAnalytics/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import useLocalStorage from "@dashboard/hooks/useLocalStorage";
import { usePostHog } from "posthog-js/react";

import { useRouteChange } from "../Router/useRouteChange";

interface UserProperties {
domain: string;
email_domain: string;
Expand All @@ -13,23 +14,21 @@ interface Analytics {

export function useAnalytics(): Analytics {
const posthog = usePostHog();
const [hasBeenUserSet, setHasBeenUserSet] = useLocalStorage<boolean>(
"analyticsHasBeenUserSet",
false,
);

const { register } = useRouteChange(location => {
trackEvent("$pageview", {
normalized_path: location.pathname,
});
});

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

// initialize function is called on each reload that cause generates new used id by identify function
// to avoid this we need to check if user has been set
if (hasBeenUserSet) return;
register();

const id = posthog.get_distinct_id();

posthog.identify(id, userProperties);

poulch marked this conversation as resolved.
Show resolved Hide resolved
setHasBeenUserSet(true);
}

function trackEvent(event: string, properties?: Record<string, any>) {
Expand Down
28 changes: 28 additions & 0 deletions src/components/Router/useRouteChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Location, UnregisterCallback } from "history";
import { useRef } from "react";
import useRouter from "use-react-router";

const compareLocations = (a: Location, b: Location) => {
return a.pathname === b.pathname && a.search === b.search;
};

export const useRouteChange = (onChange: (location: Location) => void) => {
const router = useRouter();
const location = useRef<Location>(router.history.location);
const listener = useRef<UnregisterCallback | null>(null);

const register = () => {
if (listener.current) return;

onChange(router.history.location);

listener.current = router.history.listen(incomingLocation => {
if (location.current && compareLocations(location.current, incomingLocation)) return;

onChange(incomingLocation);
location.current = incomingLocation;
});
};

return { register };
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { OnboardingProvider } from "./onboardingContext";
import { useOnboardingStorage } from "./onboardingContext/useOnboardingStorage";
import { WelcomePageOnboarding } from "./WelcomePageOnboarding";

jest.mock("@dashboard/components/Router/useRouteChange", () => ({
useRouteChange: () => ({
register: jest.fn(),
}),
}));
jest.mock("@dashboard/auth");
jest.mock("@dashboard/featureFlags", () => ({
useFlag: jest.fn().mockReturnValue({
Expand Down
Loading