// // KochavaTracker (ReactNative) // // Copyright (c) 2018 - 2023 Kochava, Inc. All rights reserved. // // Imports import { NativeModules, NativeEventEmitter } from 'react-native'; // Native Tracker Instance. const { RNKochavaTracker } = NativeModules; const RNKochavaTrackerEvent = RNKochavaTracker != null ? new NativeEventEmitter(NativeModules.RNKochavaTracker) : null; // // Log Levels // // Defaults to Info // export enum KochavaTrackerLogLevel { None = "none", Error = "error", Warn = "warn", Info = "info", Debug = "debug", Trace = "trace" } // // Standard Event Types // // For samples and expected usage see: https://support.kochava.com/reference-information/post-install-event-examples/ // export enum KochavaTrackerEventType { Achievement = "Achievement", AddToCart = "Add to Cart", AddToWishList = "Add to Wish List", CheckoutStart = "Checkout Start", LevelComplete = "Level Complete", Purchase = "Purchase", Rating = "Rating", RegistrationComplete = "Registration Complete", Search = "Search", TutorialComplete = "Tutorial Complete", View = "View", AdView = "Ad View", PushReceived = "Push Received", PushOpened = "Push Opened", ConsentGranted = "Consent Granted", Deeplink = "_Deeplink", AdClick = "Ad Click", StartTrial = "Start Trial", Subscribe = "Subscribe" } // // Kochava Tracker Event // export class KochavaTrackerEvent { _eventName: string; _eventData: any = {}; _iosAppStoreReceiptBase64String: string | null = null; _androidGooglePlayReceiptData: string | null = null; _androidGooglePlayReceiptSignature:string | null = null; // Constructor constructor(eventName: string) { this._eventName = eventName; } // Send the event. send(): void { KochavaTracker.instance.sendEventWithEvent(this); } // Set a custom key/value on the event where the type of the value is a string. setCustomStringValue(key: string, value: string): void { if(key && value && typeof(value) === "string") { this._eventData[key] = value; } } // Set a custom key/value on the event where the type of the value is a boolean. setCustomBoolValue(key: string, value: boolean): void { if(key && value != null && typeof(value) === "boolean") { this._eventData[key] = value; } } // Set a custom key/value on the event where the type of the value is a number. setCustomNumberValue(key: string, value: number): void { if(key && value != null && typeof(value) === "number") { this._eventData[key] = value; } } // (Internal) Set a custom key/value on the event where the type of the value is a dictionary. _setCustomDictionaryValue(key: string, value: object): void { if(key && value != null && typeof(value) === "object") { this._eventData[key] = value; } } // (Android Only) Set the receipt from the Android Google Play Store. setAndroidGooglePlayReceipt(data: string, signature: string): void { if(data && typeof(data) === "string" && signature && typeof(signature) === "string") { this._androidGooglePlayReceiptData = data; this._androidGooglePlayReceiptSignature = signature; } } // (iOS Only) Set the receipt from the iOS Apple App Store. setIosAppStoreReceipt(base64String: string): void { if(base64String && typeof(base64String) === "string") { this._iosAppStoreReceiptBase64String = base64String; } } // // Standard Event Parameters. // setAction(value: string): void { this.setCustomStringValue("action", value); } setBackground(value: boolean): void { this.setCustomBoolValue("background", value); } setCheckoutAsGuest(value: string): void { this.setCustomStringValue("checkout_as_guest", value); } setCompleted(value: boolean): void { this.setCustomBoolValue("completed", value); } setContentId(value: string): void { this.setCustomStringValue("content_id", value); } setContentType(value: string): void { this.setCustomStringValue("content_type", value); } setCurrency(value: string): void { this.setCustomStringValue("currency", value); } setDate(value: string): void { this.setCustomStringValue("date", value); } setDescription(value: string): void { this.setCustomStringValue("description", value); } setDestination(value: string): void { this.setCustomStringValue("destination", value); } setDuration(value: number): void { this.setCustomNumberValue("duration", value); } setEndDate(value: string): void { this.setCustomStringValue("end_date", value); } setItemAddedFrom(value: string): void { this.setCustomStringValue("item_added_from", value); } setLevel(value: string): void { this.setCustomStringValue("level", value); } setMaxRatingValue(value: number): void { this.setCustomNumberValue("max_rating_value", value); } setName(value: string): void { this.setCustomStringValue("name", value); } setOrderId(value: string): void { this.setCustomStringValue("order_id", value); } setOrigin(value: string): void { this.setCustomStringValue("origin", value); } setPayload(value: object): void { this._setCustomDictionaryValue("payload", value); } setPrice(value: number): void { this.setCustomNumberValue("price", value); } setQuantity(value: number): void { this.setCustomNumberValue("quantity", value); } setRatingValue(value: number): void { this.setCustomNumberValue("rating_value", value); } setReceiptId(value: string): void { this.setCustomStringValue("receipt_id", value); } setReferralFrom(value: string): void { this.setCustomStringValue("referral_from", value); } setRegistrationMethod(value: string): void { this.setCustomStringValue("registration_method", value); } setResults(value: string): void { this.setCustomStringValue("results", value); } setScore(value: string): void { this.setCustomStringValue("score", value); } setSearchTerm(value: string): void { this.setCustomStringValue("search_term", value); } setSource(value: string): void { this.setCustomStringValue("source", value); } setSpatialX(value: number): void { this.setCustomNumberValue("spatial_x", value); } setSpatialY(value: number): void { this.setCustomNumberValue("spatial_y", value); } setSpatialZ(value: number): void { this.setCustomNumberValue("spatial_z", value); } setStartDate(value: string): void { this.setCustomStringValue("start_date", value); } setSuccess(value: string): void { this.setCustomStringValue("success", value); } setUri(value: string): void { this.setCustomStringValue("uri", value); } setUserId(value: string): void { this.setCustomStringValue("user_id", value); } setUserName(value: string): void { this.setCustomStringValue("user_name", value); } setValidated(value: string): void { this.setCustomStringValue("validated", value); } // // Ad LTV Event Parameters // setAdCampaignId(value: string): void { this.setCustomStringValue("ad_campaign_id", value); } setAdCampaignName(value: string): void { this.setCustomStringValue("ad_campaign_name", value); } setAdDeviceType(value: string): void { this.setCustomStringValue("device_type", value); } setAdGroupId(value: string): void { this.setCustomStringValue("ad_group_id", value); } setAdGroupName(value: string): void { this.setCustomStringValue("ad_group_name", value); } setAdMediationName(value: string): void { this.setCustomStringValue("ad_mediation_name", value); } setAdNetworkName(value: string): void { this.setCustomStringValue("ad_network_name", value); } setAdPlacement(value: string): void { this.setCustomStringValue("placement", value); } setAdSize(value: string): void { this.setCustomStringValue("ad_size", value); } setAdType(value: string): void { this.setCustomStringValue("ad_type", value); } // Return all the event info in the form to pass down to the native layer. getData(): object { return { "name": this._eventName, "data": this._eventData, "iosAppStoreReceiptBase64String": this._iosAppStoreReceiptBase64String, "androidGooglePlayReceiptData": this._androidGooglePlayReceiptData, "androidGooglePlayReceiptSignature": this._androidGooglePlayReceiptSignature }; } } // // Kochava Tracker Install Attribution Result // export class KochavaTrackerInstallAttribution { retrieved: boolean; raw: object; attributed: boolean; firstInstall: boolean; // Constructor constructor(serializedData: string | null) { if(serializedData == null) { this.retrieved = false; this.raw = {}; this.attributed = false; this.firstInstall = false; } else { var data = JSON.parse(serializedData); this.retrieved = data["retrieved"] ?? false; this.raw = data["raw"] ?? {}; this.attributed = data["attributed"] ?? false; this.firstInstall = data["firstInstall"] ?? false; } } } // // Kochava Tracker Deeplink Result // export class KochavaTrackerDeeplink { destination: string; raw: object; // Constructor constructor(serializedData: string | null) { if(serializedData == null) { this.destination = ""; this.raw = {}; } else { var data = JSON.parse(serializedData); this.destination = data["destination"] ?? ""; this.raw = data["raw"] ?? {}; } } } // // Kochava Tracker Init Result // export class KochavaTrackerInit { consentGdprApplies: boolean; // Constructor constructor(serializedData: string | null) { if(serializedData == null) { this.consentGdprApplies = false; } else { var data = JSON.parse(serializedData); this.consentGdprApplies = data["consentGdprApplies"] ?? false; } } } // // Kochava Tracker Init Completed // type KochavaTrackerInitCompletedListener = (init: KochavaTrackerInit) => void; // // Kochava Tracker SDK // // A lightweight and easy to integrate SDK, providing first-class integration with Kochava’s installation attribution and analytics platform. // Getting Started: https://support.kochava.com/sdk-integration/reactnative-sdk-integration/ // export class KochavaTracker { // Singleton Instance static instance: KochavaTracker = new KochavaTracker(); // Internal State _registeredAndroidAppGuid: string | null = null; _registeredIosAppGuid: string | null = null; _registeredPartnerName: string | null = null; // Callback handler _initCompletedListener: KochavaTrackerInitCompletedListener | null = null; _initCompletedListenerSubscription = RNKochavaTrackerEvent != null ? RNKochavaTrackerEvent.addListener("KochavaTrackerInitCompleted", (event) => { if(this._initCompletedListener == null) { return; } this._initCompletedListener(new KochavaTrackerInit(event)) }) : null; // Reserved function, only use if directed to by your Client Success Manager. executeAdvancedInstruction(name: string, value: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.executeAdvancedInstruction(name, value); } // Set the log level. This should be set prior to starting the SDK. setLogLevel(logLevel: KochavaTrackerLogLevel): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setLogLevel(logLevel); } // Set the sleep state. setSleep(sleep: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setSleep(sleep); } // Set if app level advertising tracking should be limited. setAppLimitAdTracking(appLimitAdTracking: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setAppLimitAdTracking(appLimitAdTracking); } // Register a custom device identifier for install attribution. registerCustomDeviceIdentifier(name: string, value: string | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerCustomDeviceIdentifier(name, value); } // Register a custom value to be included in SDK payloads. registerCustomStringValue(name: string, value: string | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerCustomStringValue(name, value); } // Register a custom value to be included in SDK payloads. registerCustomBoolValue(name: string, value: boolean | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerCustomBoolValue(name, value); } // Register a custom value to be included in SDK payloads. registerCustomNumberValue(name: string, value: number | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerCustomNumberValue(name, value); } // Register an Identity Link that allows linking different identities together in the form of key and value pairs. registerIdentityLink(name: string, value: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerIdentityLink(name, value); } // (Android Only) Enable the Instant App feature by setting the instant app guid. enableAndroidInstantApps(instantAppGuid: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.enableAndroidInstantApps(instantAppGuid); } // (iOS Only) Enable App Clips by setting the Container App Group Identifier for App Clips data migration. enableIosAppClips(containerAppGroupIdentifier: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.enableIosAppClips(containerAppGroupIdentifier); } // (iOS Only) Enable App Tracking Transparency. enableIosAtt(): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.enableIosAtt(); } // (iOS Only) Set the amount of time in seconds to wait for App Tracking Transparency Authorization. Default 30 seconds. setIosAttAuthorizationWaitTime(waitTime: number): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setIosAttAuthorizationWaitTime(waitTime); } // (iOS Only) Set if the SDK should automatically request App Tracking Transparency Authorization on start. Default true. setIosAttAuthorizationAutoRequest(autoRequest: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setIosAttAuthorizationAutoRequest(autoRequest); } // Register a privacy profile, creating or overwriting an existing pofile. registerPrivacyProfile(name: string, keys: string[]): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerPrivacyProfile(name, JSON.stringify(keys ?? [])); } // Enable or disable an existing privacy profile. setPrivacyProfileEnabled(name: string, enabled: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setPrivacyProfileEnabled(name, enabled); } // Set the init completed callback listener. setInitCompletedListener(initCompletedListener: KochavaTrackerInitCompletedListener | null): void { if(RNKochavaTracker == null) { return; } this._initCompletedListener = initCompletedListener; RNKochavaTracker.setInitCompletedListener(initCompletedListener != null); } // Set if consent has been explicitly opted in or out by the user. setIntelligentConsentGranted(granted: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setIntelligentConsentGranted(granted); } // Return if the SDK is currently started. getStarted(): Promise { if(RNKochavaTracker == null) { return Promise.resolve(false); } return RNKochavaTracker.getStarted(); } // Register the Android App GUID. Do this prior to calling Start. registerAndroidAppGuid(androidAppGuid: string): void { this._registeredAndroidAppGuid = androidAppGuid; } // Register the iOS App GUID. Do this prior to calling Start. registerIosAppGuid(iosAppGuid: string): void { this._registeredIosAppGuid = iosAppGuid; } // Register your Partner Name. Do this prior to calling Start. // // NOTE: Only use this method if directed to by your Client Success Manager. registerPartnerName(partnerName: string): void { this._registeredPartnerName = partnerName; } // Start the SDK with the previously registered App GUID or Partner Name. start(): void { if(RNKochavaTracker == null) { console.log("KVA/Tracker: ERROR: Attempting to start the SDK on an unsupported platform."); return; } let wrapper = { name: "ReactNative", version: "2.8.2", build_date: "2024-10-14T22:16:48Z" }; RNKochavaTracker.executeAdvancedInstruction("wrapper", JSON.stringify(wrapper)); RNKochavaTracker.start(this._registeredAndroidAppGuid, this._registeredIosAppGuid, this._registeredPartnerName); } // Shut down the SDK and optionally delete all local SDK data. // // NOTE: Care should be taken when using this method as deleting the SDK data will make it reset back to a first install state. shutdown(deleteData: boolean): void { if(RNKochavaTracker == null) { return; } this._registeredAndroidAppGuid = null; this._registeredIosAppGuid = null; this._registeredPartnerName = null; RNKochavaTracker.shutdown(deleteData); } // Return the Kochava Device ID. getDeviceId(): Promise { if(RNKochavaTracker == null) { return Promise.resolve(""); } return RNKochavaTracker.getDeviceId(); } // Return the currently retrieved install attribution data. getInstallAttribution(): Promise { return new Promise(function(resolve, reject) { if(RNKochavaTracker == null) { return resolve(new KochavaTrackerInstallAttribution(null)); } RNKochavaTracker.getInstallAttribution().then((value: string) => { resolve(new KochavaTrackerInstallAttribution(value)); }).catch((error) => { reject(error) }); }); } // Retrieve install attribution data from the server. retrieveInstallAttribution(): Promise { return new Promise(function(resolve, reject) { if(RNKochavaTracker == null) { return resolve(new KochavaTrackerInstallAttribution(null)); } RNKochavaTracker.retrieveInstallAttribution().then((value: string) => { resolve(new KochavaTrackerInstallAttribution(value)); }).catch((error) => { reject(error) }); }); } // Process a launch deeplink using the default 10 second timeout. processDeeplink(path: string): Promise { return new Promise(function(resolve, reject) { if(RNKochavaTracker == null) { return resolve(new KochavaTrackerDeeplink(null)); } RNKochavaTracker.processDeeplink(path).then((value: string) => { resolve(new KochavaTrackerDeeplink(value)); }).catch((error) => { reject(error) }); }); } // Process a launch deeplink using a custom timeout in seconds. processDeeplinkWithOverrideTimeout(path: string, timeout: number): Promise { return new Promise(function(resolve, reject) { if(RNKochavaTracker == null) { return resolve(new KochavaTrackerDeeplink(null)); } RNKochavaTracker.processDeeplinkWithOverrideTimeout(path, timeout).then((value: string) => { resolve(new KochavaTrackerDeeplink(value)); }).catch((error) => { reject(error) }); }); } // Register the push token. registerPushToken(token: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerPushToken(token); } // Enable or disable the use of the push token. setPushEnabled(enabled: boolean): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.setPushEnabled(enabled); } // Registers a default parameter on every event. registerDefaultEventStringParameter(name: string, value: string | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerDefaultEventStringParameter(name, value); } // Registers a default parameter on every event. registerDefaultEventBoolParameter(name: string, value: boolean | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerDefaultEventBoolParameter(name, value); } // Registers a default parameter on every event. registerDefaultEventNumberParameter(name: string, value: number | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerDefaultEventNumberParameter(name, value); } // Registers a default user_id value on every event. registerDefaultEventUserId(value: string | null): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.registerDefaultEventUserId(value); } // Send an event. sendEvent(name: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.sendEvent(name); } // Send an event with string data. sendEventWithString(name: string, data: string): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.sendEventWithString(name, data); } // Send an event with dictionary data. sendEventWithDictionary(name: string, data: object): void { if(RNKochavaTracker == null) { return; } RNKochavaTracker.sendEventWithDictionary(name, JSON.stringify(data ?? {})); } // (Internal) Send an event object (Called via Event.send()). sendEventWithEvent(event: KochavaTrackerEvent): void { if(RNKochavaTracker == null) { return; } if(event == null) { return; } RNKochavaTracker.sendEventWithEvent(JSON.stringify(event.getData())); } // Build and return an event using a Standard Event Type. buildEventWithEventType(type: KochavaTrackerEventType): KochavaTrackerEvent { return new KochavaTrackerEvent(type); } // Build and return an event using a custom name. buildEventWithEventName(name: string): KochavaTrackerEvent { return new KochavaTrackerEvent(name); } }