diff --git a/dist/api.types.js b/dist/api.types.js index 21ea311..5958ba5 100644 --- a/dist/api.types.js +++ b/dist/api.types.js @@ -7,4 +7,4 @@ var ConsoleLogLevel; ConsoleLogLevel["info"] = "info"; ConsoleLogLevel["warn"] = "warn"; ConsoleLogLevel["error"] = "error"; -})(ConsoleLogLevel || (exports.ConsoleLogLevel = ConsoleLogLevel = {})); +})(ConsoleLogLevel = exports.ConsoleLogLevel || (exports.ConsoleLogLevel = {})); diff --git a/dist/esm/api.d.ts b/dist/esm/api.d.ts new file mode 100644 index 0000000..6c4867f --- /dev/null +++ b/dist/esm/api.d.ts @@ -0,0 +1,18 @@ +import { ConsoleLogLevel } from "./api.types"; +export interface apiConfig { + init: (trackID: string, options?: object | null) => void; + getSessionURL: (callback?: void) => void; + identify: (data?: object) => void; + invalidateSession: () => void; + newPageView: (options?: object) => void; + setOptions: (options?: object) => void; + setCustomParams: (data?: object) => void; + off: () => void; + optOut: () => void; + debug: () => void; + track: (eventName: string, properties?: object) => void; + log: (logLevel: ConsoleLogLevel, ...rest: any) => void; +} +declare const api: apiConfig; +export declare const SDK_VERSION = "1.1.0"; +export default api; diff --git a/dist/esm/api.js b/dist/esm/api.js new file mode 100644 index 0000000..374413c --- /dev/null +++ b/dist/esm/api.js @@ -0,0 +1,19 @@ +const apiCall = (name, ...args) => { + return window.__ls(name, ...args); +}; +const api = { + init: (trackID, options) => apiCall("init", trackID, options), + getSessionURL: (callback) => apiCall("getSessionURL", callback), + identify: (data) => apiCall("identify", data), + invalidateSession: () => apiCall("invalidateSession"), + newPageView: (options) => apiCall("newPageView", options), + setOptions: (options) => apiCall("setOptions", options), + setCustomParams: (data) => apiCall("setCustomParams", data), + off: () => apiCall("off"), + optOut: () => apiCall("optOut", true), + debug: () => apiCall("debug", true), + track: (eventName, properties) => apiCall("track", eventName, properties), + log: (logLevel, ...rest) => apiCall("log", logLevel, rest), +}; +export const SDK_VERSION = "1.1.0"; +export default api; diff --git a/dist/esm/api.types.d.ts b/dist/esm/api.types.d.ts new file mode 100644 index 0000000..eb95064 --- /dev/null +++ b/dist/esm/api.types.d.ts @@ -0,0 +1,6 @@ +export declare enum ConsoleLogLevel { + log = "log", + info = "info", + warn = "warn", + error = "error" +} diff --git a/dist/esm/api.types.js b/dist/esm/api.types.js new file mode 100644 index 0000000..7e13488 --- /dev/null +++ b/dist/esm/api.types.js @@ -0,0 +1,7 @@ +export var ConsoleLogLevel; +(function (ConsoleLogLevel) { + ConsoleLogLevel["log"] = "log"; + ConsoleLogLevel["info"] = "info"; + ConsoleLogLevel["warn"] = "warn"; + ConsoleLogLevel["error"] = "error"; +})(ConsoleLogLevel || (ConsoleLogLevel = {})); diff --git a/dist/esm/index.d.ts b/dist/esm/index.d.ts new file mode 100644 index 0000000..9d4cf9a --- /dev/null +++ b/dist/esm/index.d.ts @@ -0,0 +1,23 @@ +declare global { + interface Window { + __ls: any; + } +} +declare const _default: { + init: (trackID: string, options?: object | null | undefined, sdkOptions?: { + devMode: boolean; + scriptURL: string; + }) => void; + getSessionURL: (args?: void | undefined) => any; + identify: (args?: object | undefined) => any; + invalidateSession: (args?: null | undefined) => any; + newPageView: (args?: object | undefined) => any; + setOptions: (args?: object | undefined) => any; + setCustomParams: (args?: object | undefined) => any; + off: (args?: null | undefined) => any; + optOut: (args?: null | undefined) => any; + debug: (args?: null | undefined) => any; + track: (eventName: string, properties?: object) => void; + log: (logLevel: string, ...args: any) => void; +}; +export default _default; diff --git a/dist/esm/index.js b/dist/esm/index.js new file mode 100644 index 0000000..2b23b28 --- /dev/null +++ b/dist/esm/index.js @@ -0,0 +1,69 @@ +import snippet, { defaultScriptURL } from "./snippet"; +import api from "./api"; +const sdkOptionsDefaults = { + devMode: false, + scriptURL: defaultScriptURL, +}; +let opts = Object.assign({}, sdkOptionsDefaults); +const isLoaded = () => window.__ls; +const getApiMethod = (name) => { + if (!isLoaded()) { + throw new Error("LiveSession is not loaded. Call init() before calling other API functions"); + } + const objectAPI = api; + if (!objectAPI.hasOwnProperty(name)) { + throw new Error(`method "${name}" doesn't exist`); + } + if (opts.devMode) { + const msg = `Skipping method: ${name}, devMode enabled`; + console.warn(msg); + return () => msg; + } + return objectAPI[name]; +}; +const safeCall = (name) => { + return (args) => { + const apiMethod = getApiMethod(name); + if (apiMethod) { + return apiMethod(args); + } + }; +}; +const safeCallManyArgs = (name) => { + return (...args) => { + const apiMethod = getApiMethod(name); + if (apiMethod) { + return apiMethod(...args); + } + }; +}; +const _init = (trackID, options, sdkOptions = sdkOptionsDefaults) => { + opts = Object.assign(Object.assign({}, sdkOptionsDefaults), sdkOptions); + if (isLoaded()) { + console.warn("LiveSession already inited (skipping init() call)"); + return; + } + if (!trackID) { + throw new Error(`trackID is required`); + } + snippet(window, document, "script", sdkOptions.scriptURL); + return api.init(trackID, options); +}; +export default { + init: _init, + getSessionURL: safeCall("getSessionURL"), + identify: safeCall("identify"), + invalidateSession: safeCall("invalidateSession"), + newPageView: safeCall("newPageView"), + setOptions: safeCall("setOptions"), + setCustomParams: safeCall("setCustomParams"), + off: safeCall("off"), + optOut: safeCall("optOut"), + debug: safeCall("debug"), + track: function (eventName, properties) { + safeCallManyArgs("track")(eventName, properties); + }, + log: function (logLevel, ...args) { + safeCallManyArgs("log")(logLevel, ...args); + }, +}; diff --git a/dist/esm/snippet.d.ts b/dist/esm/snippet.d.ts new file mode 100644 index 0000000..9829a56 --- /dev/null +++ b/dist/esm/snippet.d.ts @@ -0,0 +1,3 @@ +export declare const defaultScriptURL = "https://cdn.livesession.io/track.js"; +declare const snippet: (wnd?: Window & typeof globalThis, doc?: Document, type?: string, scriptURL?: string) => void; +export default snippet; diff --git a/dist/esm/snippet.js b/dist/esm/snippet.js new file mode 100644 index 0000000..a609a62 --- /dev/null +++ b/dist/esm/snippet.js @@ -0,0 +1,24 @@ +import { SDK_VERSION } from "./api"; +export const defaultScriptURL = "https://cdn.livesession.io/track.js"; +const snippet = (wnd = window, doc = document, type = "script", scriptURL = defaultScriptURL) => { + return ((w, d, t, u) => { + if (w.__ls) { + throw new Error("LiveSession script already added"); + } + const f = (w.__ls = function () { + f.push ? f.push.apply(f, arguments) : f.store.push(arguments); + }); + if (!w.__ls) + w.__ls = f; + f.store = []; + f.v = SDK_VERSION; + const ls = d.createElement(t); + ls.async = true; + ls.src = u; + ls.charset = "utf-8"; + ls.crossOrigin = "anonymous"; + const h = d.getElementsByTagName("head")[0]; + h.appendChild(ls); + })(wnd, doc, type, scriptURL); +}; +export default snippet; diff --git a/dist/index.d.ts b/dist/index.d.ts index b20ec4d..9d4cf9a 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -4,7 +4,7 @@ declare global { } } declare const _default: { - init: (trackID: string, options?: object | null, sdkOptions?: { + init: (trackID: string, options?: object | null | undefined, sdkOptions?: { devMode: boolean; scriptURL: string; }) => void; diff --git a/package.json b/package.json index 95d0222..26c3f45 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "1.2.0", "description": "Add a LiveSession script to your site even easier with our SDK", "main": "./dist/index.js", + "module": "./dist/esm/index.js", + "browser": "./dist/esm/index.js", "types": "./dist/index.d.ts", "repository": { "type": "git", @@ -22,7 +24,7 @@ "homepage": "https://livesession.io/", "scripts": { "watch": "tsc -w", - "build": "tsc", + "build": "tsc && tsc --project tsconfig.esm.json", "test": "NODE_ENV=production jest" }, "devDependencies": { @@ -32,4 +34,4 @@ "ts-jest": "^26.4.4", "typescript": "^4.1.3" } -} +} \ No newline at end of file diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..741a57c --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm", + "declaration": true, + "declarationDir": "dist/esm" + } +} \ No newline at end of file