react-native-appstack-sdk
Version:
React Native bridge for Appstack iOS SDK - Track events and revenue with SKAdNetwork integration
155 lines (145 loc) • 5.21 kB
JavaScript
import { NativeModules, Platform } from 'react-native';
import { EventType } from './types';
// Lazy evaluation of LINKING_ERROR to avoid calling Platform.select during module initialization
const getLinkingError = () => {
return `The package 'react-native-appstack-sdk' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
ios: "- You have run 'cd ios && pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
};
const AppstackReactNative = NativeModules.AppstackReactNative ? NativeModules.AppstackReactNative : new Proxy({}, {
get() {
throw new Error(getLinkingError());
}
});
/**
* Main Appstack SDK class for React Native
*
* Usage example:
* ```typescript
* import AppstackSDK from 'react-native-appstack-sdk';
*
* // Configure the SDK (basic)
* await AppstackSDK.configure('your-api-key');
*
* // Configure the SDK (with all parameters)
* await AppstackSDK.configure(
* 'your-api-key',
* true, // isDebug
* 'https://api.event.dev.appstack.tech/android/', // endpointBaseUrl
* 0 // logLevel (DEBUG)
* );
*
* // Send events
* await AppstackSDK.sendEvent('PURCHASE'); // Without parameters
* await AppstackSDK.sendEvent('PURCHASE', null, { revenue: 29.99, currency: 'USD' }); // With parameters
*
* // Enable Apple Ads Attribution (iOS only)
* if (Platform.OS === 'ios') {
* await AppstackSDK.enableAppleAdsAttribution();
* }
* ```
*/
class AppstackSDK {
constructor() {}
/**
* Get the singleton instance of the SDK
*/
static getInstance() {
if (!AppstackSDK.instance) {
AppstackSDK.instance = new AppstackSDK();
}
return AppstackSDK.instance;
}
/**
* Configure Appstack SDK with your API key and optional parameters
*/
async configure(apiKey, isDebug = false, endpointBaseUrl, logLevel = 1) {
if (!apiKey || typeof apiKey !== 'string' || apiKey.trim() === '') {
throw new Error('API key must be a non-empty string');
}
if (typeof isDebug !== 'boolean') {
throw new Error('isDebug must be a boolean');
}
if (endpointBaseUrl !== undefined && (typeof endpointBaseUrl !== 'string' || endpointBaseUrl.trim() === '')) {
throw new Error('endpointBaseUrl must be a non-empty string or undefined');
}
if (typeof logLevel !== 'number' || logLevel < 0 || logLevel > 3) {
throw new Error('logLevel must be a number between 0 and 3');
}
try {
const result = await AppstackReactNative.configure(apiKey.trim(), isDebug, (endpointBaseUrl === null || endpointBaseUrl === void 0 ? void 0 : endpointBaseUrl.trim()) || null, logLevel);
return result;
} catch (error) {
console.error('Failed to configure Appstack SDK:', error);
throw error;
}
}
/**
* Send an event with optional parameters
*/
async sendEvent(eventType, eventName, parameters) {
// Validate that at least one of eventName or eventType is provided
if ((!eventName || eventName.trim() === '') && (!eventType || eventType.toString().trim() === '')) {
throw new Error('Either eventName or eventType must be provided');
}
try {
// Convert eventType to string if it's an enum
const eventTypeString = eventType ? eventType.toString() : null;
return await AppstackReactNative.sendEvent((eventTypeString === null || eventTypeString === void 0 ? void 0 : eventTypeString.trim()) || null, (eventName === null || eventName === void 0 ? void 0 : eventName.trim()) || null, parameters || null);
} catch (error) {
console.error(`Failed to send event (eventType: '${eventType}', eventName: '${eventName}'):`, error);
throw error;
}
}
/**
* Enable Apple Ads Attribution tracking
*/
async enableAppleAdsAttribution() {
if (Platform.OS !== 'ios') {
console.warn('Apple Ads Attribution is only available on iOS');
return false;
}
try {
return await AppstackReactNative.enableAppleAdsAttribution();
} catch (error) {
console.error('Failed to enable Apple Ads Attribution:', error);
throw error;
}
}
/**
* Get the Appstack ID for the current user/device
*/
async getAppstackId() {
try {
return await AppstackReactNative.getAppstackId();
} catch (error) {
console.error('Failed to get Appstack ID:', error);
throw error;
}
}
/**
* Check if the SDK is disabled
*/
async isSdkDisabled() {
try {
const isDisabled = await AppstackReactNative.isSdkDisabled();
if (isDisabled) {
console.warn('⚠️ Appstack SDK is currently disabled. All SDK operations will be skipped. Please check your API key and try again.');
}
return isDisabled;
} catch (error) {
console.error('Failed to check if SDK is disabled:', error);
throw error;
}
}
}
// Export the singleton instance
const appstackSDK = AppstackSDK.getInstance();
export default appstackSDK;
// Also export the class for advanced use cases
export { AppstackSDK };
// Export the EventType enum
export { EventType };
// Types are already exported automatically with interfaces
//# sourceMappingURL=index.js.map