@open-condo/miniapp-utils
Version:
A set of helper functions / components / hooks used to build new condo apps fast
109 lines (107 loc) • 3.6 kB
JavaScript
// src/helpers/analytics/instance.ts
import { Analytics as DefaultAnalytics } from "analytics";
// src/helpers/analytics/middlewares.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/instance.ts
var Analytics = class _Analytics {
constructor(config) {
this._groups = /* @__PURE__ */ new Set();
this._analytics = DefaultAnalytics({
...config,
plugins: [
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);
}
};
export {
Analytics
};
//# sourceMappingURL=index.mjs.map