Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/api.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ var ConsoleLogLevel;
ConsoleLogLevel["info"] = "info";
ConsoleLogLevel["warn"] = "warn";
ConsoleLogLevel["error"] = "error";
})(ConsoleLogLevel || (exports.ConsoleLogLevel = ConsoleLogLevel = {}));
})(ConsoleLogLevel = exports.ConsoleLogLevel || (exports.ConsoleLogLevel = {}));
18 changes: 18 additions & 0 deletions dist/esm/api.d.ts
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions dist/esm/api.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions dist/esm/api.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare enum ConsoleLogLevel {
log = "log",
info = "info",
warn = "warn",
error = "error"
}
7 changes: 7 additions & 0 deletions dist/esm/api.types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export var ConsoleLogLevel;
(function (ConsoleLogLevel) {
ConsoleLogLevel["log"] = "log";
ConsoleLogLevel["info"] = "info";
ConsoleLogLevel["warn"] = "warn";
ConsoleLogLevel["error"] = "error";
})(ConsoleLogLevel || (ConsoleLogLevel = {}));
23 changes: 23 additions & 0 deletions dist/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
69 changes: 69 additions & 0 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
3 changes: 3 additions & 0 deletions dist/esm/snippet.d.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions dist/esm/snippet.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -32,4 +34,4 @@
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
}
}
}
9 changes: 9 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm",
"declaration": true,
"declarationDir": "dist/esm"
}
}