@vutolabs/analytics
Version:
Vuto Analytics - instant, Stripe-native analytics for SaaS apps built with Next.js or React.
142 lines (139 loc) • 3.86 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
identify: () => identify,
track: () => track
});
module.exports = __toCommonJS(index_exports);
// src/utils.ts
function isBrowser() {
return typeof window !== "undefined";
}
function detectEnvironment() {
try {
const env = process.env.NODE_ENV;
if (env === "development" || env === "test") {
return "development";
}
} catch (e) {
}
return "production";
}
function getMode() {
const mode = isBrowser() ? window.vam : detectEnvironment();
return mode || "production";
}
function isProduction() {
return getMode() === "production";
}
function isDevelopment() {
return getMode() === "development";
}
function removeKey(key, { [key]: _, ...rest }) {
return rest;
}
function parseProperties(properties, options) {
if (!properties) return void 0;
let props = properties;
const errorProperties = [];
for (const [key, value] of Object.entries(properties)) {
if (typeof value === "object" && value !== null) {
if (options.strip) {
props = removeKey(key, props);
} else {
errorProperties.push(key);
}
}
}
if (errorProperties.length > 0 && !options.strip) {
throw Error(
`The following properties are not valid: ${errorProperties.join(
", "
)}. Only strings, numbers, booleans, and null are allowed.`
);
}
return props;
}
// src/generic.ts
function track(name, properties, options) {
var _a, _b;
if (!isBrowser()) {
const msg = "[Vuto Web Analytics] Please import `track` from `@vutolabs/analytics/server` when using this function in a server environment";
if (isProduction()) {
console.warn(msg);
} else {
throw new Error(msg);
}
return;
}
if (!properties) {
(_a = window.va) == null ? void 0 : _a.call(window, "event", {
name,
options
});
return;
}
try {
const props = parseProperties(properties, {
strip: isProduction()
});
(_b = window.va) == null ? void 0 : _b.call(window, "event", {
name,
data: props,
options
});
} catch (err) {
if (err instanceof Error && isDevelopment()) {
console.error(err);
}
}
}
function identify(email) {
var _a;
if (!isBrowser()) {
const msg = "[Vuto Web Analytics] Please import `identify` from `@vutolabs/analytics/server` when using this function in a server environment";
if (isProduction()) {
console.warn(msg);
} else {
throw new Error(msg);
}
return;
}
if (!email || typeof email !== "string") {
if (isDevelopment()) {
console.error(
"[Vuto Web Analytics] Invalid email provided to identify(). Email must be a non-empty string."
);
}
return;
}
const vid = localStorage.getItem("analytics_vid");
(_a = window.va) == null ? void 0 : _a.call(window, "identify", {
email,
vid
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
identify,
track
});
//# sourceMappingURL=index.js.map