@open-condo/miniapp-utils
Version:
A set of helper functions / components / hooks used to build new condo apps fast
155 lines (150 loc) • 5.28 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/helpers/analytics/index.ts
var analytics_exports = {};
__export(analytics_exports, {
Analytics: () => Analytics
});
module.exports = __toCommonJS(analytics_exports);
// src/helpers/analytics/instance.ts
var import_analytics = require("analytics");
// src/helpers/analytics/middlewares/grouping.ts
function _addGroupingProperties(data) {
const { instance } = data;
for (const groupName of instance.groups) {
const groupKey = Analytics.getGroupKey(groupName);
const groupValue = instance.storage.getItem(groupKey);
if (typeof groupValue === "string") {
const groupAttrName = `groups.${groupName}`;
data.payload.properties[groupAttrName] = groupValue;
}
}
return data;
}
var GroupingMiddlewarePlugin = {
name: "analytics-plugin-grouping",
track: _addGroupingProperties,
page: _addGroupingProperties
};
// src/helpers/analytics/middlewares/identity.ts
var IDENTITY_PROPERTIES = ["app", "version"];
function _addIdentityProperties(data) {
const { instance } = data;
for (const contextPropertyName of IDENTITY_PROPERTIES) {
const propertyValue = instance.getState(`context.${contextPropertyName}`);
if (typeof propertyValue === "string") {
data.payload.properties[contextPropertyName] = propertyValue;
}
}
return data;
}
var IdentityMiddlewarePlugin = {
name: "analytics-plugin-identity",
track: _addIdentityProperties,
page: _addIdentityProperties
};
// src/helpers/analytics/instance.ts
var Analytics = class _Analytics {
constructor(config) {
this._groups = /* @__PURE__ */ new Set();
this._analytics = (0, import_analytics.Analytics)({
...config,
plugins: [
IdentityMiddlewarePlugin,
GroupingMiddlewarePlugin,
...config.plugins || []
]
});
this._analytics.groups = this._groups;
}
/**
* Tracks type-safe business events. Recommended to use in most cases in app's codebase.
* To add an event, modify "Events" generic.
*/
async track(eventName, eventData) {
await this._analytics.track(eventName, eventData);
}
/**
* Tracks untyped analytics events, used mainly for external sources (bridge / ui-kit / messages, etc.)
* @deprecated It's not recommended to use this in your business logic, consider using typed "track" instead
*/
async trackUntyped(eventName, eventData) {
await this._analytics.track(eventName, eventData);
}
/**
* Tracks page changing in SPAs
*/
async pageView(data) {
await this._analytics.page(data);
}
/**
* Identifies user in analytics provider.
* To specify all possible shape of user's data, modify "UserData" generic
*
* NOTE: Analytics plugins don't have a fixed behavior on how to handle consecutive identify calls.
* Some of them affect only subsequent events, others affect all user events.
* Therefore, it is not recommended to put cohort-specific data (organization, address, language, etc.) here.
* Instead, use something like "group" method if your plugins supports it.
*/
async identify(userId, userData) {
await this._analytics.identify(userId, userData);
}
/**
* Resets analytics providers
*/
async reset() {
for (const groupName of this._groups) {
const groupKey = _Analytics.getGroupKey(groupName);
this._analytics.storage.removeItem(groupKey);
}
this._groups.clear();
await this._analytics.reset();
}
static getGroupKey(groupName) {
return ["analytics", "groups", groupName].join(":");
}
/**
* Associates the user with a group, adding the attributes `groups.${groupName} = groupId`
* to all subsequent analytic queries for the user
* @example
* analytics.setGroup('organization', organizationId)
*/
setGroup(groupName, groupId) {
const groupKey = _Analytics.getGroupKey(groupName);
this._groups.add(groupName);
this._analytics.storage.setItem(groupKey, groupId);
}
/**
* Removes the current user from the group, stripping the “groups.${groupName}”
* attribute from all subsequent eventualities
* @example
* deleteOrganization()
* .then(() => analytics.removeGroup('organization'))
*/
removeGroup(groupName) {
const groupKey = _Analytics.getGroupKey(groupName);
this._analytics.storage.removeItem(groupKey);
this._groups.delete(groupName);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Analytics
});
//# sourceMappingURL=index.js.map