klaviyo-react-native-sdk
Version:
Official Klaviyo React Native SDK
154 lines (149 loc) • 5.54 kB
JavaScript
;
import { KlaviyoReactNativeSdk } from "./KlaviyoReactNativeSdk.js";
import { formatProfile } from "./Profile.js";
import { parseFormLifecycleEvent } from "./Forms.js";
import { NativeEventEmitter, NativeModules } from 'react-native';
const FORMS_UNAVAILABLE_MESSAGE = 'Klaviyo In-App Forms is not available. The KlaviyoForms module was not included in this build. ' + 'To enable forms, ensure KLAVIYO_INCLUDE_FORMS is not set to "false" in your Podfile (iOS) ' + 'and klaviyoIncludeForms is not set to false in gradle.properties (Android).';
const LOCATION_UNAVAILABLE_MESSAGE = 'Klaviyo Location (geofencing) is not available. The KlaviyoLocation module was not included in this build. ' + 'To enable location, ensure KLAVIYO_INCLUDE_LOCATION is not set to "false" in your Podfile (iOS) ' + 'and klaviyoIncludeLocation is not set to false in gradle.properties (Android).';
function isFormsAvailable() {
const constants = KlaviyoReactNativeSdk.getConstants?.() ?? {};
if (constants.FORMS_AVAILABLE === false) {
console.error(`[Klaviyo] ${FORMS_UNAVAILABLE_MESSAGE}`);
return false;
}
return true;
}
function isLocationAvailable() {
const constants = KlaviyoReactNativeSdk.getConstants?.() ?? {};
if (constants.LOCATION_AVAILABLE === false) {
console.error(`[Klaviyo] ${LOCATION_UNAVAILABLE_MESSAGE}`);
return false;
}
return true;
}
// Track active lifecycle subscription to prevent duplicate listeners
let activeLifecycleSubscription = null;
/**
* Implementation of the {@link KlaviyoInterface}
*/
export const Klaviyo = {
initialize(apiKey) {
KlaviyoReactNativeSdk.initialize(apiKey);
},
setProfile(profile) {
KlaviyoReactNativeSdk.setProfile(formatProfile(profile));
},
setExternalId(externalId) {
KlaviyoReactNativeSdk.setExternalId(externalId);
},
getExternalId(callback) {
return KlaviyoReactNativeSdk.getExternalId(callback);
},
setEmail(email) {
KlaviyoReactNativeSdk.setEmail(email);
},
getEmail(callback) {
return KlaviyoReactNativeSdk.getEmail(callback);
},
setPhoneNumber(phoneNumber) {
KlaviyoReactNativeSdk.setPhoneNumber(phoneNumber);
},
getPhoneNumber(callback) {
return KlaviyoReactNativeSdk.getPhoneNumber(callback);
},
setProfileAttribute(propertyKey, value) {
KlaviyoReactNativeSdk.setProfileAttribute(propertyKey, value);
},
setBadgeCount(count) {
// checking if method exists since this is iOS only and don't want a
// runtime error on android
if (KlaviyoReactNativeSdk.setBadgeCount) {
KlaviyoReactNativeSdk.setBadgeCount(count);
} else {
console.log('setBadgeCount is not available on this platform');
}
},
resetProfile() {
KlaviyoReactNativeSdk.resetProfile();
},
setPushToken(token) {
KlaviyoReactNativeSdk.setPushToken(token);
},
getPushToken(callback) {
return KlaviyoReactNativeSdk.getPushToken(callback);
},
createEvent(event) {
KlaviyoReactNativeSdk.createEvent(event);
},
registerForInAppForms(configuration) {
if (!isFormsAvailable()) return;
KlaviyoReactNativeSdk.registerForInAppForms(configuration);
},
unregisterFromInAppForms() {
if (!isFormsAvailable()) return;
KlaviyoReactNativeSdk.unregisterFromInAppForms();
},
registerGeofencing() {
if (!isLocationAvailable()) return;
KlaviyoReactNativeSdk.registerGeofencing();
},
unregisterGeofencing() {
if (!isLocationAvailable()) return;
KlaviyoReactNativeSdk.unregisterGeofencing();
},
getCurrentGeofences(callback) {
if (!isLocationAvailable()) {
callback({
geofences: []
});
return;
}
KlaviyoReactNativeSdk.getCurrentGeofences(callback);
},
/**
* Resolves a Klaviyo tracking link to a Universal Link URL,
* then handles navigation to the resolved URL.
* @param urlStr - The tracking link to be handled
*/
handleUniversalTrackingLink(urlStr) {
if (!urlStr || urlStr.trim() === '') {
console.error('[Klaviyo] Error: Empty tracking link provided');
return false;
}
// Validate that the URL is a Klaviyo universal tracking link using regex
// Pattern: https://domain/u/path
const klaviyoTrackingLinkPattern = /^https:\/\/[^/]+\/u\/.*$/;
if (!klaviyoTrackingLinkPattern.test(urlStr)) {
console.warn('[Klaviyo] Warning: Not a Klaviyo tracking link');
return false;
}
KlaviyoReactNativeSdk.handleUniversalTrackingLink(urlStr);
return true;
},
registerFormLifecycleHandler(handler) {
if (!isFormsAvailable()) return () => {};
// Clean up any existing subscription before re-registering
if (activeLifecycleSubscription) {
activeLifecycleSubscription.remove();
KlaviyoReactNativeSdk.unregisterFormLifecycleHandler();
activeLifecycleSubscription = null;
}
const eventEmitter = new NativeEventEmitter(NativeModules.KlaviyoReactNativeSdk);
activeLifecycleSubscription = eventEmitter.addListener('FormLifecycleEvent', data => {
const event = parseFormLifecycleEvent(data);
if (event !== null) {
handler(event);
}
});
KlaviyoReactNativeSdk.registerFormLifecycleHandler();
return () => {
activeLifecycleSubscription?.remove();
activeLifecycleSubscription = null;
KlaviyoReactNativeSdk.unregisterFormLifecycleHandler();
};
}
};
export { EventName } from "./Event.js";
export { ProfileProperty } from "./Profile.js";
export { FormLifecycleEventType } from "./Forms.js";
//# sourceMappingURL=index.js.map