このドキュメントでは、React Nativeネイティブブリッジを使用して、PageSense iOS SDKをReact Nativeアプリケーションに連携する方法について説明します。
このガイドでは、以下について説明します。
最終的に、React Nativeアプリケーションで次のようなPageSense SDK関数をReact Nativeコードから直接呼び出せるようになります。
React Nativeネイティブブリッジを使用すると、JavaScriptコードがプラットフォーム固有のネイティブコードと通信できます。
これにより、React Nativeアプリケーションで次の機能を使用できます。
仕組み
a. JavaScriptがNativeModules内の関数を呼び出します
b. React Nativeがブリッジ経由でリクエストを送信します
c. ネイティブコードがリクエストを実行します
d. ネイティブコードが結果をPromiseとして返します
React Nativeアプリケーションは主にJavaScriptコードを実行しますが、iOS向けPageSense SDKはSwiftで実装されています。JavaScriptはSwiftコードと直接連携できないため、React Nativeにはネイティブブリッジの仕組みが用意されています。このブリッジにより、JavaScriptレイヤーとネイティブモジュール間の通信が可能になり、React NativeコードからSDK関数の呼び出し、実験のバリエーションの取得、目標の追跡をスムーズに行えます。
PageSense iOS SDK React Native–ネイティブブリッジのアーキテクチャ
PageSense SDKでは、次の関数を利用できます。
|
機能 |
関数 |
説明 |
|
SDKの初期化 |
createNewPageSenseClient |
アプリケーション内でPageSenseクライアントインスタンスを作成して初期化します。このクライアントインスタンスは、PageSenseサービスとの通信と、ユーザーセッションの実験管理を行います。 |
|
実験の有効化 |
activateExperiment |
特定の実験をユーザーに対して有効化し、実験の設定に基づいて適切なバリエーションを割り当てます。 |
|
バリエーション名の取得 |
getVariationName |
特定の実験で現在のユーザーに割り当てられているバリエーション名を取得し、アプリケーションで対応するUIや動作の変更を適用できるようにします。 |
|
目標の追跡 |
trackGoal |
実験で定義されたコンバージョンイベントや目標を記録・追跡し、各バリエーションの効果とパフォーマンスの測定に役立てます。 |
PageSense iOS SDKをReact Nativeアプリケーションに連携するためにReact Nativeネイティブブリッジを使用できるよう、開発環境に次のツールがインストールされていることを確認してください。
必要なパッケージ。
インストールを確認します。
PageSense iOS SDKはXCFrameworkを使用して配布できます。
a. PageSense iOS SDK(PageSenseFramework.xcframework)は、次のリポジトリーからダウンロードできます。
https://github.com/zoho/ZohoPageSenseSDK
b. 上記のリポジトリーに移動し、Frameworksフォルダーを開きます。
c. PageSenseFramework.xcframework.zipファイルをダウンロードし、内容を展開します。
d. 展開したPageSense XCFrameworkを、iOSプロジェクトフォルダー内の次の場所に配置します。/ProjectRoot/ios/Frameworks/PageSenseFramework.xcframework
e. Xcodeでワークスペースを開き、PageSenseFramework.xcframeworkをプロジェクトに追加します。
f. Xcodeで、プロジェクトの[Target]設定に移動します。
g. [Target]→[General]→[Frameworks, Libraries and Embedded Content]に移動します。
h. このセクションにPageSenseFramework.xcframeworkを追加します。
i. フレームワークオプションを[Embed & Sign]に設定します。
CocoaPodsを使用して、必要なReact NativeライブラリーがiOSプロジェクトにインストールされていることを確認してください。これらのライブラリーは、iosディレクトリーでpod installコマンドを実行すると自動的にインストールされます。CocoaPodsのインストールの一部として、次のReact Nativeライブラリーがプロジェクトで利用可能である必要があります。
また、React Nativeネイティブブリッジの実装に必要な次のiOSフレームワーク依存関係が利用可能であることを確認してください。
これらのライブラリーにより、SwiftまたはObjective-Cでネイティブモジュールを実装し、React Nativeネイティブブリッジを通じてPageSense iOS SDKをReact Nativeアプリケーションに公開できます。
React Nativeネイティブモジュールは、次の2つの部分で実装されます。
a. Swift実装
b. Objective-Cブリッジ
Objective-Cレイヤーは、SwiftメソッドをReact Nativeに公開します。
Swiftを使用して、PageSense iOS SDK用のネイティブモジュールファイルを作成します。このファイルは、React Nativeプロジェクトのルートディレクトリーにあるiosフォルダー内のiOSアプリケーションディレクトリーに配置します。
/ProjectRoot/ios/SampleApp/PageSenseSDKModule.swift
a. SwiftとObjective-C間の通信を有効にするには、次の場所にブリッジングヘッダーを作成します。/ProjectRoot/ios/SampleApp/SampleApp-Bridging-Header.h
b. Xcodeでプロジェクトを開き、[ビルド設定]に移動します。
c. [Objective-Cブリッジングヘッダー]を探します。
d. 値を次のように設定します: SampleApp/SampleApp-Bridging-Header.h
React NativeアプリケーションとネイティブのPageSense SDK実装を連携できるように、React Nativeラッパーファイルを作成します。このラッパーは、React Nativeアプリケーション内のJavaScriptコードにネイティブのPageSense SDKメソッドを公開するインターフェイス層として機能します。
JavaScriptラッパーファイルは、プロジェクト構造内の次のパスに作成します。src/sdk/PageSenseSDKWrapper.js
import { NativeModules } from 'react-native';// Extract the native module exposed through the React Native bridge.const { PageSenseSDKModule } = NativeModules;/** * PageSenseSDKWrapper provides a JavaScript interface for interacting * with the PageSense SDK through the React Native Native Bridge. */class PageSenseSDKWrapper { /** * Initialise the PageSense SDK. Should be called once during application startup. * * @param {string} accountId - PageSense account identifier. * @param {string} sdkKey - SDK key used for authentication. * @param {string} projectName - Name of the PageSense project. * * @returns {Promise<boolean>} * Returns: * - true → SDK initialized successfully * - false → initialization failed */ async initialiseSDK(accountId, sdkKey, projectName) { try { this._validateString(accountId, 'accountId'); this._validateString(sdkKey, 'sdkKey'); this._validateString(projectName, 'projectName'); const result = await PageSenseSDKModule.initialiseSDK( accountId, sdkKey, projectName, ); return result === true; } catch (error) { console.log('[PageSenseSDK] initialiseSDK error:', error); return false; } } /** * Activates an experiment and returns the assigned variation. * * @param {string} experimentName - Name of the experiment. * @param {string} userId - Unique user identifier. * @param {Object.<string, string>} [attributes] - Optional user attributes. * * @returns {Promise<string|null>} * Returns: * - variation name → if assigned * - null → if no variation or error */ async activateExperiment(experimentName, userId, attributes) { try { this._validateString(experimentName, 'experimentName'); this._validateString(userId, 'userId'); this._validateAttributes(attributes); const safeAttributes = attributes || {}; const variation = await PageSenseSDKModule.activateExperiment( experimentName, userId, safeAttributes, ); return variation ?? null; } catch (error) { console.log('[PageSenseSDK] activateExperiment error:', error); return null; } } /** * Retrieves the variation name without activating the experiment. * * @param {string} experimentName - Name of the experiment. * @param {string} userId - Unique user identifier. * @param {Object.<string, string>} [attributes] - Optional user attributes. * * @returns {Promise<string|null>} * Returns: * - variation name → if available * - null → if not available or error */ async getVariationName(experimentName, userId, attributes) { try { this._validateString(experimentName, 'experimentName'); this._validateString(userId, 'userId'); this._validateAttributes(attributes); const safeAttributes = attributes || {}; const variation = await PageSenseSDKModule.getVariationName( experimentName, userId, safeAttributes, ); return variation ?? null; } catch (error) { console.log('[PageSenseSDK] getVariationName error:', error); return null; } } /** * Tracks a goal event for an experiment. * * @param {string} experimentName - Name of the experiment. * @param {string} userId - Unique user identifier. * @param {string} goalName - Name of the goal. * @param {Object.<string, string>} [attributes] - Optional goal attributes. * * @returns {void} */ trackGoal(experimentName, userId, goalName, attributes) { try { this._validateString(experimentName, 'experimentName'); this._validateString(userId, 'userId'); this._validateString(goalName, 'goalName'); this._validateAttributes(attributes); const safeAttributes = attributes || {}; PageSenseSDKModule.trackGoal( experimentName, userId, goalName, safeAttributes, ); } catch (error) { console.log('[PageSenseSDK] trackGoal error:', error); } } /** * Validates whether the given parameter is a non-empty string. * @private * @throws {Error} if param is not a non-empty string */ _validateString(param, paramName) { if (typeof param !== 'string' || param.trim() === '') { throw new Error( `[PageSenseSDK] Invalid ${paramName}: must be a non-empty string`, ); } } /** * Validates the attributes object to ensure it is a valid key-value pair structure. * @private * @throws {Error} if attributes is invalid */ _validateAttributes(attributes) { if (attributes === undefined || attributes == null) return; if (typeof attributes !== 'object' || Array.isArray(attributes)) { throw new Error('[PageSenseSDK] attributes must be a key-value object'); } for (const key in attributes) { if (typeof key !== 'string' || key.trim() === '') { throw new Error( '[PageSenseSDK] Invalid attribute key: must be a non-empty string', ); } const value = attributes[key]; if (typeof value !== 'string') { throw new Error( `[PageSenseSDK] Invalid value for attribute '${key}'. Allowed type: string`, ); } } }}// Export a singleton instanceexport default new PageSenseSDKWrapper();PageSense SDKは、アプリケーションのライフサイクル中に1回だけ初期化する必要があります。理想的には、アプリの起動時に初期化します。たとえば、React Nativeアプリケーションのメインエントリーポイント(App.jsxやApp.jsなど)で、コンポーネントをレンダリングしたり、実験関連のロジックを実行したりする前にSDKを初期化できます。この方法により、SDKの設定が完了し、アプリ内で必要になったときにいつでも利用できるようになります。
ユーザーが適切なバリエーションに割り当てられ、UIや機能に関連する変更がすぐに適用されるよう、画面の読み込み時に実験を実行する必要があります。これにより、画面がアクティブになると同時に実験がトリガーされ、一貫した動作と正確なバリエーション割り当てが実現します。たとえば、React Nativeコンポーネントでは、App.jsxやApp.js、または関連する画面コンポーネントのuseEffectなどのライフサイクルメソッド内で実験を呼び出すことで、画面のマウント時に1回実行できます。
目標のトラッキングは、ユーザーがアプリケーション内で特定の操作やイベントを完了した直後に実行してください。これにより、コンバージョン、操作、その他の主要なパフォーマンス指標がリアルタイムで記録され、実験の効果を分析するための正確でタイムリーなデータが得られます。たとえば、ユーザーがボタンをタップしたり、フォームを送信したり、購入を完了したりした後に、対応する目標をトラッキング用にPageSense SDKへ送信してください。
このガイドに従うことで、React Nativeアプリケーションで次のことが可能になります。
この連携により、PageSense iOS SDKを使用して、React Nativeアプリケーション内でFullStack A/Bテスト機能を利用できるようになります。