airbridge-react-native-sdk
Version:
Airbridge SDK for React Native
419 lines (367 loc) • 13.8 kB
text/typescript
import { NativeModules } from 'react-native'
import { createAttributionModule } from './module/Attribution'
import { createDeeplinkModule } from './module/Deeplink'
import { createEventModule } from './module/Event'
import { createFetchModule } from './module/Fetch'
import { createPlacementModule } from './module/Placement'
import { createRegisterModule } from './module/Register'
import { createSwitchModule } from './module/Switch'
import { createWebInterfaceModule } from './module/WebInterface'
import { AirbridgeTrackingLink } from './type/AirbridgeTrackingLink'
import { check } from './utility/check'
import { AirbridgeTrackingBlocklist } from './module'
export const createDependency = () => {}
createDependency.Airbridge = () => {
if (!check.object(NativeModules.EventInteractor)) {
return undefined
}
return ({
attributionModule: createAttributionModule(),
deeplinkModule: createDeeplinkModule(),
eventModule: createEventModule(),
fetchModule: createFetchModule(),
placementModule: createPlacementModule(),
registerModule: createRegisterModule(),
switchModule: createSwitchModule(),
webInterfaceModule: createWebInterfaceModule(),
})
};
class Dependency {
static instance = createDependency.Airbridge()
}
export class Airbridge {
// attribution
/**
* Sets a listener for receiving attribution of install event.
* @param onReceived Map of attribution is delivered.
*/
static setOnAttributionReceived(
onReceived: (attribution: Record<string, string>) => void,
): void {
Dependency.instance?.attributionModule.setOnAttributionReceived(onReceived)
}
// deeplink
/**
* Handles deeplink and deferred-deeplink.
* @param onReceived URL of deeplink is delivered.
*/
static setOnDeeplinkReceived(
onReceived: (deeplink: string) => void,
): void {
Dependency.instance?.deeplinkModule.setOnDeeplinkReceived(onReceived)
}
// event
/**
* Tracks user behavior through event tracking with attributes.
* @param category Name of event.
* @param semanticAttributes Additional attributes of the event that defined by Airbridge.
* @param customAttributes Additional attributes of the event.
*/
static trackEvent(
category: string,
semanticAttributes?: Record<string, any>,
customAttributes?: Record<string, any>,
): void {
Dependency.instance?.eventModule.trackEvent(category, semanticAttributes, customAttributes)
}
// fetch
/**
* Fetch deviceUUID of SDK.
* @param onSuccess Callback to be invoked when deviceUUID is successfully handled.
* @param onFailure Callback to be invoked when any error occurs.
*/
static fetchDeviceUUID(
onSuccess: (deviceUUID: string) => void,
onFailure?: (error: Error) => void
): Promise<boolean> {
return Dependency.instance?.fetchModule.fetchDeviceUUID(onSuccess, onFailure) ?? Promise.resolve(false)
}
/**
* Fetch airbridgeGeneratedUUID of SDK.
* @param onSuccess Callback to be invoked when airbridgeGeneratedUUID is successfully handled.
* @param onFailure Callback to be invoked when any error occurs.
*/
static fetchAirbridgeGeneratedUUID(
onSuccess: (airbridgeGeneratedUUID: string) => void,
onFailure?: (error: Error) => void
): Promise<boolean> {
return Dependency.instance?.fetchModule.fetchAirbridgeGeneratedUUID(onSuccess, onFailure) ?? Promise.resolve(false)
}
/**
* Indicates whether notification was sent by Airbridge to track uninstall of app.
* @param notification The notification to check.
*/
static isUninstallTrackingNotification(
notification: Record<string, any>,
): boolean {
return Dependency.instance?.fetchModule.isUninstallTrackingNotification(notification) ?? false
}
// placement
/**
* Notifies that an in-app area within an app has been clicked on by the user.
* @param trackingLink tracking link uri
* @param onSuccess Callback to be invoked when tracking link is successfully handled.
* @param onFailure Callback to be invoked when any error occurs.
* @return `true` if all of the following conditions are met below, `false` otherwise.
* - If the SDK is initialized and enabled.
* - If tracking link is successfully handled.
*/
static click(
trackingLink: string,
onSuccess?: () => void,
onFailure?: (error: Error) => void
): Promise<boolean> {
return Dependency.instance?.placementModule.click(trackingLink, onSuccess, onFailure) ?? Promise.resolve(false)
}
/**
* Notifies that the in-app area within the app has been exposed to the user.
* @param trackingLink tracking link uri
* @param onSuccess Callback to be invoked when tracking link is successfully handled.
* @param onFailure Callback to be invoked when any error occurs.
* @return `true` if all of the following conditions are met below, `false` otherwise.
* - If the SDK is initialized and enabled.
* - If tracking link is successfully handled.
*/
static impression(
trackingLink: string,
onSuccess?: () => void,
onFailure?: ((error: Error) => void)
): Promise<boolean> {
return Dependency.instance?.placementModule.impression(trackingLink, onSuccess, onFailure) ?? Promise.resolve(false)
}
// register
/**
* Sets the user ID.
* @param id The user ID.
*/
static setUserID(id: string): void {
Dependency.instance?.registerModule.setUserID(id)
}
/**
* Clear the user ID.
*/
static clearUserID(): void {
Dependency.instance?.registerModule.clearUserID()
}
/**
* Sets the user email.
* @param email The user email.
*/
static setUserEmail(email: string): void {
Dependency.instance?.registerModule.setUserEmail(email)
}
/**
* Clear the user email.
*/
static clearUserEmail(): void {
Dependency.instance?.registerModule.clearUserEmail()
}
/**
* Sets the user phone number.
* @param phone The user phone number.
*/
static setUserPhone(phone: string): void {
Dependency.instance?.registerModule.setUserPhone(phone)
}
/**
* Clear the user phone number.
*/
static clearUserPhone(): void {
Dependency.instance?.registerModule.clearUserPhone()
}
/**
* Sets the key, value pair to the user attribute.
* @param key The key that uniquely identifies the user attribute.
* @param value The value to set for the user attribute.
*/
static setUserAttribute(key: string, value: any): void {
Dependency.instance?.registerModule.setUserAttribute(key, value)
}
/**
* Removes the user attribute with the given key.
* @param key The key that uniquely identifies the user attribute.
*/
static removeUserAttribute(key: string): void {
Dependency.instance?.registerModule.removeUserAttribute(key)
}
/**
* Clears all user attributes.
*/
static clearUserAttributes(): void {
Dependency.instance?.registerModule.clearUserAttributes()
}
/**
* Sets the key, value pair to the user alias.
* @param key The key that uniquely identifies the user alias.
* @param value The value to set for the user alias.
*/
static setUserAlias(key: string, value: string): void {
Dependency.instance?.registerModule.setUserAlias(key, value)
}
/**
* Removes the user alias with the given key.
* @param key The key that uniquely identifies the user alias.
*/
static removeUserAlias(key: string): void {
Dependency.instance?.registerModule.removeUserAlias(key)
}
/**
* Clears all user aliases.
*/
static clearUserAlias(): void {
Dependency.instance?.registerModule.clearUserAlias()
}
/**
* Clears all user information.
*/
static clearUser(): void {
Dependency.instance?.registerModule.clearUser()
}
/**
* Sets the key, value pair to the device alias.
* @param key The key that uniquely identifies the device alias.
* @param value The value to set for the device alias.
*/
static setDeviceAlias(key: string, value: string): void {
Dependency.instance?.registerModule.setDeviceAlias(key, value)
}
/**
* Removes the device alias with the given key.
* @param key The key that uniquely identifies the device alias.
*/
static removeDeviceAlias(key: string): void {
Dependency.instance?.registerModule.removeDeviceAlias(key)
}
/**
* Clears all device aliases.
*/
static clearDeviceAlias(): void {
Dependency.instance?.registerModule.clearDeviceAlias()
}
/**
* Registers the FCM or APNS registration token to track app uninstalls.
* @param token The FCM or APNS registration token.
*/
static registerPushToken(token: string): void {
Dependency.instance?.registerModule.registerPushToken(token)
}
// switch
/**
* Enables the SDK.
*/
static enableSDK(): void {
Dependency.instance?.switchModule.enableSDK()
}
/**
* Disables the SDK.
*/
static disableSDK(): void {
Dependency.instance?.switchModule.disableSDK()
}
/**
* Checks whether the SDK is currently enabled.
* @return `true` if the SDK is enabled, `false` otherwise.
*/
static isSDKEnabled(): Promise<boolean> {
return Dependency.instance?.switchModule.isSDKEnabled() ?? Promise.resolve(false)
}
/**
* Start collecting and transferring events.
*/
static startTracking(): void {
Dependency.instance?.switchModule.startTracking()
}
/**
* Stop collecting and transferring events.
*/
static stopTracking(): void {
Dependency.instance?.switchModule.stopTracking()
}
/**
* Checks whether the tracking feature of SDK is currently enabled.
* @return `true` if the tracking feature of SDK tracking is enabled,`false` otherwise.
*/
static isTrackingEnabled(): Promise<boolean> {
return Dependency.instance?.switchModule.isTrackingEnabled() ?? Promise.resolve(false)
}
// web interface
/**
* Creates a script that initialize the web interface.
* @param webToken The token to initialize Airbridge Web SDK.
* @param postMessageScript The JavaScript code to post commands from web to app.
* @return web interface script
*/
static createWebInterfaceScript(
webToken: string,
postMessageScript: string,
): Promise<string|undefined> {
return Dependency.instance?.webInterfaceModule.createWebInterfaceScript(webToken, postMessageScript) ?? Promise.resolve(undefined)
}
/**
* Handles commands from the web interface.
* @param command The command to handle.
*/
static handleWebInterfaceCommand(command: string): void {
Dependency.instance?.webInterfaceModule.handleWebInterfaceCommand(command)
}
/**
* Creates a tracking-link using airbridge-server that move user
* to specific page of app and track click-event.
*
* @param channel The channel of tracking-link.
* @param option The option to create tracking-link.
* @param onSuccess Created tracking-link is delivered if succeed.
* @param onFailure Error is delivered if failed.
*/
static createTrackingLink(
channel: string,
option: Record<string, any>,
onSuccess: (airbridgeTrackingLink: AirbridgeTrackingLink) => void,
onFailure?: (error: Error) => void
): void {
Dependency.instance?.placementModule.createTrackingLink(channel, option, onSuccess, onFailure)
}
/**
* Starts tracking event automatically for each user
* purchases product through in-app-purchase.
*/
static startInAppPurchaseTracking() {
Dependency.instance?.switchModule.startInAppPurchaseTracking();
}
/**
* Stops tracking event automatically for each user
* purchases product through in-app-purchase.
*/
static stopInAppPurchaseTracking() {
Dependency.instance?.switchModule.stopInAppPurchaseTracking();
}
/**
* Indicates that SDK can track event automatically for each user
* purchases product through in-app-purchase.
*
* @return `true` if the SDK is enabled for in-app-purchase tracking, `false` otherwise.
*/
static isInAppPurchaseTrackingEnabled(): Promise<boolean> {
return Dependency.instance?.switchModule.isInAppPurchaseTrackingEnabled() ?? Promise.resolve(false)
}
/**
* Allows tracking for the specified [AirbridgeTrackingBlocklist] item.
* This function removes the given item from the current TrackingBlocklist
* applied in the `airbridge.json`, enabling tracking at runtime.
*
* @param item The tracking blocklist item to allow.
*/
static allowTrackingItem(item: AirbridgeTrackingBlocklist) {
Dependency.instance?.switchModule.allowTrackingItem(item);
}
/**
* Blocks tracking for the specified [AirbridgeTrackingBlocklist] item.
* This function adds the given item to the current TrackingBlocklist
* applied in the `airbridge.json`, disabling tracking at runtime.
*
* @param item The tracking blocklist item to block.
*/
static blockTrackingItem(item: AirbridgeTrackingBlocklist) {
Dependency.instance?.switchModule.blockTrackingItem(item);
}
}