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

[Analytics] Unify client analytics #2224

Merged
merged 24 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
7 changes: 7 additions & 0 deletions .changeset/silly-dolphins-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@thirdweb-dev/unity-js-bridge": patch
"@thirdweb-dev/react-native": patch
"@thirdweb-dev/react": patch
---

Adds global sdk vars
31 changes: 19 additions & 12 deletions packages/react-native/src/core/storage/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
TW_UPLOAD_SERVER_URL,
} from "@thirdweb-dev/storage";
import { IpfsUploaderOptions, UploadDataValue } from "./types";
import { appBundleId, packageVersion } from "../../evm/utils/version";
import { BUNDLE_ID_HEADER } from "../../evm/constants/headers";

const METADATA_NAME = "Storage React Native SDK";
Expand Down Expand Up @@ -38,9 +37,6 @@ export class IpfsUploader implements IStorageUploader<IpfsUploadBatchOptions> {
keyvalues: { ...options?.metadata },
};

const { version, name: packageName } = packageVersion;
const platform = "react-native";

if ("uri" in data[0] && "type" in data[0] && "name" in data[0]) {
// then it's an array of files
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -140,14 +136,24 @@ export class IpfsUploader implements IStorageUploader<IpfsUploadBatchOptions> {
});

xhr.open("POST", `${TW_UPLOAD_SERVER_URL}/ipfs/upload`);
xhr.setRequestHeader(BUNDLE_ID_HEADER, appBundleId || ""); // only empty on web
xhr.setRequestHeader(
BUNDLE_ID_HEADER,
(globalThis as any).APP_BUNDLE_ID,
); // only empty on web
if (this.clientId) {
xhr.setRequestHeader("x-client-id", this.clientId);
}

xhr.setRequestHeader("x-sdk-version", version);
xhr.setRequestHeader("x-sdk-name", packageName);
xhr.setRequestHeader("x-sdk-platform", platform);
xhr.setRequestHeader(
"x-sdk-version",
(globalThis as any).X_SDK_VERSION,
);
xhr.setRequestHeader("x-sdk-os", (globalThis as any).X_SDK_OS);
xhr.setRequestHeader("x-sdk-name", (globalThis as any).X_SDK_NAME);
xhr.setRequestHeader(
"x-sdk-platform",
(globalThis as any).X_SDK_PLATFORM,
);

xhr.send(form);
});
Expand All @@ -165,12 +171,13 @@ export class IpfsUploader implements IStorageUploader<IpfsUploadBatchOptions> {
{
method: "POST",
headers: {
[BUNDLE_ID_HEADER]: appBundleId || "", // only empty on web
[BUNDLE_ID_HEADER]: (globalThis as any).APP_BUNDLE_ID,
...(this.clientId ? { "x-client-id": this.clientId } : {}),
"Content-Type": "application/json",
"x-sdk-version": version,
"x-sdk-name": packageName,
"x-sdk-platform": platform,
"x-sdk-version": (globalThis as any).X_SDK_VERSION,
"x-sdk-name": (globalThis as any).X_SDK_NAME,
"x-sdk-platform": (globalThis as any).X_SDK_PLATFORM,
"x-sdk-os": (globalThis as any).X_SDK_OS,
},
body: fetchBody,
},
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/src/evm/constants/headers.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export const BUNDLE_ID_HEADER = "x-bundle-id";

export const X_SDK_VERSION_HEADER = "x-sdk-version";
export const X_SDK_NAME_HEADER = "x-sdk-name";
export const X_SDK_PLATFORM_HEADER = "x-sdk-platform";
export const X_SDK_OS_HEADER = "x-sdk-os";
10 changes: 10 additions & 0 deletions packages/react-native/src/evm/providers/thirdweb-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { SafeAreaProvider } from "react-native-safe-area-context";
import { walletIds } from "@thirdweb-dev/wallets";
import { ThirdwebStorage } from "../../core/storage/storage";
import type { Locale } from "../i18n/types";
import { appBundleId, packageVersion } from "../utils/version";
import { Platform } from "react-native";

export interface ThirdwebProviderProps<TChains extends Chain[]>
extends Omit<
Expand Down Expand Up @@ -148,6 +150,14 @@ export const ThirdwebProvider = <TChains extends Chain[] = DefaultChains>(
...restProps
} = props;

if (typeof globalThis !== "undefined") {
(globalThis as any).X_SDK_NAME = packageVersion.name;
(globalThis as any).X_SDK_PLATFORM = "mobile";
(globalThis as any).X_SDK_VERSION = packageVersion.version;
(globalThis as any).X_SDK_OS = Platform.OS;
(globalThis as any).APP_BUNDLE_ID = appBundleId;
}

const coinbaseWalletObj = supportedWallets.find(
(w) => w.id === walletIds.coinbase,
);
Expand Down
7 changes: 0 additions & 7 deletions packages/react-native/src/evm/utils/global.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ import {
import { InAppBrowser } from "react-native-inappbrowser-reborn";
import { createErrorMessage } from "./helpers/errors";
import {
appBundleId,
reactNativePackageVersion,
} from "../../../../utils/version";
import { BUNDLE_ID_HEADER } from "../../../../constants/headers";
BUNDLE_ID_HEADER,
X_SDK_NAME_HEADER,
X_SDK_OS_HEADER,
X_SDK_PLATFORM_HEADER,
X_SDK_VERSION_HEADER,
} from "../../../../constants/headers";
import { ANALYTICS } from "./helpers/analytics";

const HEADERS = {
[EWS_VERSION_HEADER]: (globalThis as any).X_SDK_VERSION,
[BUNDLE_ID_HEADER]: (globalThis as any).APP_BUNDLE_ID,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
[X_SDK_NAME_HEADER]: (globalThis as any).X_SDK_NAME,
[X_SDK_OS_HEADER]: (globalThis as any).X_SDK_OS,
[X_SDK_PLATFORM_HEADER]: (globalThis as any).X_SDK_PLATFORM,
[X_SDK_VERSION_HEADER]: (globalThis as any).X_SDK_VERSION,
Copy link
Member

Choose a reason for hiding this comment

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

are you sure this is ok? not sure if the values are set (in the provider) at the time of importing this file

};

export async function sendVerificationEmail(options: {
email: string;
clientId: string;
Expand Down Expand Up @@ -196,9 +208,7 @@ export async function socialLogin(oauthOptions: OauthOption, clientId: string) {

const resp = await fetch(headlessLoginLinkWithParams, {
headers: {
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[BUNDLE_ID_HEADER]: appBundleId,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
...HEADERS,
},
});

Expand Down Expand Up @@ -279,9 +289,7 @@ export async function customJwt(authOptions: AuthOptions, clientId: string) {
method: "POST",
headers: {
"Content-Type": "application/json",
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[BUNDLE_ID_HEADER]: appBundleId,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
...HEADERS,
},
body: JSON.stringify({
jwt: jwt,
Expand Down Expand Up @@ -329,9 +337,7 @@ export async function authEndpoint(
method: "POST",
headers: {
"Content-Type": "application/json",
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[BUNDLE_ID_HEADER]: appBundleId,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
...HEADERS,
},
body: JSON.stringify({
payload: payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ import {
} from "../../../types";
import { createErrorMessage } from "../errors";
import {
appBundleId,
reactNativePackageVersion,
} from "../../../../../../utils/version";
import { BUNDLE_ID_HEADER } from "../../../../../../constants/headers";
BUNDLE_ID_HEADER,
X_SDK_NAME_HEADER,
X_SDK_OS_HEADER,
X_SDK_PLATFORM_HEADER,
X_SDK_VERSION_HEADER,
} from "../../../../../../constants/headers";
import { ANALYTICS } from "../analytics";

const EMBEDDED_WALLET_TOKEN_HEADER = "embedded-wallet-token";
const PAPER_CLIENT_ID_HEADER = "x-thirdweb-client-id";

const HEADERS = {
"Content-Type": "application/json",
[BUNDLE_ID_HEADER]: appBundleId,
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[EWS_VERSION_HEADER]: (globalThis as any).X_SDK_VERSION,
[BUNDLE_ID_HEADER]: (globalThis as any).APP_BUNDLE_ID,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
[X_SDK_NAME_HEADER]: (globalThis as any).X_SDK_NAME,
[X_SDK_OS_HEADER]: (globalThis as any).X_SDK_OS,
[X_SDK_PLATFORM_HEADER]: (globalThis as any).X_SDK_PLATFORM,
[X_SDK_VERSION_HEADER]: (globalThis as any).X_SDK_VERSION,
Copy link
Member

Choose a reason for hiding this comment

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

same here

};

export const verifyClientId = async (clientId: string) => {
Expand Down Expand Up @@ -68,19 +75,15 @@ export const authFetchEmbeddedWalletUser = async (
Authorization: `Bearer ${EMBEDDED_WALLET_TOKEN_HEADER}:${
authTokenClient || ""
}`,
[BUNDLE_ID_HEADER]: appBundleId,
[PAPER_CLIENT_ID_HEADER]: clientId,
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
...HEADERS,
}
: {
Authorization: `Bearer ${EMBEDDED_WALLET_TOKEN_HEADER}:${
authTokenClient || ""
}`,
[BUNDLE_ID_HEADER]: appBundleId,
[PAPER_CLIENT_ID_HEADER]: clientId,
[EWS_VERSION_HEADER]: reactNativePackageVersion,
[THIRDWEB_SESSION_NONCE_HEADER]: ANALYTICS.nonce,
...HEADERS,
};
return fetch(url, params);
};
Expand Down
7 changes: 0 additions & 7 deletions packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
import { isGlobalThisPresent } from "./evm/utils/global";
import { appBundleId } from "./evm/utils/version";

export * from "./evm";

if (isGlobalThisPresent()) {
(globalThis as any).APP_BUNDLE_ID = appBundleId;
}
9 changes: 9 additions & 0 deletions packages/react/src/evm/providers/thirdweb-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { en } from "../locales/en";
import { ThirdwebLocaleContext } from "./locale-provider";
import { walletIds } from "@thirdweb-dev/wallets";
import { ThirdwebLocale } from "../locales/types";
import packageJson from "../../../package.json";
import { detectOS } from "../utils/isMobile";

export interface ThirdwebProviderProps<TChains extends Chain[]>
extends Omit<
Expand Down Expand Up @@ -479,6 +481,13 @@ export const ThirdwebProvider = <TChains extends Chain[] = DefaultChains>(
...restProps
} = props;

if (typeof globalThis !== "undefined") {
(globalThis as any).X_SDK_NAME = packageJson.name;
(globalThis as any).X_SDK_PLATFORM = "browser";
(globalThis as any).X_SDK_VERSION = packageJson.version;
(globalThis as any).X_SDK_OS = detectOS();
}

const wallets: WalletConfig[] = supportedWallets || defaultWallets;
const theme = _theme || "dark";

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/evm/utils/isMobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function isIOS(): boolean {
/**
* @internal
*/
function detectOS() {
export function detectOS() {
const env = detectEnv();
return env?.os ? env.os : undefined;
}
Expand Down
Loading
Loading