salsify-experiences-sdk
Version:
SDK to be used by commerce websites to implement product experiences.
219 lines • 7.23 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const enhancedContent_1 = __importStar(require("./enhancedContent"));
const events_1 = __importDefault(require("./events"));
const cookies_1 = require("./utils/cookies");
const logger_1 = require("./utils/logger");
const time_on_page_tracker_1 = __importDefault(require("./utils/time-on-page-tracker"));
const version_1 = require("./version");
const runtime_1 = require("./utils/runtime");
const sessionIdKey = 'salsify_session_id';
const defaultOptions = {
languageCode: 'en-US',
tracking: true,
};
const defaultEcSettings = {
idType: 'SDKID',
};
/**
* The Salsify Experiences SDK.
*
* This class is responsible for initializing the SDK and exposing the public getter methods for each different available API.
*/
class SdkApi {
#initialized = false;
#settings;
#ecApi;
#eventsApi;
#context;
#stalePageSessionId = false;
#timeOnPageTracker;
#jsSource;
/** @internal */
constructor(jsSource) {
this.#jsSource = jsSource;
}
/**
* Initializes the SDK.
*
* @example
* ```javascript
* window.salsifyExperiencesSdk.init({ clientId, enhancedContent: { idType }})
* ```
*
* @param options The options to initialize the SDK with.
*/
init(options) {
if (this.#initialized) {
throw new Error('Salsify Experiences SDK has already been initialized.');
}
const ecSettings = {
...defaultEcSettings,
...options.enhancedContent,
};
this.#settings = {
...defaultOptions,
...options,
enhancedContent: ecSettings,
};
this.#context = {
url: (0, runtime_1.inBrowser)() ? window.location.href : undefined,
sessionId: this.#updateSessionId(this.#settings.tracking),
pageSessionId: (0, runtime_1.inBrowser)() ? crypto.randomUUID?.() : undefined,
tracking: this.#settings.tracking,
clientId: this.#settings.clientId,
languageCode: this.#settings.languageCode,
enhancedContent: this.#settings.enhancedContent,
version: version_1.SDK_VERSION,
jsSource: this.#jsSource,
};
if ((0, runtime_1.inBrowser)()) {
(0, enhancedContent_1.attachIframeContextListener)(this.#context);
}
const logger = (0, logger_1.createLogger)(this.#context, this.#settings);
logger.log('init', this.#settings);
this.#ecApi = new enhancedContent_1.default(this.#settings, this.#context, logger, {
beforeRender: this.#beforeRender.bind(this),
afterRender: this.#afterRender.bind(this),
});
if ((0, runtime_1.inBrowser)()) {
this.#timeOnPageTracker = new time_on_page_tracker_1.default(logger, this.#ecApi);
this.#timeOnPageTracker.start();
}
this.#eventsApi = new events_1.default(logger, { beforeNavigation: this.#beforeNavigation.bind(this) }, this.#ecApi);
this.#initialized = true;
}
/**
* Whether the SDK has been initialized.
*
* @example
* ```javascript
* const salsify = window.salsifyExperiencesSdk;
* salsify.initialized; // false
* salsify.init({ clientId, enhancedContent: { idType } });
* salsify.initialized; // true
* ```
*/
get initialized() {
return this.#initialized;
}
/**
* The initialized Enhanced Content API.
*
* Throws an error if the SDK has not been initialized.
*
* @example
* ```javascript
* const salsify = window.salsifyExperiencesSdk;
* const ec = salsify.enhancedContent;
* ```
*/
get enhancedContent() {
if (!this.#ecApi) {
throw new Error('Salsify Experiences SDK has not been initialized.');
}
return this.#ecApi;
}
/**
* The initialized Events API.
*
* Throws an error if the SDK has not been initialized.
*
* @example
* ```javascript
* const salsify = window.salsifyExperiencesSdk;
* const events = salsify.events;
* ```
*/
get events() {
if (!this.#eventsApi) {
throw new Error('Salsify Experiences SDK has not been initialized.');
}
return this.#eventsApi;
}
#getOrCreateSessionId() {
let id = (0, cookies_1.getCookie)(sessionIdKey);
if (!id) {
id = crypto.randomUUID?.();
if (id) {
(0, cookies_1.setCookie)(sessionIdKey, id);
}
}
return id;
}
#removeSessionId() {
(0, cookies_1.deleteCookie)(sessionIdKey);
}
#resetPageSessionId() {
if (this.#context) {
this.#context.pageSessionId = crypto.randomUUID?.();
this.#stalePageSessionId = false;
}
}
#beforeRender() {
if (!(0, runtime_1.inBrowser)())
return;
if (this.#context) {
this.#context.url = window.location.href;
}
if (this.#stalePageSessionId) {
this.#resetPageSessionId();
}
}
#afterRender() {
if ((0, runtime_1.inBrowser)()) {
this.#stalePageSessionId = true;
}
}
#beforeNavigation() {
if (!(0, runtime_1.inBrowser)())
return;
if (this.#context) {
this.#context.url = window.location.href;
}
if (this.#stalePageSessionId) {
this.#timeOnPageTracker?.sendEvent();
this.#timeOnPageTracker?.restart();
this.#resetPageSessionId();
}
}
#updateSessionId(tracking) {
if ((0, runtime_1.inBrowser)()) {
if (tracking) {
return this.#getOrCreateSessionId();
}
else {
this.#removeSessionId();
return 'NOT_TRACKED';
}
}
}
}
exports.default = SdkApi;
//# sourceMappingURL=api.js.map