Restructure omni services and add Chatwoot research snapshot

This commit is contained in:
Ruslan Bakiev
2026-02-21 11:11:27 +07:00
parent edea7a0034
commit b73babbbf6
7732 changed files with 978203 additions and 32 deletions

View File

@@ -0,0 +1,151 @@
export const CONVERSATION_EVENTS = Object.freeze({
EXECUTED_A_MACRO: 'Executed a macro',
SENT_MESSAGE: 'Sent a message',
SENT_PRIVATE_NOTE: 'Sent a private note',
INSERTED_A_CANNED_RESPONSE: 'Inserted a canned response',
TRANSLATE_A_MESSAGE: 'Translated a message',
INSERTED_A_VARIABLE: 'Inserted a variable',
INSERTED_AN_EMOJI: 'Inserted an emoji',
INSERTED_A_TOOL: 'Inserted a tool',
USED_MENTIONS: 'Used mentions',
SEARCH_CONVERSATION: 'Searched conversations',
APPLY_FILTER: 'Applied filters in the conversation list',
CHANGE_PRIORITY: 'Assigned priority to a conversation',
INSERT_ARTICLE_LINK: 'Inserted article into reply via article search',
});
export const ACCOUNT_EVENTS = Object.freeze({
ADDED_TO_CANNED_RESPONSE: 'Used added to canned response option',
ADDED_A_CUSTOM_ATTRIBUTE: 'Added a custom attribute',
ADDED_AN_INBOX: 'Added an inbox',
OPEN_MESSAGE_CONTEXT_MENU: 'Opened message context menu',
OPENED_NOTIFICATIONS: 'Opened notifications',
MARK_AS_READ_NOTIFICATIONS: 'Marked notifications as read',
OPEN_CONVERSATION_VIA_NOTIFICATION: 'Opened conversation via notification',
});
export const LABEL_EVENTS = Object.freeze({
CREATE: 'Created a label',
UPDATE: 'Updated a label',
DELETED: 'Deleted a label',
APPLY_LABEL: 'Applied a label',
});
// REPORTS EVENTS
export const REPORTS_EVENTS = Object.freeze({
DOWNLOAD_REPORT: 'Downloaded a report',
FILTER_REPORT: 'Used filters in the reports',
});
// CONTACTS PAGE EVENTS
export const CONTACTS_EVENTS = Object.freeze({
APPLY_FILTER: 'Applied filters in the contacts list',
SAVE_FILTER: 'Saved a filter in the contacts list',
DELETE_FILTER: 'Deleted a filter in the contacts list',
APPLY_SORT: 'Sorted contacts list',
SEARCH: 'Searched contacts list',
CREATE_CONTACT: 'Created a contact',
MERGED_CONTACTS: 'Used merge contact option',
IMPORT_MODAL_OPEN: 'Opened import contacts modal',
IMPORT_FAILURE: 'Import contacts failed',
IMPORT_SUCCESS: 'Imported contacts successfully',
});
// CAMPAIGN EVENTS
export const CAMPAIGNS_EVENTS = Object.freeze({
OPEN_NEW_CAMPAIGN_MODAL: 'Opened new campaign modal',
CREATE_CAMPAIGN: 'Created a new campaign',
UPDATE_CAMPAIGN: 'Updated a campaign',
DELETE_CAMPAIGN: 'Deleted a campaign',
});
// PORTAL EVENTS
export const PORTALS_EVENTS = Object.freeze({
ONBOARD_BASIC_INFORMATION: 'New Portal: Completed basic information',
ONBOARD_CUSTOMIZATION: 'New portal: Completed customization',
CREATE_PORTAL: 'Created a portal',
DELETE_PORTAL: 'Deleted a portal',
UPDATE_PORTAL: 'Updated a portal',
CREATE_LOCALE: 'Created a portal locale',
SET_DEFAULT_LOCALE: 'Set default portal locale',
DELETE_LOCALE: 'Deleted a portal locale',
SWITCH_LOCALE: 'Switched portal locale',
CREATE_CATEGORY: 'Created a portal category',
DELETE_CATEGORY: 'Deleted a portal category',
EDIT_CATEGORY: 'Edited a portal category',
CREATE_ARTICLE: 'Created an article',
PUBLISH_ARTICLE: 'Published an article',
ARCHIVE_ARTICLE: 'Archived an article',
DELETE_ARTICLE: 'Deleted an article',
PREVIEW_ARTICLE: 'Previewed article',
});
export const CAPTAIN_EVENTS = Object.freeze({
// Editor funnel events
EDITOR_AI_MENU_OPENED: 'Captain: Editor AI menu opened',
GENERATION_FAILED: 'Captain: Generation failed',
AI_ASSISTED_MESSAGE_SENT: 'Captain: AI-assisted message sent',
// Rewrite events (with operation attribute in payload)
REWRITE_USED: 'Captain: Rewrite used',
REWRITE_APPLIED: 'Captain: Rewrite applied',
REWRITE_DISMISSED: 'Captain: Rewrite dismissed',
// Summarize events
SUMMARIZE_USED: 'Captain: Summarize used',
SUMMARIZE_APPLIED: 'Captain: Summarize applied',
SUMMARIZE_DISMISSED: 'Captain: Summarize dismissed',
// Reply suggestion events
REPLY_SUGGESTION_USED: 'Captain: Reply suggestion used',
REPLY_SUGGESTION_APPLIED: 'Captain: Reply suggestion applied',
REPLY_SUGGESTION_DISMISSED: 'Captain: Reply suggestion dismissed',
// Follow-up events
FOLLOW_UP_SENT: 'Captain: Follow-up sent',
// Label suggestions
LABEL_SUGGESTION_APPLIED: 'Captain: Label suggestion applied',
LABEL_SUGGESTION_DISMISSED: 'Captain: Label suggestion dismissed',
});
export const COPILOT_EVENTS = Object.freeze({
SEND_SUGGESTED: 'Copilot: Send suggested message',
SEND_MESSAGE: 'Copilot: Sent a message',
USE_CAPTAIN_RESPONSE: 'Copilot: Used captain response',
});
export const GENERAL_EVENTS = Object.freeze({
COMMAND_BAR: 'Used commandbar',
});
export const INBOX_EVENTS = Object.freeze({
OPEN_CONVERSATION_VIA_INBOX: 'Opened conversation via inbox',
MARK_NOTIFICATION_AS_READ: 'Marked notification as read',
MARK_ALL_NOTIFICATIONS_AS_READ: 'Marked all notifications as read',
MARK_NOTIFICATION_AS_UNREAD: 'Marked notification as unread',
DELETE_NOTIFICATION: 'Deleted notification',
DELETE_ALL_NOTIFICATIONS: 'Deleted all notifications',
});
export const SLA_EVENTS = Object.freeze({
CREATE: 'Created an SLA',
UPDATE: 'Updated an SLA',
DELETED: 'Deleted an SLA',
});
export const LINEAR_EVENTS = Object.freeze({
CREATE_ISSUE: 'Created a linear issue',
LINK_ISSUE: 'Linked a linear issue',
UNLINK_ISSUE: 'Unlinked a linear issue',
});
export const YEAR_IN_REVIEW_EVENTS = Object.freeze({
MODAL_OPENED: 'Year in Review: Modal opened',
NEXT_CLICKED: 'Year in Review: Next clicked',
SHARE_CLICKED: 'Year in Review: Share clicked',
});

View File

@@ -0,0 +1,98 @@
import * as amplitude from '@amplitude/analytics-browser';
/**
* AnalyticsHelper class to initialize and track user analytics
* @class AnalyticsHelper
*/
export class AnalyticsHelper {
/**
* @constructor
* @param {Object} [options={}] - options for analytics
* @param {string} [options.token] - analytics token
*/
constructor({ token: analyticsToken } = {}) {
this.analyticsToken = analyticsToken;
this.analytics = null;
this.user = {};
}
/**
* Initialize analytics
* @function
* @async
*/
async init() {
if (!this.analyticsToken) {
return;
}
amplitude.init(this.analyticsToken, {
defaultTracking: false,
});
this.analytics = amplitude;
}
/**
* Identify the user
* @function
* @param {Object} user - User object
*/
identify(user) {
if (!this.analytics || !user) {
return;
}
this.user = user;
this.analytics.setUserId(`user-${this.user.id.toString()}`);
const identifyEvent = new amplitude.Identify();
identifyEvent.set('email', this.user.email);
identifyEvent.set('name', this.user.name);
identifyEvent.set('avatar', this.user.avatar_url);
this.analytics.identify(identifyEvent);
const { accounts, account_id: accountId } = this.user;
const [currentAccount] = accounts.filter(
account => account.id === accountId
);
if (currentAccount) {
const groupId = `account-${currentAccount.id.toString()}`;
this.analytics.setGroup('company', groupId);
const groupIdentify = new amplitude.Identify();
groupIdentify.set('name', currentAccount.name);
this.analytics.groupIdentify('company', groupId, groupIdentify);
}
}
/**
* Track any event
* @function
* @param {string} eventName - event name
* @param {Object} [properties={}] - event properties
*/
track(eventName, properties = {}) {
if (!this.analytics) {
return;
}
this.analytics.track(eventName, properties);
}
/**
* Track the page views
* @function
* @param {string} pageName - Page name
* @param {Object} [properties={}] - Page view properties
*/
page(pageName, properties = {}) {
if (!this.analytics) {
return;
}
this.analytics.track('$pageview', { pageName, ...properties });
}
}
// This object is shared across, the init is called in app/javascript/entrypoints/dashboard.js
export default new AnalyticsHelper(window.analyticsConfig);

View File

@@ -0,0 +1,26 @@
import * as AnalyticsEvents from '../events';
describe('Analytics Events', () => {
it('should be frozen', () => {
Object.entries(AnalyticsEvents).forEach(([, value]) => {
expect(Object.isFrozen(value)).toBe(true);
});
});
it('event names should be unique across the board', () => {
const allValues = Object.values(AnalyticsEvents).reduce(
(acc, curr) => acc.concat(Object.values(curr)),
[]
);
const uniqueValues = new Set(allValues);
expect(allValues.length).toBe(uniqueValues.size);
});
it('should not allow properties to be modified', () => {
Object.values(AnalyticsEvents).forEach(eventsObject => {
expect(() => {
eventsObject.NEW_PROPERTY = 'new value';
}).toThrow();
});
});
});

View File

@@ -0,0 +1,144 @@
import helperObject, { AnalyticsHelper } from '../';
vi.mock('@amplitude/analytics-browser', () => ({
init: vi.fn(),
setUserId: vi.fn(),
identify: vi.fn(),
setGroup: vi.fn(),
groupIdentify: vi.fn(),
track: vi.fn(),
Identify: vi.fn(() => ({
set: vi.fn(),
})),
}));
describe('helperObject', () => {
it('should return an instance of AnalyticsHelper', () => {
expect(helperObject).toBeInstanceOf(AnalyticsHelper);
});
});
describe('AnalyticsHelper', () => {
let analyticsHelper;
beforeEach(() => {
analyticsHelper = new AnalyticsHelper({ token: 'test_token' });
});
describe('init', () => {
it('should initialize amplitude with the correct token', async () => {
await analyticsHelper.init();
expect(analyticsHelper.analytics).not.toBe(null);
});
it('should not initialize amplitude if token is not provided', async () => {
analyticsHelper = new AnalyticsHelper();
await analyticsHelper.init();
expect(analyticsHelper.analytics).toBe(null);
});
});
describe('identify', () => {
beforeEach(() => {
analyticsHelper.analytics = {
setUserId: vi.fn(),
identify: vi.fn(),
setGroup: vi.fn(),
groupIdentify: vi.fn(),
};
});
it('should call setUserId and identify on amplitude with correct arguments', () => {
analyticsHelper.identify({
id: 123,
email: 'test@example.com',
name: 'Test User',
avatar_url: 'avatar_url',
accounts: [{ id: 1, name: 'Account 1' }],
account_id: 1,
});
expect(analyticsHelper.analytics.setUserId).toHaveBeenCalledWith(
'user-123'
);
expect(analyticsHelper.analytics.identify).toHaveBeenCalled();
expect(analyticsHelper.analytics.setGroup).toHaveBeenCalledWith(
'company',
'account-1'
);
expect(analyticsHelper.analytics.groupIdentify).toHaveBeenCalled();
});
it('should call identify on amplitude without group', () => {
analyticsHelper.identify({
id: 123,
email: 'test@example.com',
name: 'Test User',
avatar_url: 'avatar_url',
accounts: [{ id: 1, name: 'Account 1' }],
account_id: 5,
});
expect(analyticsHelper.analytics.setGroup).not.toHaveBeenCalled();
});
it('should not call analytics methods if analytics is null', () => {
analyticsHelper.analytics = null;
analyticsHelper.identify({});
expect(analyticsHelper.analytics).toBe(null);
});
});
describe('track', () => {
beforeEach(() => {
analyticsHelper.analytics = { track: vi.fn() };
analyticsHelper.user = { id: 123 };
});
it('should call track on amplitude with correct arguments', () => {
analyticsHelper.track('Test Event', { prop1: 'value1', prop2: 'value2' });
expect(analyticsHelper.analytics.track).toHaveBeenCalledWith(
'Test Event',
{ prop1: 'value1', prop2: 'value2' }
);
});
it('should call track on amplitude with default properties', () => {
analyticsHelper.track('Test Event');
expect(analyticsHelper.analytics.track).toHaveBeenCalledWith(
'Test Event',
{}
);
});
it('should not call track on amplitude if analytics is not initialized', () => {
analyticsHelper.analytics = null;
analyticsHelper.track('Test Event', { prop1: 'value1', prop2: 'value2' });
expect(analyticsHelper.analytics).toBe(null);
});
});
describe('page', () => {
beforeEach(() => {
analyticsHelper.analytics = { track: vi.fn() };
});
it('should call the track method for pageview with the correct arguments', () => {
const pageName = 'home';
const properties = {
path: '/test',
name: 'home',
};
analyticsHelper.page(pageName, properties);
expect(analyticsHelper.analytics.track).toHaveBeenCalledWith(
'$pageview',
{ pageName: 'home', path: '/test', name: 'home' }
);
});
it('should not call analytics.track if analytics is null', () => {
analyticsHelper.analytics = null;
analyticsHelper.page('home');
expect(analyticsHelper.analytics).toBe(null);
});
});
});