Skip to content

Commit

Permalink
plug in custom analytics to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Jan 24, 2025
1 parent 1fd4956 commit 80e024b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/core-react/internal/hooks/useAnalytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,61 @@ describe('useAnalytics', () => {
expect(generatedId).toBe(mockUUID);
expect(mockCrypto.randomUUID).toHaveBeenCalled();
});

it('should handle analyticsUrl from config correctly', () => {
const mockTitle = 'Test App';
const customAnalyticsUrl = 'https://custom-analytics.example.com';

Object.defineProperty(global.document, 'title', {
value: mockTitle,
writable: true,
});

(useOnchainKit as unknown as ReturnType<typeof vi.fn>).mockReturnValue({
apiKey: mockApiKey,
interactionId: mockInteractionId,
config: {
analyticsUrl: customAnalyticsUrl,
},
});

const { result: resultWithUrl } = renderHook(() => useAnalytics());
const event = AnalyticsEvent.WALLET_CONNECTED;
const data = { address: '0x0000000000000000000000000000000000000000' };

resultWithUrl.current.sendAnalytics(event, data);

expect(sendAnalytics).toHaveBeenCalledWith(
expect.objectContaining({
analyticsUrl: customAnalyticsUrl,
event,
data,
appName: mockTitle,
apiKey: mockApiKey,
interactionId: mockInteractionId,
}),
);

(useOnchainKit as unknown as ReturnType<typeof vi.fn>).mockReturnValue({
apiKey: mockApiKey,
interactionId: mockInteractionId,
config: {
analyticsUrl: null,
},
});

const { result: resultWithNull } = renderHook(() => useAnalytics());
resultWithNull.current.sendAnalytics(event, data);

expect(sendAnalytics).toHaveBeenCalledWith(
expect.objectContaining({
analyticsUrl: undefined,
event,
data,
appName: mockTitle,
apiKey: mockApiKey,
interactionId: mockInteractionId,
}),
);
});
});
11 changes: 9 additions & 2 deletions src/core-react/internal/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { sendAnalytics } from '@/core/network/sendAnalytics';
import { useEffect, useState } from 'react';

export const useAnalytics = () => {
const { apiKey, interactionId } = useOnchainKit();
const { apiKey, interactionId, config } = useOnchainKit();
const [appName, setAppName] = useState('');

useEffect(() => {
Expand All @@ -19,7 +19,14 @@ export const useAnalytics = () => {
event: AnalyticsEvent,
data: AnalyticsEventData[AnalyticsEvent],
) => {
sendAnalytics({ event, data, appName, apiKey, interactionId });
sendAnalytics({
analyticsUrl: config?.analyticsUrl ?? undefined,
appName,
apiKey,
data,
event,
interactionId,
});
},
generateInteractionId: () => {
return crypto.randomUUID();
Expand Down
37 changes: 37 additions & 0 deletions src/core/network/sendAnalytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,41 @@ describe('sendAnalytics', () => {

expect(consoleSpy).toHaveBeenCalledWith('Error sending analytics:', error);
});

it('should use provided analyticsUrl when specified', async () => {
const customUrl = 'https://custom-analytics.example.com';
const params = {
analyticsUrl: customUrl,
appName: 'TestApp',
apiKey: 'test-api-key',
data: { foo: 'bar' },
event: 'test-event',
interactionId: 'test-interaction-id',
};

mockFetch.mockResolvedValueOnce({});

await sendAnalytics(params);

expect(mockFetch).toHaveBeenCalledWith(customUrl, expect.any(Object));
});

it('should use default ANALYTICS_API_URL when analyticsUrl is not provided', async () => {
const params = {
appName: 'TestApp',
apiKey: 'test-api-key',
data: { foo: 'bar' },
event: 'test-event',
interactionId: 'test-interaction-id',
};

mockFetch.mockResolvedValueOnce({});

await sendAnalytics(params);

expect(mockFetch).toHaveBeenCalledWith(
ANALYTICS_API_URL,
expect.any(Object),
);
});
});
4 changes: 3 additions & 1 deletion src/core/network/sendAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ANALYTICS_API_URL, JSON_HEADERS } from '@/core/network/constants';

interface AnalyticsParams {
analyticsUrl?: string;
appName: string;
apiKey: string | null;
data: Record<string, unknown>;
Expand All @@ -9,14 +10,15 @@ interface AnalyticsParams {
}

export const sendAnalytics = async ({
analyticsUrl = ANALYTICS_API_URL,
appName,
apiKey,
data,
event,
interactionId,
}: AnalyticsParams) => {
try {
await fetch(ANALYTICS_API_URL, {
await fetch(analyticsUrl, {
method: 'POST',
headers: {
...JSON_HEADERS,
Expand Down

0 comments on commit 80e024b

Please sign in to comment.