@perceptr/web-sdk
Version:
Perceptr Web SDK for recording and monitoring user sessions
51 lines (50 loc) • 1.37 kB
JavaScript
import { Core } from "./SessionCore";
import { logger } from "./utils/logger";
class PerceptrSDK {
constructor() {
this.initialized = false;
// Private constructor to prevent direct instantiation
}
static getInstance() {
if (!PerceptrSDK.instance) {
PerceptrSDK.instance = new PerceptrSDK();
}
return PerceptrSDK.instance;
}
init(config) {
if (this.initialized) {
logger.forceLog("warn", "SDK already initialized");
return;
}
this.core = new Core(config);
this.initialized = true;
logger.configure({ debug: !!config.debug });
}
start() {
this.ensureInitialized();
return this.core.start();
}
stop() {
this.ensureInitialized();
return this.core.stop();
}
pause() {
this.ensureInitialized();
this.core.pause();
}
resume() {
this.ensureInitialized();
this.core.resume();
}
identify(distinctId, traits = {}) {
this.ensureInitialized();
return this.core.identify(distinctId, traits);
}
ensureInitialized() {
if (!this.initialized) {
throw new Error("[SDK] SDK not initialized. Call init() first");
}
}
}
// Export a singleton instance
export default PerceptrSDK.getInstance();