Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 0 additions & 67 deletions packages/thirdweb/src/analytics/track.ts

This file was deleted.

78 changes: 78 additions & 0 deletions packages/thirdweb/src/analytics/track/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import type { ThirdwebClient } from "../../client/client.js";
import { trackConnect } from "./connect.js";

const server = setupServer(
http.post("https://c.thirdweb.com/event", () => {
return HttpResponse.json({});
}),
);

describe("trackConnect", () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it("should send a POST request with correct data", async () => {
const mockClient: ThirdwebClient = {
clientId: "test-client-id",
secretKey: undefined,
};

let requestBody: unknown;
server.use(
http.post("https://c.thirdweb.com/event", async (handler) => {
requestBody = await handler.request.json();
return HttpResponse.json({});
}),
);

await trackConnect({
client: mockClient,
walletType: "metamask",
walletAddress: "0x1234567890123456789012345678901234567890",
});

expect(requestBody).toEqual({
source: "connectWallet",
action: "connect",
walletType: "metamask",
walletAddress: "0x1234567890123456789012345678901234567890",
});
});

it("should send a POST request with correct headers", async () => {
const mockClient: ThirdwebClient = {
clientId: "test-client-id",
secretKey: undefined,
};

let requestHeaders: Headers | undefined;
server.use(
http.post("https://c.thirdweb.com/event", (handler) => {
requestHeaders = handler.request.headers;
return HttpResponse.json({});
}),
);

await trackConnect({
client: mockClient,
ecosystem: {
id: "ecosystem.test-ecosystem-id",
partnerId: "test-partner-id",
},
walletType: "metamask",
walletAddress: "0x1234567890123456789012345678901234567890",
});

expect(requestHeaders?.get("x-client-id")).toEqual("test-client-id");
expect(requestHeaders?.get("x-ecosystem-id")).toEqual(
"ecosystem.test-ecosystem-id",
);
expect(requestHeaders?.get("x-ecosystem-partner-id")).toEqual(
"test-partner-id",
);
});
});
25 changes: 25 additions & 0 deletions packages/thirdweb/src/analytics/track/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ThirdwebClient } from "../../client/client.js";
import type { Ecosystem } from "../../wallets/in-app/core/wallet/types.js";
import { track } from "./index.js";

/**
* @internal
*/
export async function trackConnect(args: {
client: ThirdwebClient;
ecosystem?: Ecosystem;
walletType: string;
walletAddress: string;
}) {
const { client, ecosystem, walletType, walletAddress } = args;
return track({
client,
ecosystem,
data: {
source: "connectWallet",
action: "connect",
walletType,
walletAddress,
},
});
}
30 changes: 30 additions & 0 deletions packages/thirdweb/src/analytics/track/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ThirdwebClient } from "../../client/client.js";
import { getClientFetch } from "../../utils/fetch.js";
import { stringify } from "../../utils/json.js";
import type { Ecosystem } from "../../wallets/in-app/core/wallet/types.js";

const ANALYTICS_ENDPOINT = "https://c.thirdweb.com/event";

/**
* @internal
*/
export async function track({
client,
ecosystem,
data,
}: {
client: ThirdwebClient;
ecosystem?: Ecosystem;
data: object;
}) {
const fetch = getClientFetch(client, ecosystem);
const event = {
source: "sdk",
...data,
};

return fetch(ANALYTICS_ENDPOINT, {
method: "POST",
body: stringify(event),
}).catch(() => {});
}
98 changes: 98 additions & 0 deletions packages/thirdweb/src/analytics/track/pay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
import type { ThirdwebClient } from "../../client/client.js";
import { trackPayEvent } from "./pay.js";

const server = setupServer(
http.post("https://c.thirdweb.com/event", () => {
return HttpResponse.json({});
}),
);

describe("trackPayEvent", () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it("should send a POST request with correct data", async () => {
const mockClient: ThirdwebClient = {
clientId: "test-client-id",
secretKey: undefined,
};

let requestBody: unknown;
server.use(
http.post("https://c.thirdweb.com/event", async (handler) => {
requestBody = await handler.request.json();
return HttpResponse.json({});
}),
);

await trackPayEvent({
client: mockClient,
event: "test-event",
walletAddress: "0x1234567890123456789012345678901234567890",
walletType: "io.metamask",
fromToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
fromAmount: "1000000",
toToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
toAmount: "1000000",
chainId: 1,
dstChainId: 137,
});

expect(requestBody).toEqual({
source: "pay",
action: "test-event",
clientId: "test-client-id",
chainId: 1,
walletAddress: "0x1234567890123456789012345678901234567890",
walletType: "io.metamask",
tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
amountWei: "1000000",
dstTokenAddress: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
dstChainId: 1,
});
});

it("should send a POST request with correct headers", async () => {
const mockClient: ThirdwebClient = {
clientId: "test-client-id",
secretKey: undefined,
};

let requestHeaders: Headers | undefined;
server.use(
http.post("https://c.thirdweb.com/event", (handler) => {
requestHeaders = handler.request.headers;
return HttpResponse.json({});
}),
);

await trackPayEvent({
client: mockClient,
ecosystem: {
id: "ecosystem.test-ecosystem-id",
partnerId: "test-partner-id",
},
event: "test-event",
walletAddress: "0x1234567890123456789012345678901234567890",
walletType: "io.metamask",
fromToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
fromAmount: "1000000",
toToken: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
toAmount: "1000000",
chainId: 1,
dstChainId: 137,
});

expect(requestHeaders?.get("x-client-id")).toEqual("test-client-id");
expect(requestHeaders?.get("x-ecosystem-id")).toEqual(
"ecosystem.test-ecosystem-id",
);
expect(requestHeaders?.get("x-ecosystem-partner-id")).toEqual(
"test-partner-id",
);
});
});
37 changes: 37 additions & 0 deletions packages/thirdweb/src/analytics/track/pay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ThirdwebClient } from "../../client/client.js";
import type { Ecosystem } from "../../wallets/in-app/core/wallet/types.js";
import { track } from "./index.js";

/**
* @internal
*/
export async function trackPayEvent(args: {
client: ThirdwebClient;
ecosystem?: Ecosystem;
event: string;
walletAddress?: string;
walletType?: string;
fromToken?: string;
fromAmount?: string;
toToken?: string;
toAmount?: string;
chainId?: number;
dstChainId?: number;
}) {
return track({
client: args.client,
ecosystem: args.ecosystem,
data: {
source: "pay",
action: args.event,
clientId: args.client.clientId,
chainId: args.chainId,
walletAddress: args.walletAddress,
walletType: args.walletType,
tokenAddress: args.fromToken,
amountWei: args.fromAmount,
dstTokenAddress: args.toToken,
dstChainId: args.chainId,
},
});
}
Loading
Loading