The GoPersonal SDK for React Native allows you to capture user behavior, display personalized content based on their interactions, and implement push notifications.
First, install the GoPersonal SDK for React Native:
npm install gopersonal-react-native-sdkTo start using the SDK, import it into your project and initialize it with your client ID and client secret.
import SDK from 'gopersonal-react-native-sdk';
// Initialize the SDK
await SDK.init('BR-your-client-id', 'your-client-secret', { debug: true });import SDK from 'gopersonal-react-native-sdk';
// Initialize the SDK with full TypeScript support
await SDK.init('BR-your-client-id', 'your-client-secret', { debug: true });
// TypeScript will provide autocomplete and type checking
const searchResults = await SDK.search('query', { jsonFilter: { category: 'electronics' } });Log in a user to the SDK:
await SDK.login('customer-id', { additionalData: 'value' });Log out the current user:
await SDK.logout();Perform a search query and get results:
const searchResults = await SDK.search('search query');Use the new AI-powered semantic search with reranking:
// Basic search
const results = await SDK.searchV2('wireless headphones');
// With options
const results = await SDK.searchV2('wireless headphones', {
limit: 50,
embedding_mode: 'gemma_bm25',
reranker: 'qwen',
filters: { brand: ['Sony', 'Bose'], color: ['black'] }
});
// Response structure
// {
// hits: [{ id, score, payload: { name, price, url, imgs, ... } }],
// hits_count: 50,
// search_id: 'abc123',
// total_results: 150,
// facets: { brand: { display_name: 'Marca', values: [...] } },
// is_occasion_search: false
// }Get search suggestions while typing:
const suggestions = await SDK.autocompleteV2('headph');Paginate through search results using the search_id from a previous search:
const page2 = await SDK.searchPageV2('search-id-from-previous-search', 2, {
page_size: 50,
filters: { brand: ['Sony'] },
sort_by: 'price-low-high'
});Get available facets/filters for the project:
const facets = await SDK.getFacetsV2();Get products similar to a specific product:
const similarProducts = await SDK.getSimilarProductsV2('product-id');Get a single product by its ID:
const product = await SDK.getProductByIdV2('product-id');Perform a search query using an image file:
const imageSearchResults = await SDK.imageSearch(file);Retrieve personalized content by content ID:
const content = await SDK.getContent('content-id');Retrieve Net Promoter Score content:
await SDK.addInteraction('view', { someData: 'value' });Interact with addon endpoints.
const addonData = await SDK.requestAddonData('endpoint');
const addonDataWithParams = await SDK.requestAddonData('endpoint', {
params: { id: 123, filter: 'active' }
});
const postResponse = await SDK.requestAddonData('endpoint', {
method: 'post',
data: { name: 'Nuevo item', status: 'active' }
});
const putResponse = await SDK.requestAddonData('endpoint', {
method: 'put',
data: { id: 123, status: 'completed' }
});
const deleteResponse = await SDK.requestAddonData('endpoint', {
method: 'delete',
params: { id: 123 }
});
const responseWithOptions = await SDK.requestAddonData('endpoint', {
method: 'get',
timeout: 5000,
debug: true
});Capture user interactions to enhance personalization:
await SDK.addInteraction('view', { someData: 'value' });Add a state to the interaction flow:
await SDK.addInteractionState('stateKey', { transactionId: 'uniqueId' });Mark an impression as opened:
await SDK.openImpression('impressionId');Note for Expo Go users: Expo Go does not include the native Firebase Messaging module. As of this SDK version, Firebase messaging is loaded lazily and guarded, so importing the SDK features will work in Expo Go without crashing. Push-related methods will no-op if the native module is unavailable. To test or use push notifications, build a custom development client.
Set up Firebase for push notifications:
await SDK.initializeFirebase();Request permission to send notifications (required for iOS):
const permissionGranted = await SDK.requestNotificationPermission();Retrieve the Firebase token for the device:
const token = await SDK.getFirebaseToken();Send the Firebase token to your backend:
await SDK.sendTokenToBackend(token);Set up a listener for incoming messages when the app is in the foreground:
const unsubscribe = SDK.onMessage(async remoteMessage => {
console.log('Received foreground message:', remoteMessage);
// Handle the message
});Set up a listener for when a notification opens the app from the background:
SDK.onNotificationOpenedApp(remoteMessage => {
console.log('Notification opened app from background state:', remoteMessage);
// Handle the notification
});Check if the app was opened from a notification when it was in a quit state:
const initialNotification = await SDK.getInitialNotification();
if (initialNotification) {
console.log('App opened from quit state by notification:', initialNotification);
// Handle the initial notification
}Here's a comprehensive example of how to use the GoPersonal SDK in a React Native application:
import SDK from 'gopersonal-react-native-sdk';
import { useEffect } from 'react';
const App = () => {
useEffect(() => {
const initializeSDK = async () => {
try {
// Initialize the SDK
await SDK.init('BR-your-client-id', 'your-client-secret', { debug: true });
// Log in the user
await SDK.login('customer-id', { additionalData: 'value' });
// Perform a search query
const searchResults = await SDK.search('search query');
console.log('Search Results:', searchResults);
// Retrieve personalized content
const content = await SDK.getContent('content-id');
console.log('Content:', content);
// Capture a user interaction
await SDK.addInteraction('view', { someData: 'value' });
// Set up push notifications
await SDK.initializeFirebase();
const permissionGranted = await SDK.requestNotificationPermission();
if (permissionGranted) {
const token = await SDK.getFirebaseToken();
await SDK.sendTokenToBackend(token);
SDK.onMessage(async remoteMessage => {
console.log('Received foreground message:', remoteMessage);
});
SDK.onNotificationOpenedApp(remoteMessage => {
console.log('Notification opened app from background state:', remoteMessage);
});
const initialNotification = await SDK.getInitialNotification();
if (initialNotification) {
console.log('App opened from quit state by notification:', initialNotification);
}
}
} catch (error) {
console.error('Error initializing SDK:', error);
}
};
initializeSDK();
}, []);
return (
// Your React Native components go here
<></>
);
};
export default App;