Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(sdk): React Native SDK works on Expo 55 (#565)

I'm Groot 🌳7251bc48d011
diff --git a/bun.lock b/bun.lock
index 4c4c0c444..ee5123581 100644
--- a/bun.lock
+++ b/bun.lock
@@ -574,12 +574,13 @@
"devDependencies": {
"@openpanel/tsconfig": "workspace:*",
"@types/node": "catalog:",
+ "bun-types": "1.4.4",
"tsup": "7.3.0",
"typescript": "catalog:",
},
"peerDependencies": {
- "expo-application": "5 - 7",
- "expo-constants": "14 - 18",
+ "expo-application": "5 - 7 || 55",
+ "expo-constants": "14 - 18 || 55",
"react-native": "*",
},
},
diff --git a/packages/sdks/react-native/index.test.ts b/packages/sdks/react-native/index.test.ts
new file mode 100644
index 000000000..8fe7c8362
--- /dev/null
+++ b/packages/sdks/react-native/index.test.ts
@@ -0,0 +1,104 @@
+import { afterEach, describe, expect, mock, test } from 'bun:test';
+
+let referrer: () => Promise<string> = async () => 'utm_source=play';
+let platform = 'android';
+let foreground: ((state: string) => void) | undefined;
+let globalProperties: Record<string, unknown> = {};
+const getInstallReferrerAsync = mock(() => referrer());
+mock.module('@openpanel/sdk', () => ({
+ OpenPanel: class {
+ api = { addHeader: mock(() => undefined) };
+ queue: unknown[] = [];
+ setGlobalProperties(value: Record<string, unknown>) {
+ globalProperties = { ...globalProperties, ...value };
+ }
+ flush = mock(() => undefined);
+ },
+}));
+mock.module('expo-application', () => ({
+ nativeApplicationVersion: '2.1.0',
+ nativeBuildVersion: '42',
+ getInstallReferrerAsync,
+}));
+mock.module('expo-constants', () => ({ default: {} }));
+mock.module('react-native', () => ({
+ AppState: {
+ addEventListener(_name: string, callback: (state: string) => void) {
+ foreground = callback;
+ },
+ },
+ Platform: {
+ get OS() {
+ return platform;
+ },
+ },
+}));
+const { OpenPanel } = await import('./index');
+const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
+const version = { __version: '2.1.0', __buildNumber: '42' };
+afterEach(() => {
+ globalProperties = {};
+ foreground = undefined;
+ platform = 'android';
+ referrer = async () => 'utm_source=play';
+ getInstallReferrerAsync.mockClear();
+});
+describe('optional Android install referrer', () => {
+ test('initialization retains version metadata when native lookup rejects', async () => {
+ referrer = async () => {
+ throw new Error('Install referrer unavailable');
+ };
+ new OpenPanel({ clientId: 'test' });
+ await settle();
+ expect(globalProperties).toEqual(version);
+ expect(getInstallReferrerAsync).toHaveBeenCalledTimes(1);
+ });
+ test('foreground refresh preserves known attribution if lookup fails', async () => {
+ new OpenPanel({ clientId: 'test' });
+ await settle();
+ referrer = async () => {
+ throw new Error('Service disconnected');
+ };
+ foreground?.('active');
+ await settle();
+ expect(globalProperties).toEqual({
+ ...version,
+ __referrer: 'utm_source=play',
+ });
+ expect(getInstallReferrerAsync).toHaveBeenCalledTimes(2);
+ });
+ test('recovers on foreground after synchronous native failure', async () => {
+ referrer = () => {
+ throw new Error('Native module unavailable');
+ };
+ new OpenPanel({ clientId: 'test' });
+ await settle();
+ expect(globalProperties).toEqual(version);
+ referrer = async () => 'utm_source=recovered';
+ foreground?.('active');
+ await settle();
+ expect(globalProperties).toEqual({
+ ...version,
+ __referrer: 'utm_source=recovered',
+ });
+ expect(getInstallReferrerAsync).toHaveBeenCalledTimes(2);
+ });
+ test('keeps the successful Android referrer', async () => {
+ new OpenPanel({ clientId: 'test' });
+ await settle();
+ expect(globalProperties).toEqual({
+ ...version,
+ __referrer: 'utm_source=play',
+ });
+ });
+ test('does not call the Android lookup on iOS', async () => {
+ platform = 'ios';
+ referrer = () => {
+ throw new Error('Android API called on iOS');
+ };
+ new OpenPanel({ clientId: 'test' });
+ await settle();
+ expect(globalProperties).toEqual(version);
+ expect(getInstallReferrerAsync).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/sdks/react-native/index.ts b/packages/sdks/react-native/index.ts
index 9044c692f..153915c28 100644
--- a/packages/sdks/react-native/index.ts
+++ b/packages/sdks/react-native/index.ts
@@ -5,8 +5,9 @@ import type {
} from '@openpanel/sdk';
import { OpenPanel as OpenPanelBase } from '@openpanel/sdk';
import * as Application from 'expo-application';
-import Constants from 'expo-constants';
+import * as ExpoConstants from 'expo-constants';
import { AppState, Platform } from 'react-native';
+import { getWebViewUserAgent } from './user-agent';
export * from '@openpanel/sdk';
@@ -59,7 +60,7 @@ export class OpenPanel extends OpenPanelBase {
sdkVersion: process.env.REACT_NATIVE_VERSION!,
});
- this.api.addHeader('User-Agent', Constants.getWebViewUserAgentAsync());
+ this.api.addHeader('User-Agent', getWebViewUserAgent(ExpoConstants));
this.storage = options.storage;
if (options.networkInfo) {
@@ -86,14 +87,21 @@ export class OpenPanel extends OpenPanelBase {
this.loadPersistedQueue();
}
+ /** Refresh app metadata while treating Android install attribution as optional. */
private async setDefaultProperties() {
+ let referrer: string | undefined;
+ if (Platform.OS === 'android') {
+ try {
+ referrer = await Application.getInstallReferrerAsync();
+ } catch {
+ // Install attribution is optional when the native service is unavailable.
+ }
+ }
+
this.setGlobalProperties({
__version: Application.nativeApplicationVersion,
__buildNumber: Application.nativeBuildVersion,
- __referrer:
- Platform.OS === 'android'
- ? await Application.getInstallReferrerAsync()
- : undefined,
+ ...(referrer === undefined ? {} : { __referrer: referrer }),
});
}
diff --git a/packages/sdks/react-native/package.json b/packages/sdks/react-native/package.json
index 4b2408d6d..070aefc44 100644
--- a/packages/sdks/react-native/package.json
+++ b/packages/sdks/react-native/package.json
@@ -13,7 +13,8 @@
},
"scripts": {
"build": "rm -rf dist && tsup",
- "typecheck": "tsc --noEmit"
+ "typecheck": "tsc --noEmit",
+ "test": "bun test --isolate"
},
"dependencies": {
"@openpanel/sdk": "workspace:1.3.3-local"
@@ -21,12 +22,13 @@
"devDependencies": {
"@openpanel/tsconfig": "workspace:*",
"@types/node": "catalog:",
+ "bun-types": "1.4.4",
"tsup": "7.3.0",
"typescript": "catalog:"
},
"peerDependencies": {
- "expo-application": "5 - 7",
- "expo-constants": "14 - 18",
+ "expo-application": "5 - 7 || 55",
+ "expo-constants": "14 - 18 || 55",
"react-native": "*"
}
}
diff --git a/packages/sdks/react-native/tsconfig.json b/packages/sdks/react-native/tsconfig.json
index 95a0d8537..20da643c7 100644
--- a/packages/sdks/react-native/tsconfig.json
+++ b/packages/sdks/react-native/tsconfig.json
@@ -3,7 +3,8 @@
"compilerOptions": {
"incremental": false,
"outDir": "dist",
- "strictNullChecks": true
+ "strictNullChecks": true,
+ "types": ["bun-types", "node"]
},
"exclude": ["dist"]
}
diff --git a/packages/sdks/react-native/user-agent.test.ts b/packages/sdks/react-native/user-agent.test.ts
new file mode 100644
index 000000000..3e50957d3
--- /dev/null
+++ b/packages/sdks/react-native/user-agent.test.ts
@@ -0,0 +1,70 @@
+import assert from 'node:assert/strict';
+import { describe, test } from 'node:test';
+import { getWebViewUserAgent } from './user-agent';
+
+describe('Expo User-Agent interop', () => {
+ test('accepts the default export and preserves the method receiver', async () => {
+ const constants = {
+ value: 'Expo/55',
+ async getWebViewUserAgentAsync() {
+ return this.value;
+ },
+ };
+ assert.equal(await getWebViewUserAgent({ default: constants }), 'Expo/55');
+ });
+
+ test('accepts a namespace helper even with an unrelated default export', async () => {
+ assert.equal(
+ await getWebViewUserAgent({
+ default: {},
+ getWebViewUserAgentAsync: async () => 'Expo/18',
+ }),
+ 'Expo/18'
+ );
+ });
+
+ test('omits the header when the helper is absent or unsupported', async () => {
+ for (const module of [
+ null,
+ undefined,
+ {},
+ { default: {} },
+ {
+ getWebViewUserAgentAsync: 'unavailable',
+ },
+ ]) {
+ assert.equal(await getWebViewUserAgent(module), null);
+ }
+ });
+
+ test('contains synchronous native errors', async () => {
+ assert.equal(
+ await getWebViewUserAgent({
+ getWebViewUserAgentAsync() {
+ throw new Error('Native module unavailable');
+ },
+ }),
+ null
+ );
+ });
+
+ test('contains asynchronous native errors', async () => {
+ assert.equal(
+ await getWebViewUserAgent({
+ getWebViewUserAgentAsync: async () => {
+ throw new Error('Unsupported runtime');
+ },
+ }),
+ null
+ );
+ });
+
+ test('preserves a null response', async () => {
+ assert.equal(
+ await getWebViewUserAgent({
+ getWebViewUserAgentAsync: async () => null,
+ }),
+ null
+ );
+ });
+});
diff --git a/packages/sdks/react-native/user-agent.ts b/packages/sdks/react-native/user-agent.ts
new file mode 100644
index 000000000..e73e597b3
--- /dev/null
+++ b/packages/sdks/react-native/user-agent.ts
@@ -0,0 +1,35 @@
+interface ConstantsLike {
+ getWebViewUserAgentAsync: () => Promise<string | null>;
+}
+
+/** Narrow Expo export objects to those with a callable User-Agent helper. */
+function hasUserAgentHelper(value: unknown): value is ConstantsLike {
+ return (
+ value !== null &&
+ typeof value === 'object' &&
+ 'getWebViewUserAgentAsync' in value &&
+ typeof value.getWebViewUserAgentAsync === 'function'
+ );
+}
+
+/** Resolve either Expo export shape and omit the optional header on failure. */
+export async function getWebViewUserAgent(
+ module: unknown
+): Promise<string | null> {
+ try {
+ const defaultExport =
+ module !== null && typeof module === 'object' && 'default' in module
+ ? module.default
+ : null;
+ const constants = hasUserAgentHelper(defaultExport)
+ ? defaultExport
+ : module;
+ if (!hasUserAgentHelper(constants)) {
+ return null;
+ }
+ return await constants.getWebViewUserAgentAsync();
+ } catch {
+ // Native User-Agent support varies by Expo runtime; tracking can omit it.
+ return null;
+ }
+}