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

[bugfix] disable analytics manager on SSR #167

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/analytics/AnalyticsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class AnalyticsManager {
cartridgeTag: string;
device?: Device;
}) {
if (this.initialized) {
throw new Error("AnalyticsManager is already initialized");
}

this.apiUri = apiUri;
this.apiKey = apiKey;
this.app = app;
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/analytics/storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { v4 as uuidv4 } from "uuid";
import type { AnalyticsPayload } from "./types";

const localStorage: Pick<
WindowLocalStorage["localStorage"],
"getItem" | "setItem" | "removeItem"
> =
typeof window !== "undefined"
? window.localStorage
: {
getItem: () => null,
setItem: () => null,
removeItem: () => null,
};

function getCachedEventIds(): string[] {
let cachedEventIds: string[] = [];
const cachedEventIdsValue = localStorage.getItem("tdk-analytics-event-ids");
Expand Down
11 changes: 7 additions & 4 deletions packages/react/src/contexts/treasure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ const TreasureProviderInner = ({
}),
[apiUri, chain.id, sessionOptions?.backendWallet, client],
);
const [analyticsManager, setAnalyticsManager] =
useState<AnalyticsManager | null>(null);

const contractAddresses = useMemo(
() => getContractAddresses(chain.id),
Expand All @@ -114,9 +116,10 @@ const TreasureProviderInner = ({
? (getUserAddress(user, chain.id) ?? user.smartAccounts[0]?.address)
: undefined;

const analyticsManager = useMemo(() => {
if (!analyticsOptions) {
return undefined;
// biome-ignore lint/correctness/useExhaustiveDependencies: analyticsManager doesnt need to be a dependency
useEffect(() => {
if (!analyticsOptions || analyticsManager) {
return;
}

AnalyticsManager.instance.init({
Expand All @@ -127,7 +130,7 @@ const TreasureProviderInner = ({
device: analyticsOptions.device,
});

return AnalyticsManager.instance;
setAnalyticsManager(AnalyticsManager.instance);
}, [analyticsOptions]);

const trackCustomEvent = useCallback(
Expand Down