Skip to content

Commit d362f5b

Browse files
YounixMmakeavish
andauthored
feat: update logEvent to pass eventType and replace segment calls wit… (#7209)
* feat: update logEvent to pass eventType and replace segment calls with logEvent * feat: update logEvent to handle rate limiting --------- Co-authored-by: Vishal Sharma <[email protected]>
1 parent 42f7511 commit d362f5b

File tree

8 files changed

+52
-105
lines changed

8 files changed

+52
-105
lines changed

frontend/src/AppRoutes/index.tsx

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import { ConfigProvider } from 'antd';
22
import getLocalStorageApi from 'api/browser/localstorage/get';
33
import setLocalStorageApi from 'api/browser/localstorage/set';
4+
import logEvent from 'api/common/logEvent';
45
import NotFound from 'components/NotFound';
56
import Spinner from 'components/Spinner';
67
import { FeatureKeys } from 'constants/features';
78
import { LOCALSTORAGE } from 'constants/localStorage';
89
import ROUTES from 'constants/routes';
910
import AppLayout from 'container/AppLayout';
10-
import useAnalytics from 'hooks/analytics/useAnalytics';
1111
import { KeyboardHotkeysProvider } from 'hooks/hotkeys/useKeyboardHotkeys';
1212
import { useThemeConfig } from 'hooks/useDarkMode';
1313
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';
1414
import { LICENSE_PLAN_KEY } from 'hooks/useLicense';
1515
import { NotificationProvider } from 'hooks/useNotifications';
1616
import { ResourceProvider } from 'hooks/useResourceAttribute';
1717
import history from 'lib/history';
18-
import { identity, pickBy } from 'lodash-es';
1918
import posthog from 'posthog-js';
2019
import AlertRuleProvider from 'providers/Alert';
2120
import { useAppContext } from 'providers/App/App';
@@ -51,8 +50,6 @@ function App(): JSX.Element {
5150
} = useAppContext();
5251
const [routes, setRoutes] = useState<AppRoutes[]>(defaultRoutes);
5352

54-
const { trackPageView } = useAnalytics();
55-
5653
const { hostname, pathname } = window.location;
5754

5855
const {
@@ -69,18 +66,21 @@ function App(): JSX.Element {
6966

7067
const { name, email, role } = user;
7168

69+
const domain = extractDomain(email);
70+
const hostNameParts = hostname.split('.');
71+
7272
const identifyPayload = {
7373
email,
7474
name,
7575
company_name: orgName,
76-
role,
76+
tenant_id: hostNameParts[0],
77+
data_region: hostNameParts[1],
78+
tenant_url: hostname,
79+
company_domain: domain,
7780
source: 'signoz-ui',
81+
role,
7882
};
7983

80-
const sanitizedIdentifyPayload = pickBy(identifyPayload, identity);
81-
const domain = extractDomain(email);
82-
const hostNameParts = hostname.split('.');
83-
8484
const groupTraits = {
8585
name: orgName,
8686
tenant_id: hostNameParts[0],
@@ -90,8 +90,13 @@ function App(): JSX.Element {
9090
source: 'signoz-ui',
9191
};
9292

93-
window.analytics.identify(email, sanitizedIdentifyPayload);
94-
window.analytics.group(domain, groupTraits);
93+
if (email) {
94+
logEvent('Email Identified', identifyPayload, 'identify');
95+
}
96+
97+
if (domain) {
98+
logEvent('Domain Identified', groupTraits, 'group');
99+
}
95100

96101
posthog?.identify(email, {
97102
email,
@@ -192,9 +197,7 @@ function App(): JSX.Element {
192197
hide_default_launcher: false,
193198
});
194199
}
195-
196-
trackPageView(pathname);
197-
}, [pathname, trackPageView]);
200+
}, [pathname]);
198201

199202
useEffect(() => {
200203
// feature flag shouldn't be loading and featureFlags or fetchError any one of this should be true indicating that req is complete

frontend/src/api/common/logEvent.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import { EventSuccessPayloadProps } from 'types/api/events/types';
77
const logEvent = async (
88
eventName: string,
99
attributes: Record<string, unknown>,
10+
eventType?: 'track' | 'group' | 'identify',
11+
rateLimited?: boolean,
1012
): Promise<SuccessResponse<EventSuccessPayloadProps> | ErrorResponse> => {
1113
try {
1214
const response = await axios.post('/event', {
1315
eventName,
1416
attributes,
17+
eventType: eventType || 'track',
18+
rateLimited: rateLimited || false, // TODO: Update this once we have a proper way to handle rate limiting
1519
});
1620

1721
return {

frontend/src/container/AppLayout/index.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,16 @@ function AppLayout(props: AppLayoutProps): JSX.Element {
392392
LOCALSTORAGE.DONT_SHOW_SLOW_API_WARNING,
393393
);
394394

395-
logEvent(`Slow API Warning`, {
396-
duration: `${data.duration}ms`,
397-
url: data.url,
398-
threshold: data.threshold,
399-
});
395+
logEvent(
396+
`Slow API Warning`,
397+
{
398+
durationMs: data.duration,
399+
url: data.url,
400+
thresholdMs: data.threshold,
401+
},
402+
'track',
403+
true, // rate limited - controlled by Backend
404+
);
400405

401406
const isDontShowSlowApiWarning = dontShowSlowApiWarning === 'true';
402407

frontend/src/container/OnboardingContainer/__test__/InviteUserFlow.test.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ jest.mock('hooks/useNotifications', () => ({
1919
})),
2020
}));
2121

22-
window.analytics = {
23-
track: jest.fn(),
24-
};
25-
2622
describe('Onboarding invite team member flow', () => {
2723
it('initial render and get started page', async () => {
2824
const { findByText } = render(

frontend/src/hooks/analytics/useAnalytics.tsx

-40
This file was deleted.

frontend/src/index.html.ejs

+21-38
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@
4949
/>
5050
<meta data-react-helmet="true" name="docusaurus_locale" content="en" />
5151
<meta data-react-helmet="true" name="docusaurus_tag" content="default" />
52-
<meta name="robots" content="noindex">
52+
<meta name="robots" content="noindex" />
5353
<link data-react-helmet="true" rel="shortcut icon" href="/favicon.ico" />
5454

5555
<link rel="stylesheet" href="/css/uPlot.min.css" />
56-
57-
5856
</head>
5957
<body>
6058
<noscript>You need to enable JavaScript to run this app.</noscript>
@@ -100,32 +98,16 @@
10098
</script>
10199
<script>
102100
const CUSTOMERIO_ID = '<%= htmlWebpackPlugin.options.CUSTOMERIO_ID %>';
103-
const CUSTOMERIO_SITE_ID = '<%= htmlWebpackPlugin.options.CUSTOMERIO_SITE_ID %>';
104-
!function(){var i="cioanalytics", analytics=(window[i]=window[i]||[]);if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.setAttribute('data-global-customerio-analytics-key', i);t.src="https://cdp.customer.io/v1/analytics-js/snippet/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);analytics._writeKey=key;analytics._loadOptions=e};analytics.SNIPPET_VERSION="4.15.3";
105-
analytics.load(
106-
CUSTOMERIO_ID,
107-
{
108-
"integrations": {
109-
"Customer.io In-App Plugin": {
110-
siteId: CUSTOMERIO_SITE_ID
111-
}
112-
}
113-
}
114-
);
115-
analytics.page();
116-
}}();
117-
</script>
118-
<script>
119-
//Set your SEGMENT_ID
120-
const SEGMENT_ID = '<%= htmlWebpackPlugin.options.SEGMENT_ID %>';
121-
101+
const CUSTOMERIO_SITE_ID =
102+
'<%= htmlWebpackPlugin.options.CUSTOMERIO_SITE_ID %>';
122103
!(function () {
123-
var analytics = (window.analytics = window.analytics || []);
104+
var i = 'cioanalytics',
105+
analytics = (window[i] = window[i] || []);
124106
if (!analytics.initialize)
125107
if (analytics.invoked)
126108
window.console &&
127109
console.error &&
128-
console.error('Segment snippet included twice.');
110+
console.error('Snippet included twice.');
129111
else {
130112
analytics.invoked = !0;
131113
analytics.methods = [
@@ -152,35 +134,36 @@
152134
];
153135
analytics.factory = function (e) {
154136
return function () {
155-
if (window.analytics.initialized)
156-
return window.analytics[e].apply(window.analytics, arguments);
157-
var i = Array.prototype.slice.call(arguments);
158-
i.unshift(e);
159-
analytics.push(i);
137+
var t = Array.prototype.slice.call(arguments);
138+
t.unshift(e);
139+
analytics.push(t);
160140
return analytics;
161141
};
162142
};
163-
for (var i = 0; i < analytics.methods.length; i++) {
164-
var key = analytics.methods[i];
143+
for (var e = 0; e < analytics.methods.length; e++) {
144+
var key = analytics.methods[e];
165145
analytics[key] = analytics.factory(key);
166146
}
167-
analytics.load = function (key, i) {
147+
analytics.load = function (key, e) {
168148
var t = document.createElement('script');
169149
t.type = 'text/javascript';
170150
t.async = !0;
151+
t.setAttribute('data-global-customerio-analytics-key', i);
171152
t.src =
172-
'https://analytics-cdn.signoz.io/analytics.js/v1/' +
153+
'https://cdp.customer.io/v1/analytics-js/snippet/' +
173154
key +
174155
'/analytics.min.js';
175156
var n = document.getElementsByTagName('script')[0];
176157
n.parentNode.insertBefore(t, n);
177-
analytics._loadOptions = i;
158+
analytics._writeKey = key;
159+
analytics._loadOptions = e;
178160
};
179-
analytics._writeKey = SEGMENT_ID;
180-
analytics.SNIPPET_VERSION = '4.16.1';
181-
analytics.load(SEGMENT_ID, {
161+
analytics.SNIPPET_VERSION = '4.15.3';
162+
analytics.load(CUSTOMERIO_ID, {
182163
integrations: {
183-
'Segment.io': { apiHost: 'analytics-api.signoz.io/v1' },
164+
'Customer.io In-App Plugin': {
165+
siteId: CUSTOMERIO_SITE_ID,
166+
},
184167
},
185168
});
186169
analytics.page();

frontend/webpack.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const plugins = [
2121
new HtmlWebpackPlugin({
2222
template: 'src/index.html.ejs',
2323
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
24-
SEGMENT_ID: process.env.SEGMENT_ID,
2524
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
2625
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
2726
POSTHOG_KEY: process.env.POSTHOG_KEY,
@@ -41,7 +40,6 @@ const plugins = [
4140
FRONTEND_API_ENDPOINT: process.env.FRONTEND_API_ENDPOINT,
4241
WEBSOCKET_API_ENDPOINT: process.env.WEBSOCKET_API_ENDPOINT,
4342
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
44-
SEGMENT_ID: process.env.SEGMENT_ID,
4543
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
4644
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
4745
POSTHOG_KEY: process.env.POSTHOG_KEY,

frontend/webpack.config.prod.js

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const plugins = [
2626
new HtmlWebpackPlugin({
2727
template: 'src/index.html.ejs',
2828
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
29-
SEGMENT_ID: process.env.SEGMENT_ID,
3029
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
3130
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
3231
POSTHOG_KEY: process.env.POSTHOG_KEY,
@@ -51,7 +50,6 @@ const plugins = [
5150
FRONTEND_API_ENDPOINT: process.env.FRONTEND_API_ENDPOINT,
5251
WEBSOCKET_API_ENDPOINT: process.env.WEBSOCKET_API_ENDPOINT,
5352
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
54-
SEGMENT_ID: process.env.SEGMENT_ID,
5553
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
5654
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
5755
POSTHOG_KEY: process.env.POSTHOG_KEY,

0 commit comments

Comments
 (0)