Skip to content

Commit

Permalink
[RHOAIENG-9754] Use the npm-Version of the Segment libraries instead …
Browse files Browse the repository at this point in the history
…of loading from remote. (#3014)
  • Loading branch information
pilhuhn authored Aug 5, 2024
1 parent 73e69e5 commit e7239c5
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 67 deletions.
187 changes: 185 additions & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@patternfly/react-tokens": "^5.3.1",
"@patternfly/react-topology": "^5.4.0-prerelease.10",
"@patternfly/react-virtualized-extension": "^5.1.0",
"@segment/analytics-next": "^1.72.0",
"@types/classnames": "^2.3.1",
"axios": "^1.6.4",
"classnames": "^2.2.6",
Expand Down
82 changes: 17 additions & 65 deletions frontend/src/concepts/analyticsTracking/initSegment.tsx
Original file line number Diff line number Diff line change
@@ -1,74 +1,26 @@
import { AnalyticsBrowser } from '@segment/analytics-next';

export const initSegment = async (props: {
segmentKey: string;
enabled: boolean;
}): Promise<void> => {
const { segmentKey, enabled } = props;
const analytics = (window.analytics = window.analytics || []);
if (analytics.initialize) {
if (!enabled || !segmentKey) {
return;
}
if (analytics.invoked) {
/* eslint-disable-next-line no-console */
console.error('Segment snippet included twice.');
} else {
analytics.invoked = true;
analytics.methods = [
'trackSubmit',
'trackClick',
'trackLink',
'trackForm',
'pageview',
'identify',
'reset',
'group',
'track',
'ready',
'alias',
'debug',
'page',
'once',
'off',
'on',
'addSourceMiddleware',
'addIntegrationMiddleware',
'setAnonymousId',
'addDestinationMiddleware',
];
analytics.factory =
(e: string) =>
(...t: unknown[]) => {
t.unshift(e);
analytics.push(t);
return analytics;
};
for (let e = 0; e < analytics.methods.length; e++) {
const key = analytics.methods[e];
analytics[key] = analytics.factory(key);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
analytics.load = (key: string, options: any) => {
const t = document.createElement('script');
t.type = 'text/javascript';
t.async = true;
t.src = `https://console.redhat.com/connections/cdn/analytics.js/v1/${encodeURIComponent(
key,
)}/analytics.min.js`;
const n = document.getElementsByTagName('script')[0];
if (n.parentNode) {
n.parentNode.insertBefore(t, n);
}
analytics._loadOptions = options;
};
analytics.SNIPPET_VERSION = '4.13.1';
if (segmentKey && enabled) {
analytics.load(segmentKey, {
integrations: {
'Segment.io': {
apiHost: 'console.redhat.com/connections/api/v1',
protocol: 'https',
},
window.analytics = AnalyticsBrowser.load(
{
writeKey: segmentKey,
cdnURL: 'https://console.redhat.com/connections/cdn',
},

{
integrations: {
'Segment.io': {
apiHost: 'console.redhat.com/connections/api/v1',
protocol: 'https',
},
});
}
}
},
},
);
};

0 comments on commit e7239c5

Please sign in to comment.