Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update logEvent to pass eventType and replace segment calls wit… #7209

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions frontend/src/AppRoutes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { ConfigProvider } from 'antd';
import getLocalStorageApi from 'api/browser/localstorage/get';
import setLocalStorageApi from 'api/browser/localstorage/set';
import logEvent from 'api/common/logEvent';
import NotFound from 'components/NotFound';
import Spinner from 'components/Spinner';
import { FeatureKeys } from 'constants/features';
import { LOCALSTORAGE } from 'constants/localStorage';
import ROUTES from 'constants/routes';
import AppLayout from 'container/AppLayout';
import useAnalytics from 'hooks/analytics/useAnalytics';
import { KeyboardHotkeysProvider } from 'hooks/hotkeys/useKeyboardHotkeys';
import { useThemeConfig } from 'hooks/useDarkMode';
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';
import { LICENSE_PLAN_KEY } from 'hooks/useLicense';
import { NotificationProvider } from 'hooks/useNotifications';
import { ResourceProvider } from 'hooks/useResourceAttribute';
import history from 'lib/history';
import { identity, pickBy } from 'lodash-es';
import posthog from 'posthog-js';
import AlertRuleProvider from 'providers/Alert';
import { useAppContext } from 'providers/App/App';
Expand Down Expand Up @@ -51,8 +50,6 @@ function App(): JSX.Element {
} = useAppContext();
const [routes, setRoutes] = useState<AppRoutes[]>(defaultRoutes);

const { trackPageView } = useAnalytics();

const { hostname, pathname } = window.location;

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

const { name, email, role } = user;

const domain = extractDomain(email);
const hostNameParts = hostname.split('.');

const identifyPayload = {
email,
name,
company_name: orgName,
role,
tenant_id: hostNameParts[0],
data_region: hostNameParts[1],
tenant_url: hostname,
company_domain: domain,
source: 'signoz-ui',
role,
};

const sanitizedIdentifyPayload = pickBy(identifyPayload, identity);
const domain = extractDomain(email);
const hostNameParts = hostname.split('.');

const groupTraits = {
name: orgName,
tenant_id: hostNameParts[0],
Expand All @@ -90,8 +90,13 @@ function App(): JSX.Element {
source: 'signoz-ui',
};

window.analytics.identify(email, sanitizedIdentifyPayload);
window.analytics.group(domain, groupTraits);
if (email) {
logEvent('Email Identified', identifyPayload, 'identify');
}

if (domain) {
logEvent('Domain Identified', groupTraits, 'group');
}

posthog?.identify(email, {
email,
Expand Down Expand Up @@ -192,9 +197,7 @@ function App(): JSX.Element {
hide_default_launcher: false,
});
}

trackPageView(pathname);
}, [pathname, trackPageView]);
}, [pathname]);

useEffect(() => {
// feature flag shouldn't be loading and featureFlags or fetchError any one of this should be true indicating that req is complete
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/api/common/logEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { EventSuccessPayloadProps } from 'types/api/events/types';
const logEvent = async (
eventName: string,
attributes: Record<string, unknown>,
eventType?: 'track' | 'group' | 'identify',
rateLimited?: boolean,
): Promise<SuccessResponse<EventSuccessPayloadProps> | ErrorResponse> => {
try {
const response = await axios.post('/event', {
eventName,
attributes,
eventType: eventType || 'track',
rateLimited: rateLimited || false, // TODO: Update this once we have a proper way to handle rate limiting
});

return {
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/container/AppLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,16 @@ function AppLayout(props: AppLayoutProps): JSX.Element {
LOCALSTORAGE.DONT_SHOW_SLOW_API_WARNING,
);

logEvent(`Slow API Warning`, {
duration: `${data.duration}ms`,
url: data.url,
threshold: data.threshold,
});
logEvent(
`Slow API Warning`,
{
durationMs: data.duration,
url: data.url,
thresholdMs: data.threshold,
},
'track',
true, // rate limited - controlled by Backend
);

const isDontShowSlowApiWarning = dontShowSlowApiWarning === 'true';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ jest.mock('hooks/useNotifications', () => ({
})),
}));

window.analytics = {
track: jest.fn(),
};

describe('Onboarding invite team member flow', () => {
it('initial render and get started page', async () => {
const { findByText } = render(
Expand Down
40 changes: 0 additions & 40 deletions frontend/src/hooks/analytics/useAnalytics.tsx

This file was deleted.

59 changes: 21 additions & 38 deletions frontend/src/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@
/>
<meta data-react-helmet="true" name="docusaurus_locale" content="en" />
<meta data-react-helmet="true" name="docusaurus_tag" content="default" />
<meta name="robots" content="noindex">
<meta name="robots" content="noindex" />
<link data-react-helmet="true" rel="shortcut icon" href="/favicon.ico" />

<link rel="stylesheet" href="/css/uPlot.min.css" />


</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down Expand Up @@ -100,32 +98,16 @@
</script>
<script>
const CUSTOMERIO_ID = '<%= htmlWebpackPlugin.options.CUSTOMERIO_ID %>';
const CUSTOMERIO_SITE_ID = '<%= htmlWebpackPlugin.options.CUSTOMERIO_SITE_ID %>';
!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";
analytics.load(
CUSTOMERIO_ID,
{
"integrations": {
"Customer.io In-App Plugin": {
siteId: CUSTOMERIO_SITE_ID
}
}
}
);
analytics.page();
}}();
</script>
<script>
//Set your SEGMENT_ID
const SEGMENT_ID = '<%= htmlWebpackPlugin.options.SEGMENT_ID %>';
const CUSTOMERIO_SITE_ID =
'<%= htmlWebpackPlugin.options.CUSTOMERIO_SITE_ID %>';
!(function () {
var analytics = (window.analytics = window.analytics || []);
var i = 'cioanalytics',
analytics = (window[i] = window[i] || []);
if (!analytics.initialize)
if (analytics.invoked)
window.console &&
console.error &&
console.error('Segment snippet included twice.');
console.error('Snippet included twice.');
else {
analytics.invoked = !0;
analytics.methods = [
Expand All @@ -152,35 +134,36 @@
];
analytics.factory = function (e) {
return function () {
if (window.analytics.initialized)
return window.analytics[e].apply(window.analytics, arguments);
var i = Array.prototype.slice.call(arguments);
i.unshift(e);
analytics.push(i);
var t = Array.prototype.slice.call(arguments);
t.unshift(e);
analytics.push(t);
return analytics;
};
};
for (var i = 0; i < analytics.methods.length; i++) {
var key = analytics.methods[i];
for (var e = 0; e < analytics.methods.length; e++) {
var key = analytics.methods[e];
analytics[key] = analytics.factory(key);
}
analytics.load = function (key, i) {
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://analytics-cdn.signoz.io/analytics.js/v1/' +
'https://cdp.customer.io/v1/analytics-js/snippet/' +
key +
'/analytics.min.js';
var n = document.getElementsByTagName('script')[0];
n.parentNode.insertBefore(t, n);
analytics._loadOptions = i;
analytics._writeKey = key;
analytics._loadOptions = e;
};
analytics._writeKey = SEGMENT_ID;
analytics.SNIPPET_VERSION = '4.16.1';
analytics.load(SEGMENT_ID, {
analytics.SNIPPET_VERSION = '4.15.3';
analytics.load(CUSTOMERIO_ID, {
integrations: {
'Segment.io': { apiHost: 'analytics-api.signoz.io/v1' },
'Customer.io In-App Plugin': {
siteId: CUSTOMERIO_SITE_ID,
},
},
});
analytics.page();
Expand Down
2 changes: 0 additions & 2 deletions frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const plugins = [
new HtmlWebpackPlugin({
template: 'src/index.html.ejs',
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
SEGMENT_ID: process.env.SEGMENT_ID,
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
POSTHOG_KEY: process.env.POSTHOG_KEY,
Expand All @@ -41,7 +40,6 @@ const plugins = [
FRONTEND_API_ENDPOINT: process.env.FRONTEND_API_ENDPOINT,
WEBSOCKET_API_ENDPOINT: process.env.WEBSOCKET_API_ENDPOINT,
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
SEGMENT_ID: process.env.SEGMENT_ID,
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
POSTHOG_KEY: process.env.POSTHOG_KEY,
Expand Down
2 changes: 0 additions & 2 deletions frontend/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const plugins = [
new HtmlWebpackPlugin({
template: 'src/index.html.ejs',
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
SEGMENT_ID: process.env.SEGMENT_ID,
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
POSTHOG_KEY: process.env.POSTHOG_KEY,
Expand All @@ -51,7 +50,6 @@ const plugins = [
FRONTEND_API_ENDPOINT: process.env.FRONTEND_API_ENDPOINT,
WEBSOCKET_API_ENDPOINT: process.env.WEBSOCKET_API_ENDPOINT,
INTERCOM_APP_ID: process.env.INTERCOM_APP_ID,
SEGMENT_ID: process.env.SEGMENT_ID,
CUSTOMERIO_SITE_ID: process.env.CUSTOMERIO_SITE_ID,
CUSTOMERIO_ID: process.env.CUSTOMERIO_ID,
POSTHOG_KEY: process.env.POSTHOG_KEY,
Expand Down
Loading